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

Skip to first unread message

Johan Swanberg

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

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();
            new Camera2Interop.Extender<>(builder)
                    .setCaptureRequestOption(CaptureRequest.CONTROL_AF_MODE, CameraMetadata.CONTROL_AF_MODE_OFF)
                    .setCaptureRequestOption(CaptureRequest.LENS_FOCUS_DISTANCE, 10f)
        Preview preview =;
        CameraSelector cameraSelector = new CameraSelector.Builder()
        ImageAnalysis imageAnalysis = new ImageAnalysis.Builder()
        ImageCapture.Builder imageBuilder = new ImageCapture.Builder();
        final ImageCapture imageCapture = imageBuilder
        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)

I expected the setCaptureRequestOptions to override the manual focus, since the API documentation ( says:

"Any values which are in conflict with values already set by CameraX, such as by, 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:

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$$ExternalSyntheticLambda2@c279e2b (Ask Gemini)
                                                                                                    java.lang.IllegalArgumentException: Option values conflicts:, 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.
Reply all
Reply to author
Message has been deleted
0 new messages