使用CameraX初始化USB摄像头时冷启动缓慢问题求助
CameraX USB摄像头冷启动初始化缓慢问题(Orange Pi5)
我在Orange Pi5上开发基于USB摄像头的帧处理应用,使用CameraX框架时遇到冷启动初始化缓慢的问题。首次启动会多次重试初始化,最终在达到最大重试次数后完成初始化。
初始化代码
private fun startCameraPreview() { cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext()) cameraProviderFuture.addListener({ val cameraProvider = cameraProviderFuture.get() bindPreview(cameraProvider) }, ContextCompat.getMainExecutor(requireContext())) } @SuppressLint("RestrictedApi") private fun bindPreview(cameraProvider: ProcessCameraProvider) { previewView = viewBinding.previewView previewView.implementationMode = PreviewView.ImplementationMode.COMPATIBLE previewView.scaleX = -1f val preview: Preview = Preview.Builder() .setResolutionSelector( ResolutionSelector.Builder().setResolutionStrategy( ResolutionStrategy(Size(1080, 768), ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER) ).build()) .build() val selector = selectExternalOrBestCamera(cameraProvider) //For Usb camera Access preview.setSurfaceProvider(previewView.surfaceProvider) val strategy = ResolutionStrategy(Size(640, 480), ResolutionStrategy.FALLBACK_RULE_CLOSEST_HIGHER) val imageFrameAnalysis = ImageAnalysis.Builder() .setResolutionSelector(ResolutionSelector.Builder().setResolutionStrategy(strategy).build()) .setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST) .setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_RGBA_8888) .build() imageFrameAnalysis.setAnalyzer(Executors.newSingleThreadExecutor(), frameAnalyser) cameraProvider.bindToLifecycle(this as LifecycleOwner, selector!!, preview, imageFrameAnalysis) }
USB摄像头选择代码
fun selectExternalOrBestCamera(provider: ProcessCameraProvider): CameraSelector? { val cam2Infos = provider.availableCameraInfos.map { Camera2CameraInfo.from(it) }.sortedByDescending { // HARDWARE_LEVEL is Int type, with the order of: // LEGACY < LIMITED < FULL < LEVEL_3 < EXTERNAL it.getCameraCharacteristic(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL) } return when { cam2Infos.isNotEmpty() -> { CameraSelector.Builder() .addCameraFilter { it.filter { camInfo -> // cam2Infos[0] is either EXTERNAL or best built-in camera val thisCamId = Camera2CameraInfo.from(camInfo).cameraId thisCamId == cam2Infos[0].cameraId } }.build() } else -> null } }
首次启动错误日志
2024-03-05 15:58:14.010 14845-14906 CameraRepository D Added camera: 0 2024-03-05 15:58:14.012 14845-14906 Camera2CameraInfo I Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL 2024-03-05 15:58:14.012 14845-14906 CameraValidator D Verifying camera lens facing on rk3588s_s, lensFacingInteger: null 2024-03-05 15:58:14.013 14845-14906 CameraValidator E Camera LensFacing verification failed, existing cameras: [Camera@e0778e4[id=0]] 2024-03-05 15:58:14.014 14845-14906 CameraX W Retry init. Start time 9251708 current time 9253283 androidx.camera.core.impl.CameraValidator$CameraIdListIncorrectException: Expected camera missing from device. at androidx.camera.core.impl.CameraValidator.validateCameras(CameraValidator.java:97) at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:334) at androidx.camera.core.CameraX$$ExternalSyntheticLambda3.run(Unknown Source:10) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:920) Caused by: java.lang.IllegalArgumentException: No available camera can be found at androidx.camera.core.CameraSelector.select(CameraSelector.java:94) at androidx.camera.core.impl.CameraValidator.validateCameras(CameraValidator.java:83) at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:334) at androidx.camera.core.CameraX$$ExternalSyntheticLambda3.run(Unknown Source:10) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:920)
初始化完成提示
The device might underreport the amount of the cameras. Finish the initialize task since we are already reaching the maximum number of retries.
后续初始化日志
Added camera: 0 2024-03-05 15:58:15.030 14845-14906 Camera2CameraInfo I Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL 2024-03-05 15:58:15.031 14845-14906 CameraValidator D Verifying camera lens facing on rk3588s_s, lensFacingInteger: null 2024-03-05 15:58:15.032 14845-14906 CameraValidator E Camera LensFacing verification failed, existing cameras: [Camera@10741b2[id=0]] 2024-03-05 15:58:15.032 14845-14906 CameraX E The device might underreport the amount of the cameras. Finish the initialize task since we are already reaching the maximum number of retries. 2024-03-05 15:58:15.083 14845-14845 CameraOrientationUtil D getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=false, result=90 2024-03-05 15:58:15.085 14845-14845 CameraOrientationUtil D getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=false, result=90 2024-03-05 15:58:15.094 14845-14845 DynamicRangeResolver D Resolved dynamic range for use case androidx.camera.core.Preview-1b649f9e-f19f-43f5-8882-7ace2058df35 to no compatible HDR dynamic ranges. DynamicRange@d8ad747{encoding=UNSPECIFIED, bitDepth=0} -> DynamicRange@c2bc286{encoding=SDR, bitDepth=8} 2024-03-05 15:58:15.099 14845-14845 CameraOrientationUtil D getRelativeImageRotation: destRotationDegrees=0, sourceRotationDegrees=90, isOppositeFacing=false, result=90 2024-03-05 15:58:15.101 14845-14845 DeferrableSurface D Surface created[total_surfaces=1, used_surfaces=0](androidx.camera.core.processing.SurfaceEdge$SettableSurface@df18112} 2024-03-05 15:58:15.103 14845-14845 DeferrableSurface D Surface created[total_surfaces=2, used_surfaces=0](androidx.camera.core.SurfaceRequest$2@4e29c5e} 2024-03-05 15:58:15.104 14845-14845 DeferrableSurface D New surface in use[total_surfaces=2, used_surfaces=1](androidx.camera.core.SurfaceRequest$2@4e29c5e} 2024-03-05 15:58:15.104 14845-14845 DeferrableSurface D use count+1, useCount=1 androidx.camera.core.SurfaceRequest$2@4e29c5e
CameraX Gradle依赖
// CameraX def camerax_version = "1.3.1" 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-video:${camerax_version}" implementation "androidx.camera:camera-view:${camerax_version}" implementation "androidx.camera:camera-extensions:${camerax_version}"
已尝试方案
- 添加
@OptIn(ExperimentalCamera2Interop::class)注解,无效果 - 为CameraSelector明确指定镜头朝向
.requireLensFacing(LENS_FACING_FRONT/BACK/EXTERNAL),导致崩溃
问题推测
CameraValidator无法接受USB摄像头的镜头参数值,导致多次重试初始化,进而造成冷启动缓慢。
内容的提问来源于stack exchange,提问作者John DoeDoe
相关产品推荐
相关产品推荐

