1、首先,肯定不能忘记添加权限
<uses-permission android:name="android.permission.CAMERA" />2、直接粘贴源码
public class MainActivity extends Activity {
private Button openLight;
private boolean status = false;
private Camera camera;
private Parameters parameters;
private MainActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
camera = Camera.open();
openLight = (Button)findViewById(R.id.openLight);
openLight.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!status) {
openLight.setText("关闭");
status = true;
new Thread(new TurnOnLight()).start();
}else {
status = false;
openLight.setText("打开");
instance.parameters.setFlashMode("off");
instance.camera.setParameters(instance.parameters);
}
}
});
}
private class TurnOnLight implements Runnable{
@Override
public void run() {
instance.parameters = instance.camera.getParameters();
instance.parameters.setFlashMode("torch");
instance.camera.setParameters(instance.parameters);
}
}
}
3、项目下载地址