如何使用sl4a Python打开和关闭相机LED?最大强度是可以的。
android中的任何内容。*?
以下是代码:
import sys
def toggle_LED(action="on"):
if action == 'on':
with open('/sys/class/leds/torch-flash/flash_light', 'w') as on:
on.write('1')
elif action == 'off':
with open('/sys/class/leds/torch-flash/flash_light', 'w') as off:
off.write('0')
if __name__ == '__main__':
toggle_LED()
while True:
if strip(sys.stdin.read())[0] == 'q':
toggle_LED(action='off')
sys.exit()
编辑:手机是华为Ideos X5 U8800H
答案 0 :(得分:2)
android中的任何内容。*?
您可以在Android中以下列方式打开和关闭手电筒。
打开手电筒。
Camera cam = null;
public void turnOnFlashLight() {
try {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception throws in turning on flashlight.", Toast.LENGTH_SHORT).show();
}
}
关掉手电筒。
public void turnOffFlashLight() {
try {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
cam.stopPreview();
cam.release();
cam = null;
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception throws in turning off flashlight.", Toast.LENGTH_SHORT).show();
}
}
不要忘记在清单中添加权限。
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT"/>
更多参考:Turn on and turn off flash light programmatically in Android