基于CameraX 基础知识和CAmeraX教程简介,我成功创建了一个在该 Activity 中打开 Camera 的 Activity。我想通过单击按钮在前后切换相机。我使用以下代码来做到这一点:
@Override
protected void onCreate(Bundle savedInstanceState)
{
...
cameraOrientation = CameraX.LensFacing.BACK;
StartCamera();
switchCamera.setOnClickListener(new View.OnClickListener()
{
@SuppressLint("RestrictedApi")
@Override
public void onClick(View view)
{
CameraX.unbindAll();
cameraOrientation = (cameraOrientation == CameraX.LensFacing.BACK ? CameraX.LensFacing.FRONT : CameraX.LensFacing.BACK);
try {
StartCamera();
} catch (Exception e) {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show(); }
}
});
}
private void StartCamera()
{
previewConfig = new PreviewConfig.Builder()
.setLensFacing(cameraOrientation)
.build();
preview = new Preview(previewConfig);
preview.setOnPreviewOutputUpdateListener(
new Preview.OnPreviewOutputUpdateListener()
{
@Override
public void onUpdated(Preview.PreviewOutput previewOutput)
{
textureView.setSurfaceTexture(previewOutput.getSurfaceTexture());
}
});
imageCaptureConfig = new ImageCaptureConfig.Builder()
.setLensFacing(cameraOrientation)
.setFlashMode(flashMode)
.build();
imageCapture = new ImageCapture(imageCaptureConfig);
CameraX.bindToLifecycle(SampleCameraActivity.this, imageCapture, preview);
}
但是,当我单击按钮时,应用程序会卡住(但不会崩溃),并且此错误会反复显示在日志中:
E/GLConsumer: [SurfaceTexture-0–6996–1] updateAndRelease: GLConsumer is not attached to an OpenGL ES context
我还使用此 api 切换闪光灯模式并拍照,所有这些都可以正常工作。我也在多种设备上进行了测试。所有这些设备中的相同错误。