Android CameraX正在takePicture()方法中捕获图像

8xiog9wr  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(151)

我正在使用下面的代码从camerax相机捕获,

imageCapture.takePicture(outputOptions,
    cameraExecutor,// ContextCompat.getMainExecutor(cntx),
j0pj023g

j0pj023g1#

建议将cameraX与替代线程一起使用。
试着像这样使用它:

private fun startCamera() {
    binding.previewView.visibility = View.VISIBLE
    cameraExecutor = Executors.newSingleThreadExecutor()
    val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())

    cameraProviderFuture.addListener({
        cameraProvider = cameraProviderFuture.get()

        val preview = Preview.Builder()
            .build()
            .also {
                it.setSurfaceProvider(binding.previewView.surfaceProvider)
            }

        val imageAnalyzer = ImageAnalysis.Builder()
            .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
            .build()
            .also {
                it.setAnalyzer(cameraExecutor, CodeAnalyser(this@HomeFragment))
            }

        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        try {
            cameraProvider.unbindAll()

            cameraProvider.bindToLifecycle(
                this, cameraSelector, preview, imageAnalyzer
            )

        } catch (exc: Exception) {
            exc.printStackTrace()
        }
    }, ContextCompat.getMainExecutor(requireContext()))
}

通过这样做,您的主线程不会被持续的图像处理阻塞。
这是一个假设。您没有共享足够的代码让我专门诊断您的问题。

相关问题