Search code examples
androidandroid-cameraandroid-camerax

Android CameraX: how to get AF status associated with an analysis ImageProxy


I'm new to CameraX, coming from Camera2

With Camera2, when CameraCaptureSession.CaptureCallback.onCaptureCompleted is called, we get an instance of TotalCaptureResult which contains many useful information about the image, for instance: final Integer afState = result.get(TotalCaptureResult.CONTROL_AF_STATE);

Now, I'm starting to use CameraX, with an ImageAnalysis UseCase.

In that case, I receive the image through

void analyze(@NonNull ImageProxy image);

But then, how could I get the autofocus status associated with the image?

Thanks a lot

I searched CameraX source code without finding an answer.

But for a use case such analyze, that seems to me very important to know if the image is in focus.


Solution

  • In case this helps someone else, I found a solution to my question.

    In summary, use Camera2Interop.Extender(builder)

    For details, at the place where I was using ImageAnalysis.Builder(), I modify it with an implementation of CameraCaptureSession.CaptureCallback which belongs to Camera2, back into the known world where you can get metadata about each image like this:

    val result: CaptureResult
    val afState result.get(CaptureResult.CONTROL_AF_STATE)
    

    Here is how I get the CaptureResult:

    val builder = ImageAnalysis.Builder()  
    Camera2Interop.Extender(builder).setSessionCaptureCallback(captureCallback)
    val analysisUseCase = builder.build()
    

    where captureCallback implements CameraCaptureSession.CaptureCallback

    Then you just have to use timestamp to map capture metadata with each image:

    • imageProxy.imageInfo.timestamp that you get from your implementation of
    ImageAnalysis.Analyzer.analyze(ImageProxy imageProxy) 
    
    • result.get(CaptureResult.SENSOR_TIMESTAMP) that you get from your implementation of
    CameraCaptureSession.CaptureCallback.onCaptureCompleted(
            session: CameraCaptureSession,
            request: CaptureRequest,
            result: TotalCaptureResult
    )
    

    But be careful with the fact that onCaptureCompleted is not necessarily called for each image that is provided to ImageAnalysis.Analyzer.analyze.