简体   繁体   English

如何在 Camera X 库中以编程方式更改相机镜头朝向?

[英]how to change camera lens facing programmatically in Camera X library?

currently I am using this gradle目前我正在使用这个 gradle

implementation "androidx.camera:camera-camera2:1.0.0-beta11"
implementation "androidx.camera:camera-lifecycle:1.0.0-beta11"
implementation "androidx.camera:camera-view:1.0.0-alpha18"

so I want change the camera lens facing programmatically after the user tap a button.所以我想在用户点击按钮后以编程方式更改相机镜头。 here is the code that I try to use这是我尝试使用的代码

class CameraFragment : Fragment() {

    private var cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA

    lateinit var mContext: Context
    lateinit var mActivity: FragmentActivity

    override fun onAttach(context: Context) {
        super.onAttach(context)

        mContext = context
        activity?.let { mActivity = it }

    }

    private fun toggleFrontBackCamera() {


        if (cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA) {
            cameraSelector = CameraSelector.DEFAULT_FRONT_CAMERA
        } else if (cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA) {
            cameraSelector == CameraSelector.DEFAULT_BACK_CAMERA
        }

        startCamera()


    }

    private fun startCamera() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(mContext)

        cameraProviderFuture.addListener(Runnable {
            // Used to bind the lifecycle of cameras to the lifecycle owner
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

            // Preview
            val preview = Preview.Builder()
                    .build()
                    .also {
                        it.setSurfaceProvider(previewView.surfaceProvider)
                    }


            imageCapture = ImageCapture.Builder().build()


            try {
                // Unbind use cases before rebinding
                cameraProvider.unbindAll()

                // Bind use cases to camera
                cameraProvider.bindToLifecycle(
                        this, cameraSelector, preview, imageCapture)

            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }

        }, ContextCompat.getMainExecutor(mContext))
    }


}

I use that toggleFrontBackCamera() method to change the lens programmatically.我使用该toggleFrontBackCamera()方法以编程方式更改镜头。

as you can see, the default camera is back camera, and then when the user tap the button to change camera then toggleFrontBackCamera() method will be called for the first time, then the camera will change from Back to Front.如您所见,默认摄像头是后置摄像头,然后当用户点击按钮更改摄像头时,第一次调用toggleFrontBackCamera()方法,然后摄像头将从 Back 变为 Front。 no issue in here这里没有问题

but the problem is, when the user want to change the camera lens again, from front back to back camera, then toggleFrontBackCamera() will not work, it stuck in CameraSelector.DEFAULT_FRONT_CAMERA但问题是,当用户想再次更换相机镜头时,从前后相机,然后toggleFrontBackCamera()将不起作用,它卡在CameraSelector.DEFAULT_FRONT_CAMERA

so how to change it programmatically ?那么如何以编程方式更改它?

Initialize a variable in the beginning:在开始时初始化一个变量:

private var lensFacing = CameraSelector.LENS_FACING_BACK

Change your camera toggle method to this:将您的相机切换方法更改为:

private fun toggleFrontBackCamera() {
    lensFacing = if (CameraSelector.LENS_FACING_FRONT == lensFacing)
        CameraSelector.LENS_FACING_BACK
    else 
        CameraSelector.LENS_FACING_FRONT
    startCamera()
}

And finally create an instance of CameraSelector in your startCamera() and use this in cameraProvider.bindToLifecycle() :最后创建的实例CameraSelectorstartCamera()并在使用此cameraProvider.bindToLifecycle()

val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM