Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flashlight Camera2 API

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

like image 459
user3579059 Avatar asked Oct 24 '15 23:10

user3579059


People also ask

What is Camera2 API?

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.

How do I know if my phone has Camera2 API?

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.


1 Answers

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();
        }
}
like image 191
Vinay Avatar answered Sep 28 '22 07:09

Vinay