Android - Camera2 : The easiest way to turn on the torch light

这一生的挚爱 提交于 2019-12-25 20:05:19

问题


The simplest way to turn on the torch light was :

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

The simplest way to turn it off was :

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

But, as the Camera class is now deprecated, what is the easiest way to achieve the same result with the Camera2 class? I only found long and complicated solutions...


回答1:


check the answer from Daniel Netzer here you just need to write 3 line to turn on torch

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);


来源:https://stackoverflow.com/questions/37482014/android-camera2-the-easiest-way-to-turn-on-the-torch-light

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!