Shaon
Shaon

Reputation: 2724

Working with CameraCaptureSession.CaptureCallback() in CameraX

In-Camera 1, we have FaceDetectionListener and camera.startFaceDetection() method. Using this way it is easier to find faces.

In-camera 2, we can do the same using CameraCaptureSession.CaptureCallback() method and this static variable

Integer mode = result.get(CaptureResult.STATISTICS_FACE_DETECT_MODE);
Face[] faces = result.get(CaptureResult.STATISTICS_FACES);

Now there is a new Camera library called CameraX. It is a wrapper of Camera2 and recommended to use. If it is a wrapper of Camera 2, we can easily get the callback result of CameraCaptureSession.CaptureCallback()

But After 3 days of trying I have failed to find a solution.

Can anyone give me the solution to call the below method as camera2 does?

In Camera2,

 private val mCaptureCallback = object : CameraCaptureSession.CaptureCallback() {
    override fun onCaptureProgressed(
        session: CameraCaptureSession,
        request: CaptureRequest,
        partialResult: CaptureResult
    ) {
        

    }

    override fun onCaptureCompleted(
        session: CameraCaptureSession,
        request: CaptureRequest,
        result: TotalCaptureResult
    ) {
        
    }
}



mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback,
                mBackgroundHandler);

How to get the callback result using CameraX?

Upvotes: 6

Views: 1547

Answers (1)

Husayn Hakeem
Husayn Hakeem

Reputation: 4570

You can use CameraX's camera2 interop classes which provide an interoperability layer between CameraX and Camera2 APIs.

If you need to set a CaptureCallback on a use case (e.g. the Preview use case) in order to track the progress of its capture requests, you can do so as follows:

// Set up and configure the Preview's builder
val previewBuilder = Preview.Builder()

// Create the callback you want to attach to the Preview use case
val captureCallback = object : CameraCaptureSession.CaptureCallback() {
   ...            
}

// Create an Extender to attach Camera2 options
val previewExtender = Camera2Interop.Extender(previewBuilder)

// Attach the Camera2 CaptureCallback
previewExtender.setSessionCaptureCallback(captureCallback)

// Initialize the Preview use case
val preview =  previewBuilder.build()

// bind the Preview use case
cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview)

Upvotes: 3

Related Questions