基于CameraX BasicsIntroduction to CAmeraX教程,我成功创建了一个活动,该活动在该活动中打开Camera。我想通过单击按钮在正面和背面之间切换相机。我使用以下代码来做到这一点:

@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拍照,所有这些工作正常。我也在多种设备上进行了测试。所有这些设备中都存在相同的错误。

最佳答案

几天前,我遇到了同样的问题,存在similar issue,但是建议的解决方案对我不起作用。起作用的是稍微修改了official sample中的方法bindCameraUseCases()(在类似的问题中已链接到该方法)。从startCamera()呼叫onCreate()

private fun startCamera() {
    texture.post { bindCameraUseCases() }
    //btn_take_picture.setOnClickListener { takePhoto() }
    btn_swap_camera.setOnClickListener {swapCamera()}
}

@SuppressLint("RestrictedApi")
private fun swapCamera() {
    lensFacing = when (lensFacing) {
        CameraX.LensFacing.BACK -> CameraX.LensFacing.FRONT
        CameraX.LensFacing.FRONT -> CameraX.LensFacing.BACK
        else -> CameraX.LensFacing.BACK
    }
    CameraX.getCameraWithLensFacing(lensFacing)
    bindCameraUseCases()
}

// Slightly modified code from the sample
private fun bindCameraUseCases() {
    CameraX.unbindAll()

    val metrics = DisplayMetrics().also { texture.display.getRealMetrics(it) }
    val screenAspectRatio = Rational(metrics.widthPixels, metrics.heightPixels)

    val viewFinderConfig = PreviewConfig.Builder().apply {
        setLensFacing(lensFacing)
        setTargetAspectRatio(screenAspectRatio)
        setTargetRotation(texture.display.rotation)
    }.build()

    preview = AutoFitPreviewBuilder.build(viewFinderConfig, texture) // See note below

    val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
        setLensFacing(lensFacing)
        setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
        setTargetAspectRatio(screenAspectRatio)
        setTargetRotation(texture.display.rotation)
    }.build()

    imageCapture = ImageCapture(imageCaptureConfig)

    CameraX.bindToLifecycle(this, preview, imageCapture)
}


请注意,类AutoFitPreviewBuilder来自样本,可以找到here
(示例是Google许可的Apache 2.0,因此用法应合理)

关于android - 如何在CameraX API中切换相机?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57985941/

10-09 07:23