Android CameraX 检索摄像机配置文件参数时出错

Android CameraX Error retrieving camcorder profile params

我正在迁移到 cameraX 并遇到异常

androidx.camera.core.InitializationException: java.lang.RuntimeException: Error retrieving camcorder profile params

错误日志指出调用

时会抛出此错误
            cameraProvider = cameraProviderFuture.get()

我无法在任何设备上重现此错误。

我在这里初始化相机:

private fun initCameraProvider() {
    val cameraProviderFuture = ProcessCameraProvider.getInstance(this) //Error is thrown here
    cameraProviderFuture.addListener(Runnable {
        cameraProvider = cameraProviderFuture.get()
        val metrics = DisplayMetrics().also {
            previewView?.display?.getRealMetrics(it) 
        }
        val screenAspectRatio = ImageUtils.aspectRatio(metrics.widthPixels, metrics.heightPixels)
        val rotation = previewView?.display?.rotation ?: 0

        preview = Preview.Builder()
                .setTargetAspectRatio(screenAspectRatio)
                .setTargetRotation(rotation)
                .build()
        analyzer = BarcodeAnalyzer(previewView, overlay) { onSuccess(it) }
        val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

        imageAnalyzer = ImageAnalysis.Builder()
                .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
                .setTargetAspectRatio(screenAspectRatio)
                .build()
                .also {
                    it.setAnalyzer(cameraExecutor, analyzer)
                }


        val useCaseGroup = UseCaseGroup.Builder()
                .addUseCase(preview)
                .addUseCase(imageAnalyzer)
                .build()
        try {
            cameraProvider?.unbindAll()
            preview.setSurfaceProvider(previewView?.surfaceProvider)
            camera = cameraProvider?.bindToLifecycle(this, cameraSelector, useCaseGroup)
        } catch (exc: Exception) {
            Log.e(TAG, "Use case binding failed", exc)
        }


    }, ContextCompat.getMainExecutor(this))
}

我在 Samsung s20+ 和 note 10+ 上使用 camerax 版本 1.1.0-alpha01 时遇到了同样的问题。尝试降级到 1.0.0-beta11。

更改相机选择器。 看看 link.

lensFacing = when {
            hasBackCamera() -> CameraSelector.LENS_FACING_BACK
            hasFrontCamera() -> CameraSelector.LENS_FACING_FRONT
            else -> throw IllegalStateException("Back and front camera are unavailable")
        }
val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()

并像这样更改版本:

def camerax_version = "1.0.0-rc01"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation 'androidx.camera:camera-view:1.0.0-alpha20'
implementation 'androidx.camera:camera-extensions:1.0.0-alpha20'

更新:(24/03/2021 - dd/MM/yyyy)

该错误已在 CameraX 1.0.0-rc04 and 1.0.0-alpha23 中修复。 只需将依赖项更新到此版本或更高版本


更新:(22/03/2021 - dd/MM/yyyy)

应该很快会发布一个新的 CameraX 版本来解决这个问题,同时您可以应用此解决方法:

// CameraX core library
def camerax_version = '1.0.0-rc03'
implementation("androidx.camera:camera-core") {
    version {
        strictly "$camerax_version"
    }
    because "Force use 1.0.0-rc03"
}

// CameraX Camera2 extensions
implementation("androidx.camera:camera-camera2") {
    version {
        strictly "$camerax_version"
    }
    because "Force use 1.0.0-rc03"
}

// CameraX Lifecycle library
implementation("androidx.camera:camera-lifecycle") {
    version {
        strictly "$camerax_version"
    }
    because "Force use 1.0.0-rc03"
}
implementation "androidx.camera:camera-extensions:1.0.0-alpha22"
implementation 'androidx.camera:camera-view:1.0.0-alpha22'

如所述here


目前 CameraX 中存在影响(至少)Samsung S20+ 和 Galaxy Note 10+ 的错误

这里是link更新的问题:https://issuetracker.google.com/issues/181599852