Android - Camera2:开启火炬之光的最简单方法

时间:2016-05-27 11:11:26

标签: android torch camera2

打开手电筒灯的最简单方法是:

Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();

关闭它的最简单方法是:

Camera camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();

但是,由于现在不推荐使用Camera类,使用Camera2类获得相同结果的最简单方法是什么?我只找到了漫长而复杂的解决方案......

1 个答案:

答案 0 :(得分:0)

检查Daniel Netzer的答案here 你只需要写3行来打开火炬

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