让相机灯闪烁

Make camera light flash

我想让相机闪光灯。我的代码将使它在按下一个按钮时闪烁得很好,但如果我在它完成闪烁之前再次按下按钮,它会使我的应用程序崩溃。我想我需要以某种方式检查它是否仍在闪烁,然后再尝试再次启动闪光灯。

public class BlinkBack {
public static void blink(MainActivity ma){
    if (ma.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
        final Camera mCamera = Camera.open();
        new CountDownTimer(5000, 100) {
            int counter = 0;
            public void onTick(long millisUntilFinished) {
                if (counter % 2 == 0) {
                    Camera.Parameters params = mCamera.getParameters();
                    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    mCamera.setParameters(params);
                    mCamera.startPreview();
                }
                else {
                    Camera.Parameters params = mCamera.getParameters();
                    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    mCamera.setParameters(params);
                    mCamera.stopPreview();
                }
                counter++;
            }

            public void onFinish() {
                Camera.Parameters params = mCamera.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                mCamera.setParameters(params);
                mCamera.stopPreview();
                mCamera.release();
            }
        }.start();
    }
}

}

我找到了自己的答案。最终相机 mCamera = Camera.open();有一个未经检查的 RunTimeException 所以我只是用 try catch 吃了那个异常并修复了它

public class BlinkBack {
public static void blink(MainActivity ma){
    if (ma.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
        try {
            final Camera mCamera = Camera.open();
            new CountDownTimer(5000, 100) {
                int counter = 0;
                public void onTick(long millisUntilFinished) {
                    if (counter % 2 == 0) {
                        Camera.Parameters params = mCamera.getParameters();
                        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                        mCamera.setParameters(params);
                        mCamera.startPreview();
                    }
                    else {
                        Camera.Parameters params = mCamera.getParameters();
                        params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                        mCamera.setParameters(params);
                        mCamera.stopPreview();
                    }
                    counter++;
                }

                public void onFinish() {
                    Camera.Parameters params = mCamera.getParameters();
                    params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    mCamera.setParameters(params);
                    mCamera.stopPreview();
                    mCamera.release();
                }
            }.start();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}