Starting camera preview with manual focus, then changing to automatic focus

89 views
Skip to first unread message

Johan Swanberg

unread,
May 3, 2024, 9:28:29 AMMay 3
to Android CameraX Discussion Group
Hi

I'm having issues changing the focus of the camera from manual to automatic. From what I understand in the API, it should be possible to change the cameraX focus after it has been set using the Camera2CameraControl. If there's another intended way to perform this with CameraX, or I have misunderstood how to approach this problem, please correct me.

I'm using CameraX 1.3.3. I've tested the code on a Samsung s21.

I have attached a small android project where i try to show the issue with minimal code. But here's the relevant code snippets to my problem:
    private Camera mCamera;
    ...

    // Starting the camera
    @OptIn(markerClass = ExperimentalCamera2Interop.class)
    void bindPreview(@NonNull ProcessCameraProvider cameraProvider) {
        Preview.Builder builder = new Preview.Builder();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            new Camera2Interop.Extender<>(builder)
                    .setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF)
                    .setCaptureRequestOption(CaptureRequest.LENS_FOCUS_DISTANCE, 10f)
        }
        Preview preview = builder.build();
        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();
        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
                .build();
        ImageCapture.Builder imageBuilder = new ImageCapture.Builder();
        final ImageCapture imageCapture = imageBuilder
                .setTargetRotation(this.getWindowManager().getDefaultDisplay().getRotation())
                .build();
        preview.setSurfaceProvider(mPreviewView.getSurfaceProvider());
        mCamera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageAnalysis, imageCapture);
    }



    // To test, I run the enableAutofocus() method on a button press, while the camera is open:
    @OptIn(markerClass = ExperimentalCamera2Interop.class)
    private void enableAutofocus() {
        Camera2CameraControl camera2CameraControl = Camera2CameraControl.from(mCamera.getCameraControl());
        CaptureRequestOptions options = new CaptureRequestOptions.Builder()
                .setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_MACRO)
                .build();
        camera2CameraControl.setCaptureRequestOptions(options);
    }
___________________________________________________________________________

I expected the setCaptureRequestOptions to override the manual focus, since the API documentation (https://developer.android.com/reference/androidx/camera/camera2/interop/Camera2CameraControl) says:

"Any values which are in conflict with values already set by CameraX, such as by androidx.camera.core.CameraControl, will overwrite the existing values. The values will be submitted with every repeating and single capture requests issued by CameraX, which may result in unexpected behavior depending on the values being applied."

I also saw that Scott Nien posted a comment in may 11 2021 which suggests that its possible to change settings without unbinding/binding the camera:
https://groups.google.com/a/android.com/g/camerax-developers/c/0I5rxKt4oV4

But instead I get the error:
2024-05-03 00:40:32.375 21630-21692 SequentialExecutor      com.ascom.myco.barcodescanner        E  Exception while executing runnable androidx.camera.camera2.internal.Camera2CameraControlImpl$$ExternalSyntheticLambda2@c279e2b (Ask Gemini)
                                                                                                    java.lang.IllegalArgumentException: Option values conflicts: camera2.captureRequest.option.android.control.afMode, existing value (ALWAYS_OVERRIDE)=0, conflicting (ALWAYS_OVERRIDE)=2



Apologies if this is better suited for stackoverflow, If this question should be moved there, please tell me.
CameraXFocusTest.zip
Reply all
Reply to author
Forward
Message has been deleted
0 new messages