klemens
klemens

Reputation: 413

android and camera flash

I want to write application which will be used camera flash. I want the light to blink. For now I have this code:

void ledon() {
    cam = Camera.open(); 
    Parameters params = cam.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_ON);

    cam.setParameters(params);
    cam.startPreview();
    cam.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });
}

this part:

        cam.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });

I dont know why its necessary but it wont work without it.

This code turn on led and turn off automaticly for about 2 seconds. I want to led will turn on e.g. for 5 sec then 3 sec led off and then again on for 4 seconds. How I can manually set periods of time in which led will be on and off. Is this possible? Thanks for any help.

PS. I have samsung galaxy ace.

Upvotes: 0

Views: 826

Answers (1)

Stephen Wylie
Stephen Wylie

Reputation: 934

it seems like what you want to use is Parameters.FLASH_MODE_TORCH. You set it to TORCH when you want to turn on the flash LED, and then set it to AUTO when you want the torch light to turn off.

Also, check out this SO question Camera.Parameters.FLASH_MODE_TORCH replacement for Android 2.1 as it'll tell you about some issues people have run into with certain devices.

Then, for the timer, you can use an instance of Timer and a subclass of TimerTask to do the trick. Here's an example from code I've written to make sure an Internet query isn't taking too long:

private QueryLyricsTask clt;
private Timer t1;
...
/* Make sure the query doesn't take too long */
try {
    t1 = new Timer("monitorTimeout");
    t1.schedule(new qlt(), lyricsTimeout * 1000);
} catch (Exception e) {
    e.printStackTrace();
}
...
class qlt extends java.util.TimerTask {
    @Override
    public void run() {
        if (clt.getStatus() != Status.FINISHED)
            clt.cancel(true);
    }
}

To explain these variables, "monitorTimeout" is a tag name to reference the Timer by. "qlt" is the class that runs once Timer t1 has elapsed. "lyricsTimeout" is a numeric value specified by the user in the settings (for timeout in seconds). "clt" is an instance of a class derived from AsyncTask so it will run the query without freezing up my UI. Obviously in your case, you probably wouldn't need an AsyncTask, but perhaps a simple "if" statement in run() to toggle the flash torch based on its current state.

Check out the documentation on the Timer class so you can learn how to make Timers that fire once or multiple times, or at certain times of day.

Upvotes: 1

Related Questions