Kostadin
Kostadin

Reputation: 41

How to create fast or slow camera flash blink signals in Android?

I have service that make flash blink signals:

    public class FlashBlinkService extends Service 
    {
        private static Camera cam = null;
        private Handler handler = new Handler();

        @Override
        public IBinder onBind(Intent intent) 
        {
            return null;
        }

        @Override
        public void onCreate()
        {
            super.onCreate();
            startFlashBlink();      
        }

        //for simulating flash blink
        Runnable flashBlinkRunnable = new Runnable()
        {
            public void run() 
            {   
                cam = Camera.open();
                Parameters p = cam.getParameters();
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                cam.release();
                cam = null;
                //--->
                handler.post(flashBlinkRunnable);
            }       
        };

        //start flash blink light
        public void startFlashBlink()
        {
            flashBlinkRunnable.run();
        }

        //stop flash blink light
        public void stopFlashBlink()
        {
            handler.removeCallbacks(flashBlinkRunnable);
            stopCamera(cam);
        }

        //stop camera
        private void stopCamera(Camera cam)
        {
            if (cam != null)
            {
                cam.stopPreview();
                cam.release();
                cam = null;
            }
        }

        @Override
        public void onDestroy()
        {
            super.onDestroy();
            handler.removeCallbacks(flashBlinkRunnable);
            stopCamera(cam);
        }
    }

This is the 'fastest' flash blink solution right now for me. Which is the best way to make faster or slower flash blink signals? There is a lots of applications in the market (like some flashlights) that provide this option. Thanks.

Upvotes: 4

Views: 2806

Answers (2)

Clapa Lucian
Clapa Lucian

Reputation: 600

You can create a new thread and after each turnOn/turnOff sleep the thread as long as you want the pause to be

Upvotes: 0

Wizche
Wizche

Reputation: 891

I've accomplished this in the following way. The frequency of the strobe (flash blinking) is regulated by the sleepMS variable in the Thread.sleep() function. For example if you want 10 flashes at 10 Hz you would set

sleepMS=(1/10)*50;
flashCount=10;

Or more general:

sleepMS=(1/Hz)*1000/2

Not sure is the best way to accomplish this but it works.

Camera cam;
Parameters params;

private void main(){
    cam = Camera.open();
    params = cam.getParameters();
    cam.setPreviewTexture(new SurfaceTexture(0));
    cam.startPreview();

    for (int i = 0; i < flashCount; i++) {
        flipFlash();
        Thread.sleep(sleepMS);
        flipFlash();
        Thread.sleep(sleepMS);
    }
    cam.stopPreview();
    cam.release();
}

private void flipFlash(){
    if (isLighOn) {
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        cam.setParameters(params);
        isLighOn = false;
    } else{
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        cam.setParameters(params);
        isLighOn = true;
    }
}

Upvotes: 5

Related Questions