实现手电筒Flash Light 关键代码

实现Flash的逻辑

view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!isOpen){
view.setBackgroundResource(R.drawable.open); //改变背景图片
        openFlash();
isOpen = true;
}else{
view.setBackgroundResource(R.drawable.off); //改变背景图片
closeFlash();
isOpen = false;
}
}
});

  

打开手电筒

private void openFlash(){
camera = Camera.open(); //打开照相机
Parameters params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
//设置照相机参数,FLASH_MODE_TORCH 持续的亮灯,FLASH_MODE_ON 只闪一下
camera.setParameters(params);
camera.startPreview(); //开始亮灯
}

  

关掉手电筒

private void closeFlash(){
if(camera != null){
camera.stopPreview(); //关掉亮灯
camera.release(); //关掉照相机
}
}

  

Release:

private void release(){
if(camera != null){
camera.release();
}
}

 参考:android小记之FlashLight --- 高仿小米手电筒

04-28 03:30