Search code examples
androidandroid-camera2android-camerax

How to listen to the cameraX lens facing changes


Does camerax provide api for lens facing changes callback? After switching the lens facing camera I want to be notified when it has finished changing and the camera is ready to use.

Currently I'm using this dependencies of camerax

implementation "androidx.camera:camera-lifecycle:1.0.0-beta01"
implementation "androidx.camera:camera-view:1.0.0-alpha08"
implementation "androidx.camera:camera-extensions:1.0.0-alpha08"

Solution

  • Sounds like you need a signal for when the camera starts emitting frames. You can use Camera2Interop and set a CaptureCallback on the preview use case for example. After binding the preview use case using a CameraSelector for the lens facing you want, you can listen for when onCaptureCompleted() is invoked, this should give you a signal that the camera has started.

    val builder = Preview.Builder()
    Camera2Interop.Extender(builder).setSessionCaptureCallback(object: CameraCaptureSession.CaptureCallback() {
       override fun onCaptureCompleted(session: CameraCaptureSession, request: CaptureRequest, result: TotalCaptureResult) {
          // Camera will start emitting frames
       }
    })
    val preview = builder.build()