Solution 1 :
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);
Solution 2 :
Looks like there is a Camera2Interop
class under the CameraX API which allows you to internally change the values. This is the official documentation: Camera2Interop.Extender
Before building your ImageCapture, add this:
Camera2Interop.Extender(imageCaptureBuilder)
.setCaptureRequestOption(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, value);
Solution 3 :
In case you want to know when the camera completed changing the exposure settings, do something like (camerax_version = ‘1.0.0’):
lateinit var myCameraExposureListener: ListenableFuture<Int>
and then
myCameraExposureListener =
myCameraControl!!.setExposureCompensationIndex(exposureCompensationIndex);
myCameraExposureListener.addListener({
try {
Log.v(TAG, "Changing Exposure Settings Completed")
} catch (exception: Exception) {
Log.v(TAG, "Changing Exposure Settings Failed")
}
}, ContextCompat.getMainExecutor(context) )
Problem :
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.
Comments
Comment posted by Šemsudin Tafilović
It needs to be changeable during the use of camera. For example, on camera UI we need to have two buttons “+” and “-” and clicking on those buttons to change exposure compensation. Currently, this is not possible on CameraX, but I found that branch with this feature was merged into master on CameraX and I hope that it will be soon in release version.