如果 cameraX 的 videoCapture 和 ImageAnlaysis 一起使用,预览会变得很奇怪。我该怎么办?& 如何在视频帧上绘图?

If cameraX's videoCapture and ImageAnlaysis are used together, the preview becomes strange. What should I do? & How can I draw on a video frame?

提问人:eun_n 提问时间:7/23/2023 更新时间:7/23/2023 访问量:38

问:

  1. 当 cameraX 的 videoCapture 和 ImageAnlaysis 一起使用时,预览会旋转 90 度,图库中录制的视频会比实际长度长。为什么会这样?
private fun bindUseCase() {
        if (cameraProvider == null) {
            return
        }

        if (analysisUseCase != null) {
            cameraProvider!!.unbind(analysisUseCase)
        }

        if (imageProcessor != null) {
            imageProcessor!!.stop()
        }

        if (!PreferenceUtils.isCameraLiveViewportEnabled(this.context)) {
            return
        }

        if (previewUseCase != null) {
            cameraProvider!!.unbind(previewUseCase)
        }

        val previewBuilder = Preview.Builder()
        previewUseCase = previewBuilder.build()
        previewUseCase!!.setSurfaceProvider(previewView!!.surfaceProvider)

        val recorder = Recorder.Builder()
            .setQualitySelector(QualitySelector.from(Quality.HIGHEST))
            .build()

        videoCapture = VideoCapture.withOutput(recorder)

        imageProcessor =
            try {
                Log.i(TAG, "Using Face Detector Processor")
                val faceDetectorOptions = FaceDetectorOptions.Builder()
                    .setClassificationMode(FaceDetectorOptions.CLASSIFICATION_MODE_ALL)
                    .enableTracking()
                    .build()

                FaceDetectorProcessor(this.context, faceDetectorOptions)
            } catch (e: Exception) {
                Log.e(TAG,"imageProcessor",e)
                return
            }

        val builder = ImageAnalysis.Builder()
        analysisUseCase = builder.build()

        needUpdateGraphicOverlayImageSourceInfo = true

        analysisUseCase?.setAnalyzer(
            // imageProcessor.processImageProxy will use another thread to run the detection underneath,
            // thus we can just runs the analyzer itself on main thread.
            ContextCompat.getMainExecutor(this.context),
            ImageAnalysis.Analyzer { imageProxy: ImageProxy ->
                if (needUpdateGraphicOverlayImageSourceInfo) {
                    val isImageFlipped = lensFacing == CameraSelector.LENS_FACING_FRONT
                    val rotationDegrees = imageProxy.imageInfo.rotationDegrees
                    if (rotationDegrees == 0 || rotationDegrees == 180) {
                        graphicOverlay!!.setImageSourceInfo(imageProxy.width, imageProxy.height, isImageFlipped)
                    } else {
                        graphicOverlay!!.setImageSourceInfo(imageProxy.height, imageProxy.width, isImageFlipped)
                    }
                    needUpdateGraphicOverlayImageSourceInfo = false
                }
                try {
                    imageProcessor!!.processImageProxy(imageProxy, graphicOverlay)


                } catch (e: MlKitException) {
                    Log.e(TAG, "Failed to process image. Error: " + e.localizedMessage)
                }
            }
        )
        Log.d(TAG,"$cameraProvider, $cameraSelector, $analysisUseCase, $imageProcessor,$previewUseCase")
        cameraProvider!!.bindToLifecycle(this, cameraSelector!!,analysisUseCase,videoCapture,previewUseCase)
    }

如果从上面的代码中减去“analysisUseCase”,预览和录制时间就会正常出来。

2. 我想在视频帧上绘制人脸识别的结果。目前,它仅在预览中绘制,但我希望将结果记录在一起。我该怎么办?

视频捕获 android-camerax ondraw google-mlkit

评论

0赞 Xi 张熹 7/24/2023
您使用的是哪个版本的 CameraX?我建议您尝试最新的 1.3.0-beta01 版本。此外,如果您可以尝试 LifecycleCameraController API,它可能会解决您的问题。然而,绘制视频是一个难题。我们正在为此开发一项功能,但没有预计到达时间。如果您有兴趣,这里有一个正在加工的原型:android-review.googlesource.com/c/platform/frameworks/support/+/...

答: 暂无答案