Android CameraX 用例绑定失败
Android CameraX use case binding failed
设备:华为P40 Lite E
依赖项:
//CameraX dependencies
def camerax_version = "1.1.0-alpha03"
//ML kit for computer vision, needed for face detection
//bundled with app
implementation 'com.google.mlkit:face-detection:16.0.6'
//CameraX core library using camera implementation
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle Library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha23"
堆栈跟踪:
E/CameraManager: Use case binding failed
java.lang.IllegalArgumentException: No supported surface combination is found for camera device - Id : 0. May be attempting to bind too many use cases. Existing surfaces: [] New configs: [androidx.camera.core.impl.PreviewConfig@eec1a35, androidx.camera.core.impl.ImageAnalysisConfig@4817eca, androidx.camera.core.impl.ImageCaptureConfig@a00b83b]
at androidx.camera.lifecycle.LifecycleCameraRepository.bindToLifecycleCamera(LifecycleCameraRepository.java:278)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:428)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:302)
at nl.innovalor.ocr.vizcapture.camerax.VIZCameraXManager.bindCameraUseCases(VIZCameraXManager.java:593)
at -.lambda$startCamera$CameraManager(CameraManager.java:337)
at -.-$$Lambda$CameraManagerPt5AM_BPyOCEft3HHixQCAePBk.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
代码:
// Unbind use cases before rebinding
cameraProvider.unbindAll();
//preview
Preview preview = new Preview.Builder()
.setTargetResolution(new Size(previewWidth, previewHeight))
.setTargetRotation(ROTATION_0) //ROTATION_90 // //rotation
.build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
//viewport defines the buffer rect visible to end users
final ViewPort viewPort = new ViewPort.Builder(new Rational(previewWidth, previewHeight), ROTATION_0).build(); //rotation
//picture callback
imageCapture = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.setBufferFormat(ImageFormat.YUV_420_888)
.setTargetResolution(new Size(pictureWidth, pictureHeight))
.setTargetRotation(ROTATION_0) //rotation
.build();
//face detection analysis
imageAnalysis = new ImageAnalysis.Builder()
//ensure that backpressure strategy is set to its default strategy for real-time analysis
.setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST)
.setTargetResolution(new Size(previewWidth, previewHeight))
.setTargetRotation(ROTATION_0) //rotation
.build();
setFaceDetector(imageAnalysis, 0.05f); //0.1f
//use UseCaseGroup to assign viewport to use cases
UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
.addUseCase(preview)
.addUseCase(imageAnalysis)
.addUseCase(imageCapture)
.setViewPort(viewPort)
.build();
this.camera = cameraProvider.bindToLifecycle((LifecycleOwner) activity, cameraSelector, useCaseGroup); //preview, imageAnalysis, imageCapture
^^^^^^ fails here on this line ^^^^^^^
特别是仅在华为 phone 上失败,而例如OnePlus 5T、Pixel 3、Galaxy phones,我没有这个问题。
并非所有相机设备都支持 YUV 中的同步 ImageCapture 和 YUV 中的 ImageAnalysis;如果将 ImageCapture 切换为 JPEG,则在所有设备上都应该没问题(如果需要详细信息,可以查看底层 camera2 API 的会话创建中的 tables)。如果其中之一是 JPEG,则 LIMITED 级别的相机设备仅支持 3 个输出。不过,完整级别的设备应该支持它。
这就是 setBufferFormat 不是 CameraX 的一部分的原因 public API - 使用它很容易让您脱离相机设备通常支持的输出集。
设备:华为P40 Lite E 依赖项:
//CameraX dependencies
def camerax_version = "1.1.0-alpha03"
//ML kit for computer vision, needed for face detection
//bundled with app
implementation 'com.google.mlkit:face-detection:16.0.6'
//CameraX core library using camera implementation
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle Library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha23"
堆栈跟踪:
E/CameraManager: Use case binding failed
java.lang.IllegalArgumentException: No supported surface combination is found for camera device - Id : 0. May be attempting to bind too many use cases. Existing surfaces: [] New configs: [androidx.camera.core.impl.PreviewConfig@eec1a35, androidx.camera.core.impl.ImageAnalysisConfig@4817eca, androidx.camera.core.impl.ImageCaptureConfig@a00b83b]
at androidx.camera.lifecycle.LifecycleCameraRepository.bindToLifecycleCamera(LifecycleCameraRepository.java:278)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:428)
at androidx.camera.lifecycle.ProcessCameraProvider.bindToLifecycle(ProcessCameraProvider.java:302)
at nl.innovalor.ocr.vizcapture.camerax.VIZCameraXManager.bindCameraUseCases(VIZCameraXManager.java:593)
at -.lambda$startCamera$CameraManager(CameraManager.java:337)
at -.-$$Lambda$CameraManagerPt5AM_BPyOCEft3HHixQCAePBk.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:900)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8349)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1055)
代码:
// Unbind use cases before rebinding
cameraProvider.unbindAll();
//preview
Preview preview = new Preview.Builder()
.setTargetResolution(new Size(previewWidth, previewHeight))
.setTargetRotation(ROTATION_0) //ROTATION_90 // //rotation
.build();
preview.setSurfaceProvider(previewView.getSurfaceProvider());
//viewport defines the buffer rect visible to end users
final ViewPort viewPort = new ViewPort.Builder(new Rational(previewWidth, previewHeight), ROTATION_0).build(); //rotation
//picture callback
imageCapture = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.setBufferFormat(ImageFormat.YUV_420_888)
.setTargetResolution(new Size(pictureWidth, pictureHeight))
.setTargetRotation(ROTATION_0) //rotation
.build();
//face detection analysis
imageAnalysis = new ImageAnalysis.Builder()
//ensure that backpressure strategy is set to its default strategy for real-time analysis
.setBackpressureStrategy(STRATEGY_KEEP_ONLY_LATEST)
.setTargetResolution(new Size(previewWidth, previewHeight))
.setTargetRotation(ROTATION_0) //rotation
.build();
setFaceDetector(imageAnalysis, 0.05f); //0.1f
//use UseCaseGroup to assign viewport to use cases
UseCaseGroup useCaseGroup = new UseCaseGroup.Builder()
.addUseCase(preview)
.addUseCase(imageAnalysis)
.addUseCase(imageCapture)
.setViewPort(viewPort)
.build();
this.camera = cameraProvider.bindToLifecycle((LifecycleOwner) activity, cameraSelector, useCaseGroup); //preview, imageAnalysis, imageCapture
^^^^^^ fails here on this line ^^^^^^^
特别是仅在华为 phone 上失败,而例如OnePlus 5T、Pixel 3、Galaxy phones,我没有这个问题。
并非所有相机设备都支持 YUV 中的同步 ImageCapture 和 YUV 中的 ImageAnalysis;如果将 ImageCapture 切换为 JPEG,则在所有设备上都应该没问题(如果需要详细信息,可以查看底层 camera2 API 的会话创建中的 tables)。如果其中之一是 JPEG,则 LIMITED 级别的相机设备仅支持 3 个输出。不过,完整级别的设备应该支持它。
这就是 setBufferFormat 不是 CameraX 的一部分的原因 public API - 使用它很容易让您脱离相机设备通常支持的输出集。