How to Turn on Front Flash Light Programmatically in Android

How to turn on front flash light programmatically in Android?

For 2021, with CameraX, it is now dead easy: https://stackoverflow.com/a/66585201/294884


For this problem you should:

  1. Check whether the flashlight is
    available or not?

  2. If so then Turn Off/On

  3. If not then you can do whatever, according to your app
    needs.

For Checking availability of flash in the device:

You can use the following:

 context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

which will return true if a flash is available, false if not.

See:
http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.

For turning on/off flashlight:

I googled out and got this about android.permission.FLASHLIGHT. Android manifests' permission looks promising:

 <!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="@string/permlab_flashlight"
android:description="@string/permdesc_flashlight" />

Then make use of Camera and set Camera.Parameters. The main parameter used here is FLASH_MODE_TORCH.

eg.

Code Snippet to turn on camera flashlight.

Camera cam = Camera.open();     
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

Code snippet to turn off camera led light.

  cam.stopPreview();
cam.release();

I just found a project that uses this permission. Check quick-settings' src code. here http://code.google.com/p/quick-settings/ (Note: This link is now broken)

For Flashlight directly look http://code.google.com/p/quick-settings/source/browse/trunk/quick-settings/#quick-settings/src/com/bwx/bequick/flashlight (Note: This link is now broken)

Update6
You could also try to add a SurfaceView as described in this answer LED flashlight on Galaxy Nexus controllable by what API?
This seems to be a solution that works on many phones.

Update 5 Major Update

I have found an alternative Link (for the broken links above): http://www.java2s.com/Open-Source/Android/Tools/quick-settings/com.bwx.bequick.flashlight.htm You can now use this link. [Update: 14/9/2012 This link is now broken]

Update 1

Another OpenSource Code :
http://code.google.com/p/torch/source/browse/

Update 2

Example showing how to enable the LED on a Motorola Droid: http://code.google.com/p/droidled/

Another Open Source Code :

http://code.google.com/p/covedesigndev/

http://code.google.com/p/search-light/

Update 3 (Widget for turning on/off camera led)

If you want to develop a widget that turns on/off your camera led, then you must refer my answer Widget for turning on/off camera flashlight in android.

Update 4

If you want to set the intensity of light emerging from camera LED you can refer Can I change the LED intensity of an Android device? full post. Note that only rooted HTC devices support this feature.

** Issues:**

There are also some problems while turning On/Off flashlight. eg. for the devices not having FLASH_MODE_TORCH or even if it has, then flashlight does not turn ON etc.

Typically Samsung creates a lot of problems.

You can refer to problems in the given below list:

Use camera flashlight in Android

Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab

Android Turn on/off Camera Flash Programmatically with Camera2

I'll be addressing you to the Android Dev. documentation about the CameraManager since more code will be required but this is the basic code to activate the Flash on the camera in API's above 21.

CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
camManager.setTorchMode(cameraId, true);

Android CameraManager documentation.

Android.hardware.camera2 documentation.

very important thing to remember that you will need to use try/catch to discard possible errors and ofcourse check that no other higher priority application is using the camera at the moment.

How to programmatically turn torch-flash light on

use the following

 context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

to see if a flash is available or not. It will will return true or false.

More here on how to actually implement the code.

How to turn on the Android Flashlight

Use camera flashlight in Android

How to turn on camera flash light programmatically in Android?

How to turn ON and OFF Device flash light?

Bad news:
According to documentation android camera needs intialized surface view to start preview
Good news:
it must not be full screen or visible

My expirience so far that starting preview is necessary to activate flash ( but I can not speak for all devices )

If you need some examples how to activate preview and possibly hide it behind overlay,
look into javaocr library (see demos - there are 2 android apps, and also camera management library in separate project - you can just grab it)

https://sourceforge.net/projects/javaocr/

How to keep the flashlight on (programmatically) while the camera preview is ON

Try this while previewing on SurfaceView

params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(params);

Remember that If you want to use it as flashlight you can do:

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

If not, to turn flash on which will come out when you take the picture, you use:

parameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);

Happy Coding!

Front Flash light in Android

I don't think there is such thing as "Front Flash Light". The only thing I can think of at this stage is the Notification LED light at the front. Even looking at my Samsung S6 Edge device, I cannot find any front flash light on my camera.



The following code only applies to the rear flash light.
I believe there are three things you will need to do.

1 - Check to see if there is availability for flash on the device

2 - Set the corresponding permission in your manifest

3 - Implement the code to operate the flash functions

This code will return a boolean based on the availability for flash feature

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

Next are your permissions in the manifest file

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

The first permission is for the camera, second permission is for the flashlight where you don't have to activate the camera hardware

And finally, the code to turn on the flash

Camera myCamera = Camera.open();     
Parameters myParameters = myCamera .getParameters();
myParameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
myCamera.setParameters(p);
myCamera.startPreview();

To turn the service off

myCamera.stopPreview();
myCamera.release();

Additional function to test

  CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

try {
String cameraId = cameraManager.getCameraIdList()[0];
cameraManager.setTorchMode(cameraId, true);
flashLightStatus = true;
imageFlashlight.setImageResource(R.drawable.btn_switch_on);
} catch (CameraAccessException e) {
}

This may assist in getting the front camera accessibility from getCameraIdList function. Usually, the front camera is at the first position.



Related Topics



Leave a reply



Submit