2

我如何打开/关闭闪烁闪光灯相机灯?我尝试使用 for 循环但没有工作...冻结...为什么?需要警报管理器?线程可运行帖子延迟?

 for(int i=0;i<5;i++){
    flashon();
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    flashoff();
    }       
 }

   public void flashon(){
   camera = Camera.open();
   Parameters p = camera.getParameters();
   p.setFlashMode(Parameters.FLASH_MODE_TORCH);
   camera.setParameters(p);
   camera.startPreview();
}
4

3 回答 3

2

你需要使用Handler

Handler handler;
boolean isFlashon=false;
handler = new Handler();
    handler.removeCallbacks(toggleFlash);
    handler.postDelayed(toggleFlash, 1000 * 60);

private Runnable toggleFlash = new Runnable() {
    public void run() {
        if(isFlashon)
        {
            //do Flash off
            isFlashon=false;
        }
        else
        {
            //do Flash on
            isFlashon=true;
        }
        handler.postDelayed(this, 1000 * 60);
    }
};
于 2012-12-21T13:10:36.607 回答
1

尝试下面的代码。它会工作,因为我也面临类似的情况,下面的代码对我有用。

 private boolean isFlashOn;
 private Camera camera;
 Parameters params;

 private void turnOnFlash() {
    if (!isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(params);
        camera.startPreview();
        isFlashOn = true;


    }

}

private void turnOffFlash() {
    if (isFlashOn) {
        if (camera == null || params == null) {
            return;
        }

        params = camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.stopPreview();
        isFlashOn = false;

    }
}

 public void flash_effect() throws InterruptedException
{

    Thread a = new Thread()
    {
        public void run()
        {
          for(int i =0; i < 50; i++)
            {

                turnOnFlash();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                turnOffFlash();
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {

                    e.printStackTrace();
                }

            }
        }
    };

    a.start();
}
于 2017-03-28T07:47:01.953 回答
0

1.开机

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

2.关闭

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

并且,在 AndroidManifest.xml 上添加以下权限。

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

看到这个http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/

于 2012-12-21T12:48:11.283 回答