参考文章:
关于Android 7.0 手电筒的控制
try {
//获取CameraManager
CameraManager mCameraManager = (CameraManager) MyApplication.getContext().getSystemService(Context.CAMERA_SERVICE);
//获取当前手机所有摄像头设备ID
String[] ids = mCameraManager.getCameraIdList();
for (String id : ids) {
CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
//查询该摄像头组件是否包含闪光灯
Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
/*
* 获取相机面对的方向
* CameraCharacteristics.LENS_FACING_FRONT 前置摄像头
* CameraCharacteristics.LENS_FACING_BACK 后只摄像头
* CameraCharacteristics.LENS_FACING_EXTERNAL 外部的摄像头
*/
Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
if (flashAvailable != null && flashAvailable
&& lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
//打开或关闭手电筒
mCameraManager.setTorchMode(id, OPEN? true:false);
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}