该方法并不是所有Android手机都能用,仅在三星手机上测试过
三星在CameraManager里面增加了setTorchMode和semSetTorchMode函数,这两个函数是一样的
函数原型
public void setTorchMode(String cameraId, boolean enabled, int strength); public void semSetTorchMode(String cameraId, boolean enabled, int strength);
通过反射来调用
CameraManager am = (CameraManager) getSystemService(Context.CAMERA_SERVICE); Method localMethod = null; try { String [] cameraIdList = am.getCameraIdList(); localMethod = am.getClass().getMethod("semSetTorchMode", new Class[]{String.class, boolean.class, int.class}); Object []params = new Object[3]; params[0] = cameraIdList[0]; // 摄像头ID params[1] = true; // true打开, false关闭 params[2] = 2; // 闪光灯亮度,范围0~6 localMethod.invoke(am, params); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (CameraAccessException e) { e.printStackTrace(); }
标签:String,int,闪光灯,am,亮度,printStackTrace,params,catch,ANDROID From: https://www.cnblogs.com/opbye/p/17030705.html