Get CameraX final resolution

Tags: , ,



Just created a camera preview in Android with CameraX with the following configuration:

    PreviewConfig previewConfig = new PreviewConfig.Builder()
            .setLensFacing(CameraX.LensFacing.FRONT)
            .setTargetResolution(new Size(720, 720))
            .build();
    Preview preview = new Preview(previewConfig);

Now, the problem is that such target resolution may not be available, in which case the preview will choose a resolution closed to the requested one. What I’m asking here is a way to know which resolution has effectively be chosen by the preview.

Thanks in advance!

Answer

Actually it is possible. I’ve found out after a little bit of source code digging that after calling bindToLifecycle on your cameraProvider:

camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, previewUseCase, imageCaptureUseCase, analyzerUseCase)

You will be able to retrieve resolution selected for each applicable usecase:

val captureSize = imageCaptureUseCase.attachedSurfaceResolution ?: Size(0, 0)
val previewSize = previewUseCase.attachedSurfaceResolution ?: Size(0, 0)

Hope this helps



Source: stackoverflow