Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Camera flash light blinking in wrong intervals in different devices

Tags:

android

flash

led

I am developing an app, in which I want to blink the flash light in specific interval.

Below are the steps I have followed.

1) Set the Timer for specific interval.

2) In run() method i did the code for TurnOn and TurnOff flash.

But the interval of flash blinking is different on different devices. The timer time is same for all devices, I have also put a Log in between, I am getting same values but, still the problem is there.

Is it a Hardware issue, because the hardware is different for different devices. I have also tested in iPhone 5s (By converting same code in iOS) but, the flash blinking is much faster than Android.

For Android, I have tested on Nexus 4, Motorola G2, Sony Xperia Neo and it is working fine.

Problem is with Nexus 5 and Samsung Galaxy S4.

EDIT

Code of Timer :

long delayLong = 200;
long timerValueLong = 500;
Timer timer;

timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        if (!mLightOn) {
            turnOnFlash();
        } else {
            turnOffFlash();
        }
    }
}, delayLong, timerValueLong);
like image 849
Mitesh Shah Avatar asked May 22 '15 10:05

Mitesh Shah


3 Answers

This is an older problem, but the problem still persists today so I'll post how I solved this.

The problem is that the call to turn the LED on or off takes variable amount of time to traverse through the Android operating system. The way these calls are handled are phone dependent.

First off you need to measure the time it takes for the LED to turn on and off starting from the time the call to do so. Use the input from the camera, keep the phone close to a surface and measure the change in brightness in the frame. You can use glReadPixels if working with OpenGL and read out the center line only each frame. You will need to make multiple measurements, as the call can be shorter or longer depending on the state of the OS. Preferably you'd want to have no buffer or a fixed buffer of frames, so timing of the frames is reliable (which might not be the case with compression). I used OpenGL and a SurfaceTexture and this is a snappy way.

You now know the minimum(1) and maximum(2) time it takes for the call to traverse the OS. Using this information you can make the LED blink as fast as possible on the phone. To truly get the most out of it, start the second call to the flash before maximum(2) time has passed; maximum(2) - minium(1).

Using the last trick, the speed of the flashing is mostly dependent on the difference in minimum and maximum time of the call traversal. This is typically very different per phone, from 10ms to 100ms+.

Also note that because the measuring of the call traversal time happens with the camera, times are rounded up/down to 33ms segments (@30fps).

like image 87
RobotRock Avatar answered Oct 24 '22 20:10

RobotRock


I had the same issue with the flashlight and the problem is not related to the Timer. It is about how you are turning the flash on and off. On some devices like Nexus 5, you have to have and use a SurfaceView inside of your layout. It would be useful to show us the methods you are using for turning the flashlight on and off.

like image 35
David Avatar answered Oct 24 '22 18:10

David


    long delayLong = 20;
    long timerValueLong = 100;
    Timer timer;
    final Parameters p = camera.getParameters();
    timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            if (!isLighOn) {
                p.setFlashMode(Parameters.FLASH_MODE_ON);
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                isLighOn = true;
            } else {
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                isLighOn = false;
            }
        }
    }, delayLong, timerValueLong);
like image 1
وليد عبد العزيز Avatar answered Oct 24 '22 19:10

وليد عبد العزيز