I need to use only the flash with the API camera2 (Android 5, API level 21), like torch applications. But all the examples that I found requires the display of camera stream in a view
Asked
Active
Viewed 2.7k times
4 Answers
16
https://github.com/pinguo-yuyidong/Camera2/blob/master/app/src/main/java/us/yydcdut/androidltest/otheractivity/FlashActivity.java
here,you don't need the preview to open the flash.

yydcdut
- 801
- 6
- 9
15
Just use following class to turn on/off FlashLight in Android L,
public class FlashLightUtilForL {
private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
private CameraDevice mCameraDevice;
private CameraManager mCameraManager;
public FlashLightUtilForL(Context context) {
try {
mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
//here to judge if flash is available
CameraCharacteristics cameraCharacteristics = mCameraManager.getCameraCharacteristics("0");
boolean flashAvailable = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
if (flashAvailable) {
mCameraManager.openCamera("0", new MyCameraDeviceStateCallback(), null);
} else {
//todo: throw Exception
}
//mCameraManager.openCamera("0", new MyCameraDeviceStateCallback(), null);
} catch (Exception e) {
e.printStackTrace();
}
}
class MyCameraDeviceStateCallback extends CameraDevice.StateCallback {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
//get builder
try {
mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_MANUAL);
//flash on, default is on
mBuilder.set(CaptureRequest.CONTROL_AE_MODE, CameraMetadata.CONTROL_AF_MODE_AUTO);
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
List<Surface> list = new ArrayList<Surface>();
SurfaceTexture mSurfaceTexture = new SurfaceTexture(1);
Size size = getSmallestSize(mCameraDevice.getId());
mSurfaceTexture.setDefaultBufferSize(size.getWidth(), size.getHeight());
Surface mSurface = new Surface(mSurfaceTexture);
list.add(mSurface);
mBuilder.addTarget(mSurface);
camera.createCaptureSession(list, new MyCameraCaptureSessionStateCallback(), null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera) {
}
@Override
public void onError(CameraDevice camera, int error) {
}
}
private Size getSmallestSize(String cameraId) throws CameraAccessException {
Size[] outputSizes = mCameraManager.getCameraCharacteristics(cameraId)
.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(SurfaceTexture.class);
if (outputSizes == null || outputSizes.length == 0) {
throw new IllegalStateException(
"Camera " + cameraId + "doesn't support any outputSize.");
}
Size chosen = outputSizes[0];
for (Size s : outputSizes) {
if (chosen.getWidth() >= s.getWidth() && chosen.getHeight() >= s.getHeight()) {
chosen = s;
}
}
return chosen;
}
/**
* session callback
*/
class MyCameraCaptureSessionStateCallback extends CameraCaptureSession.StateCallback {
@Override
public void onConfigured(CameraCaptureSession session) {
mSession = session;
try {
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
}
}
public void turnOnFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOffFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
private void close() {
if (mCameraDevice == null || mSession == null) {
return;
}
mSession.close();
mCameraDevice.close();
mCameraDevice = null;
mSession = null;
}
}

Umang Kothari
- 3,674
- 27
- 36
-
1I try your source code with this : FlashLightUtilForL flash = new FlashLightUtilForL(getActivity().getBaseContext()); if(flashIsOn){ flash.turnOffFlashLight(); flashIsOn = false; }else{ flash.turnOnFlashLight(); flashIsOn = true; } But i can't turn if off . What's the solution please. – Aug 15 '15 at 20:43
-
Change context while making Object, like FlashLightUtilForL flash = new FlashLightUtilForL(this); It may help you. – Umang Kothari Aug 17 '15 at 06:30
-
I'm in a fragment and the problem is to turn it off, turn on is ok – Aug 17 '15 at 16:07
-
Change context while making Object, like FlashLightUtilForL flash = new FlashLightUtilForL(getActivity()); It may help you. – Umang Kothari Aug 17 '15 at 16:11
-
i made it but nothing change, the light turn on when i click but never turn off. i have to use the method close?and if yes how to do use it? Thanks for your help – Aug 17 '15 at 19:33
-
1Yes, I got your problem. First make close() method to public from private. And then make first call turnOffFlashLight() and after that call close() method. I hope it will work for you. – Umang Kothari Aug 18 '15 at 05:26
-
@UmangKothari i am calling turnOnFlashLight() from IntentService but its not turning on the flash. Below is my code: ` FlashLightUtilForL flashLight = new FlashLightUtilForL(context); flashLight.turnOnFlashLight(); ` – rupesh Sep 08 '15 at 09:58
-
http://stackoverflow.com/questions/32451163/mediaplayer-and-flash-light-in-android – rupesh Sep 08 '15 at 10:17
-
I don't know exactly but It need context of Activity. But please confirm. I am not sure. – Umang Kothari Sep 08 '15 at 13:14
-
@UmangKothari i have the same issue not able to turn off the flash. – rupesh Sep 11 '15 at 13:29
-
@UmangKothari i have tried above solution which you have mentioned in comment. close() method. – rupesh Sep 11 '15 at 13:30
-
If you release camera resources using close method then it will turn off flash light. – Umang Kothari Sep 11 '15 at 13:39
-
@MaximeVince did you get the solutions – rupesh Sep 15 '15 at 09:57
4
here is the complete demo check this.
package com.camera2;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Size;
import android.view.Surface;
import android.view.View;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
private CameraManager cameraManager;
private CameraCharacteristics cameraCharacteristics;
private CameraDevice mCameraDevice;
private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
private Button on;
private Button off;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
on = (Button) findViewById(R.id.on);
off = (Button) findViewById(R.id.off);
initCamera();
}
public void click(View v)
{
switch (v.getId())
{
case R.id.on:
try
{
turnOnFlashLight();
}
catch (Exception e)
{
e.printStackTrace();
}
break;
case R.id.off:
turnOffFlashLight();
break;
}
}
private void initCamera()
{
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try
{
String[] id = cameraManager.getCameraIdList();
if (id != null && id.length > 0)
{
cameraCharacteristics = cameraManager.getCameraCharacteristics(id[0]);
boolean isFlash = cameraCharacteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
if (isFlash)
{
cameraManager.openCamera(id[0], new MyCameraDeviceStateCallback(), null);
}
}
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
class MyCameraDeviceStateCallback extends CameraDevice.StateCallback
{
@Override
public void onOpened(CameraDevice camera)
{
mCameraDevice = camera;
// get builder
try
{
mBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
List<Surface> list = new ArrayList<Surface>();
SurfaceTexture mSurfaceTexture = new SurfaceTexture(1);
Size size = getSmallestSize(mCameraDevice.getId());
mSurfaceTexture.setDefaultBufferSize(size.getWidth(), size.getHeight());
Surface mSurface = new Surface(mSurfaceTexture);
list.add(mSurface);
mBuilder.addTarget(mSurface);
camera.createCaptureSession(list, new MyCameraCaptureSessionStateCallback(), null);
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
@Override
public void onDisconnected(CameraDevice camera)
{
}
@Override
public void onError(CameraDevice camera, int error)
{
}
}
private Size getSmallestSize(String cameraId) throws CameraAccessException
{
Size[] outputSizes = cameraManager.getCameraCharacteristics(cameraId).get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(SurfaceTexture.class);
if (outputSizes == null || outputSizes.length == 0)
{
throw new IllegalStateException("Camera " + cameraId + "doesn't support any outputSize.");
}
Size chosen = outputSizes[0];
for (Size s : outputSizes)
{
if (chosen.getWidth() >= s.getWidth() && chosen.getHeight() >= s.getHeight())
{
chosen = s;
}
}
return chosen;
}
class MyCameraCaptureSessionStateCallback extends CameraCaptureSession.StateCallback
{
@Override
public void onConfigured(CameraCaptureSession session)
{
mSession = session;
try
{
mSession.setRepeatingRequest(mBuilder.build(), null, null);
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session)
{
}
}
public void turnOnFlashLight()
{
try
{
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void turnOffFlashLight()
{
try
{
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void close()
{
if (mCameraDevice == null || mSession == null)
{
return;
}
mSession.close();
mCameraDevice.close();
mCameraDevice = null;
mSession = null;
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.camera2.MainActivity">
<Button
android:onClick="click"
android:id="@+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On" />
<Button
android:onClick="click"
android:id="@+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Off" />
</LinearLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.camera2">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
use close() to close camera settings

rizwan
- 71
- 6
0
Well, this worked perfectly for me:
public class MainActivity extends AppCompatActivity {
ToggleButton button;
boolean flashLightStatus = true;
boolean deviceHasCameraFlash = getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (ToggleButton) findViewById(R.id.button);
FlashLightOn();//When app opens, turn it on.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(deviceHasCameraFlash){
if(flashLightStatus){//when light on
FlashLightOff();//we should off
}
else //when light off
FlashLightOn();//we should on
}
else
Toast.makeText(MainActivity.this, "No flash available on your device.", Toast.LENGTH_SHORT).show();
}
});
}
private void FlashLightOn() {
CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try{
String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
camManager.setTorchMode(cameraId, true);
flashLightStatus=true;
} catch (Exception e){
}
}
private void FlashLightOff() {
CameraManager camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try{
String cameraId = camManager.getCameraIdList()[0]; // Usually front camera is at 0 position.
camManager.setTorchMode(cameraId, false);
flashLightStatus=false;
} catch (Exception e){
}
}
}