0

I am currently working on a school project that requires me to turn on the LED (flashlight) on my android phone and take a photo with the front facing camera simultaneously.

How can I do this?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65

1 Answers1

0

If your device is below API 21 (Lollipop), or has legacy camera, you are probably out of luck, because the old hardware.Camera API requires to open the camera to operate the flashlight. But the flash belongs to rear facing camera, and you cannot usually open two cameras.

With the new camera2 API you can try this function:

private void FlashLightOn() {
    CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        String cameraId = 
        camManager.setTorchMode(camManager.getCameraIdList()[0];, true);
    } catch (Exception e) {}
}

This function needs the camera manager, but does not need the rear-facing camera, therefore, quite likely, using it you can still open the front-facing camera.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307