Seok Jeong Song
Seok Jeong Song

Reputation: 21

How to control camera flash light with camera2 API

I want to control flash light of smartphone (Galaxy s6) using camera2 API.

I checked it is available by codes like this,

try {
    CameraManager mManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    String [] cameraId = mManager.getCameraIdList();
    CameraCharacteristics cameraCharacteristics = mManager.getCameraCharacteristics(cameraId[1]);
    Toast.makeText(getApplicationContext(),cameraId[0]+cameraId[1],Toast.LENGTH_LONG).show();
    boolean flashAvailable = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
    if (flashAvailable) {
        mManager.openCamera(cameraId[0], new MyStateCallback(), null);
        Toast.makeText(getApplicationContext(),"Flash is available",Toast.LENGTH_LONG).show();
    } else {
        //todo: throw Exception
        Toast.makeText(getApplicationContext(),"Flash is not available",Toast.LENGTH_LONG).show();
    }
} catch (Exception e) {
    e.printStackTrace();
}

But, Toast message : Flash is not available.

Of course, I declared permission and hardware features in manifest.

Is it impossible to control flash light with this device or camera2 API?

Upvotes: 1

Views: 1165

Answers (1)

Eddy Talvala
Eddy Talvala

Reputation: 18137

You're checking whether the second camera (probably the front-facing one) has a flash, because you're getting the camera characteristics for cameraId[1] instead of cameraId[0] on the 4th line.

Typically, the back-facing camera is first, and is the one with the flash. To be most robust, iterate over all the camera IDs and check if any of them has a flash.

Upvotes: 2

Related Questions