Android CameraX – manually change exposure compensation?

Tags: , , ,



I’m using CameraX for my project. I have created preview useCase and capture useCase.

final CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(lensFacing).build();

        previewBuilder = new Preview.Builder()
            .setTargetResolution(targetOutputSize)
            .setTargetRotation(rotation);

        preview = previewBuilder.build();

        final ImageCapture.Builder imageCaptureBuilder = new ImageCapture.Builder()
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
            .setTargetResolution(targetOutputSize)
            .setTargetRotation(rotation);

        imageCapture = imageCaptureBuilder.build();

Everything works fine. Now, I need to add functionality to manually change exposure compensation, but I can’t find any reference in official documentation or anywhere else how to do this. Is it possible with CameraX, or I need to switch to Camera2 API?

Please, any help is welcome.

Answer

There is new version of CameraX library.

def camerax_version = '1.0.0-beta09'

Firstly add those dependencies to gradle file.

// CameraX core library
implementation "androidx.camera:camera-core:$camerax_version"

// CameraX Camera2 extensions
implementation "androidx.camera:camera-camera2:$camerax_version"

This version supports Exposure compensation which can be adjusted in runtime. First create preview and takePicure use cases, and then bind those use cases to cameraProvider.

 camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture);

Now we have camera instance and it can be used to retrive CameraInfo, and from CameraInfo, we retrive ExposureState.

ExposureState exposureState = camera.getCameraInfo().getExposureState();

We can use exposureState to check if exposure compensation is supported on device

if (!exposureState.isExposureCompensationSupported()) return;

Next we need to get ExposureCompensation Range

Range<Integer> range = exposureState.getExposureCompensationRange();

Finally, we can set ExposureCompensation index in following way

 if (range.contains(index))
     camera.getCameraControl().setExposureCompensationIndex(index);


Source: stackoverflow