I use this code below to toggle on and off camera flash light
private void toggleFlash() {
try {
if(!flashOn) {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
} else {
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
}
captureSession.setRepeatingRequest(captureRequestBuilder.build(), null, handler);
flashOn = !flashOn;
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
It works, but the issue is this. At first when I open the camera (without flash light), I can see the image preview from the camera clear, but when I toggle on the flash and toggle it off, the preview becomes more darker than the initial time of opening the camera.
I'm still new to the camera API but I guess the issue is coming from here
captureRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
But I don't know the proper code to use to make the camera not to be dark. Anyone with a solution?
It's going black because you're turning off auto-exposure, and I suspect the default manual exposure and sensitivity values are so low that the image is very underexposed.
You don't need to turn auto-exposure OFF
when you turn off the flash. To control the flash manually, you do need to make sure AE is not set to ON_AUTO_FLASH
, ON_ALWAYS_FLASH
, or ON_AUTO_FLASH_REDEYE
, but that just means you can leave it as ON
all the time, if you don't need automatic flash firing.
So just remove the bit about setting CONTROL_AE_MODE
to CONTROL_AE_MODE_OFF
Torch and flash are two different setting in camera2. You can try and control flash with these setting.
On captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
Off captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.