Make camera light flash

104 Views Asked by At

I want to make the camera light flash. My code will make it flash just fine of a single button press but if I hit the button again before it has finished flashing it will crash my app. I believe I need to somehow check to see if it is still flashing before I try to start the flash again.

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();
    }
}

}

1

There are 1 best solutions below

0
On BEST ANSWER

I found my own answer. final Camera mCamera = Camera.open(); has an unchecked RunTimeException so I just ate that exception with a try catch and it fixed it

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();
        }
    }
}

}