2016-05-27 81 views
-1

打開電筒光線最簡單的方法最簡單的方法是:的Android - 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類相同結果的最簡單方法是什麼?我才發現,長期和複雜的解決方案......

+0

https://developer.android.com/reference /android/hardware/camera2/CameraManager.html#setTorchMode(java.lang.String,boolean) – user0815

+0

嚴重@ user0815。官方文檔鏈接?來吧......:p – Denis

回答

0

檢查答案由丹尼爾·內策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); 
+0

激活Flash需要更多的代碼。 :| – Denis

+0

請注意,setTorchMode可從API級別23+ –