Torch Light based on Camera API 2

Torch Light based on Camera API 2

Android has deprecated its old camera API Class and now added a new Camera API 2 class from Android 5+. The code for torch light based on new Camera API 2 class is defined below:

import android.content.Context;
import android.content.pm.PackageManager;

import android.util.Log;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager.TorchCallback;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;

private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
CameraDevice mCameraDevice;
CameraManager mCameraManager;
CameraCharacteristics mCameraCharacteristics;

boolean flashAvailable = false;
String cameraId;
String[] list=new String[10];

// ———————– code to check number of torch lights available to a mobile ———————-
public ArrayList getFlashIds()
{
ArrayList cIds = new ArrayList();

if (Build.VERSION.SDK_INT <= 21)
{

android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();

if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cIds.add(“1”);
} else { // back-facing camera
cIds.add(“0”);
}

return cIds;
}

mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try
{
list = mCameraManager.getCameraIdList();
for(int i=0;i<list.length;i++)
{
mCameraCharacteristics = mCameraManager.getCameraCharacteristics(list[i]);

flashAvailable = mCameraCharacteristics.get(mCameraCharacteristics.FLASH_INFO_AVAILABLE);

if(flashAvailable == true)
{
cIds.add(list[i]);
}
}
}
catch (CameraAccessException e)
{
e.printStackTrace();
BA.Log(“Error: ” + e.getReason() + “”);
}
return cIds;

}

//—————————————————————

// ———————- code to turn Torch Light on based on id ————————-

// ———————- id=0 is for back light, while id=1 is for front torch light —————

public void cam2On(String Id)
{

mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (mCameraManager == null)
{
BA.Log(“Camera not initiated”);
}

try
{
//list = mCameraManager.getCameraIdList();
//BA.Log(list[0]);

//BA.Log(“Test”);
//mCameraCharacteristics = mCameraManager.getCameraCharacteristics(list[0]);
mCameraCharacteristics = mCameraManager.getCameraCharacteristics(Id);

flashAvailable = mCameraCharacteristics.get(mCameraCharacteristics.FLASH_INFO_AVAILABLE);
}
catch (CameraAccessException e) {
e.printStackTrace();
BA.Log(“Error: ” + e.getReason() + “”);
}

if (flashAvailable) {

//BA.Log(“flash available”);

try {
//BA.Log(“1”);
mCameraManager.setTorchMode(Id, true);
//BA.Log(“1.5”);

// mCameraManager.registerTorchCallback(torchCallback, null);
//BA.Log(“2”);

try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
} catch (CameraAccessException e) {
e.printStackTrace();
BA.Log(“Open camera Error: ” + e.getReason() + “”);
}

} else {
BA.Log(“Flash not available”);
}

}

//================= Cam2 OFF ======================
public void cam2Off(String Id)
{

mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (mCameraManager == null)
{
BA.Log(“Camera not initiated”);
}

try
{
//list = mCameraManager.getCameraIdList();
//BA.Log(list[0]);

//BA.Log(“Test”);
//mCameraCharacteristics = mCameraManager.getCameraCharacteristics(list[0]);
mCameraCharacteristics = mCameraManager.getCameraCharacteristics(Id);

flashAvailable = mCameraCharacteristics.get(mCameraCharacteristics.FLASH_INFO_AVAILABLE);
}
catch (CameraAccessException e) {
e.printStackTrace();
BA.Log(“Error: ” + e.getReason() + “”);
}

if (flashAvailable) {

//BA.Log(“flash available”);

try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}

try {

// mCameraManager.unregisterTorchCallback(torchCallback);
//BA.Log(“1”);
mCameraManager.setTorchMode(Id, false);
//BA.Log(“1.5”);

} catch (CameraAccessException e) {
e.printStackTrace();
BA.Log(“Open camera Error: ” + e.getReason() + “”);
}

} else {
BA.Log(“Flash not available”);
}

}

//==================================================

//public CameraManager.TorchCallback torchCallback = new CameraManager.TorchCallback()
public static abstract class torchCallback {

// @Override
public void onTorchModeUnavailable (String cameraId) {
BA.Log(cameraId + ” mode Unavailable”);
}

public void onTorchModeChanged(String cameraId, boolean enabled)
{
BA.Log(“torch mode changed”);
BA.Log(cameraId + “: ” + enabled);

}

}

 

An Android App based on this Camera API 2 class is: Torch Light+

Torch Light+

 

Leave a Reply

Your email address will not be published. Required fields are marked *