Can I use camera preview and flashlight at the same time in Android Camera2 API?
When I try use CameraManager.setTorchMode(String cameraId, boolean enabled)
it's work fine when camera is not opened. But when Camera is open and I try setTorchMode
I receive this exception:
CameraService: setTorchMode: torch mode of camera 0 is not available because camera is in use
Camera2 is the latest low-level Android camera package and replaces the deprecated Camera class. Camera2 provides in-depth controls for complex use cases, but requires you to manage device-specific configurations. You can read about specific Camera2 classes and functions in the reference documentation.
Under each Camera ID, you can find a sub-category of various features and their support details. We are particularly interested in the category named “Hardware Support Level” which shows the Camera2 API support level on the device.
Setting both FLASH_MODE and AE_MODE is important. Below is the working piece of code:
public void toggleFlashMode(boolean enable){
try {
if (mCameraType.equals(CameraType.BACK)) {
if (enable) {
mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
} else {
mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF);
}
mCaptureSession.setRepeatingRequest(mPreviewRequestBuilder.build(), null, null);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With