Android CameraX 检索摄像机配置文件参数时出错
Posted
技术标签:
【中文标题】Android CameraX 检索摄像机配置文件参数时出错【英文标题】:Android CameraX Error retrieving camcorder profile params 【发布时间】:2021-06-09 05:27:11 【问题描述】:我正在迁移到 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))
【问题讨论】:
我已将提供的解决方法添加到我的答案中,试试看它是否适合您 【参考方案1】:更新:(24/03/2021 - dd/MM/yyyy)
该错误已在 CameraX 1.0.0-rc04 和 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 中存在影响(至少)三星 S20+ 和 Galaxy Note 10+ 的错误
这里是更新的问题链接:https://issuetracker.google.com/issues/181599852
【讨论】:
有趣的是这两款设备都没有按照这个来源进行测试developer.android.com/training/camerax/devices【参考方案2】:我在三星 s20+ 和 note 10+ 上使用 camerax 版本 1.1.0-alpha01 时遇到了同样的问题。尝试降级到 1.0.0-beta11。
【讨论】:
我在使用 CameraX 1.0.0-rc03 的 Note 10+ 上遇到了同样的崩溃(尚未尝试 1.0.0-beta11) 1.0.0-beta11 是我的解决方法的最新稳定版本,所以我会坚持一段时间【参考方案3】:更改相机选择器。 看看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'
【讨论】:
以上是关于Android CameraX 检索摄像机配置文件参数时出错的主要内容,如果未能解决你的问题,请参考以下文章
Android 使用CameraX实现预览/拍照/录制视频/图片分析/对焦/缩放/切换摄像头等操作
Android CameraX 仿一甜相机(录像拍照可调节尺寸聚焦照明网格线),最全的CameraX教程