在仿真器上进行测试时,使用CameraX.LensFacing.FRONT
时会发生此错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: meter.meter, PID: 15289
java.lang.RuntimeException: Unable to start activity ComponentInfo{meter.meter/meter.meter.CameraActivity}: java.lang.IllegalArgumentException: Suggested resolution map missing resolution for camera 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalArgumentException: Suggested resolution map missing resolution for camera 1
at androidx.camera.core.Preview.onSuggestedResolutionUpdated(Preview.java:310)
at androidx.camera.core.UseCase.updateSuggestedResolution(UseCase.java:375)
at androidx.camera.core.CameraX.calculateSuggestedResolutions(CameraX.java:458)
at androidx.camera.core.CameraX.bindToLifecycle(CameraX.java:144)
at meter.meter.CameraActivity.onCreate(CameraActivity.java:68)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
代码:
PreviewConfig config = new PreviewConfig.Builder().setLensFacing(CameraX.LensFacing.FRONT).build();
Preview preview = new Preview(config);
TextureView textureView = findViewById(R.id.textureView);
preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {
@Override
public void onUpdated(Preview.PreviewOutput output) {
textureView.setSurfaceTexture(output.getSurfaceTexture());
}
});
ImageCaptureConfig imageCaptureConfig = new ImageCaptureConfig.Builder().build();
imageCapture = new ImageCapture(imageCaptureConfig);
CameraX.bindToLifecycle((LifecycleOwner) this,imageCapture, preview);
答案 0 :(得分:4)
将setLensFacing(CameraX.LensFacing.FRONT)
添加到ImageCaptureConfig
应该可以解决您的问题
答案 1 :(得分:2)
作为Nestos回答的补充,我必须在ImageCaptureConfig和PreviewConfig中都添加setLensFacing(CameraX.LensFacing.FRONT)
才能使这项工作生效。
答案 2 :(得分:1)
您应为传递给的setLensFacing(..)
的每个配置调用UseCase
CameraX.bindToLifecycle((LifecycleOwner) this, imageCapture, preview)
答案 3 :(得分:0)
PreviewConfig ImageCaptureConfig ImageAnalysisConfig 所有需要添加的
setLensFacing(CameraX.LensFacing.FRONT)
这就是我设置的方式
答案 4 :(得分:0)
您需要将lensFacing ArrayList
添加到以下区域:
1. PreviewConfig
2. ImageCaptureConfig
3. ImageAnalysisConfig
那应该做到的。