Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CameraX final resolution

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!

like image 919
Fran Marzoa Avatar asked Sep 12 '19 13:09

Fran Marzoa


2 Answers

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

like image 57
Marcin Bak Avatar answered Sep 18 '22 16:09

Marcin Bak


Ok, I found myself a solution.

    preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() {
        @Override
        public void onUpdated(Preview.PreviewOutput output) {
             ...
             Size resolution = output.getTextureSize();
             ...
        }
    });
like image 24
Fran Marzoa Avatar answered Sep 22 '22 16:09

Fran Marzoa