Java – torch / flashlight application (Android. Hardware. Camera is not recommended)
I have a torch / flashlight application that uses this code
open
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
turn off
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
But now
Any help on how to turn on / off the device flashlight using Camera2?
Thank you for your help
resolvent:
First, the deprecated android.hardware.camera API still works. If you need to support Android versions earlier than 5.0 lollipop, you still need to use it
The simplest option for the latest Android Version (Android marshmallow or later) is the new direct flashlight control: cameramanager.settorchmode
It is very simple to use and does not require a camera license
So I suggest the following:
Before API 23, use the deprecated camera API and your existing methods (don't forget to set the preview display, virtual surfacetexture is the simplest). You need camera permission and runtime permission request processing
API 23 or later, using settorchmode calls, you don't even need to ask for any specific runtime permissions