0

I am implementing strobe functionality which works fine ( the LED camera flash light turn on/off) the problem that i occurring is that the turn on/off with different frequency in different devices. In HTC SensationXE its blinking fast and in NEXUS its blinking slow dont know what the issue is. Can anyone help please: Here is code:

    /*
 * This method turn on the LED camera flash light
 */
public void flashLightOn() {
    Log.d("Time", "On");
    if (!isFlashOn) {
        if (cam != null || params != null) {
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(params);
            cam.startPreview();
            isFlashOn = true;
        } else {
            cam = Camera.open();
            params = cam.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(params);
            cam.startPreview();
            isFlashOn = true;
        }
    }
    else{
        return;
    }
}

/*
 * This method turn off the LED camera flash light
 */
public void flashLightOff() {
    Log.d("Time", "Off");
    if (isFlashOn) {
        if (cam != null || params != null) {
            //params.setFlashMode(Parameters.FLASH_MODE_OFF);
            //cam.setParameters(params);
            cam.stopPreview();
            isFlashOn = false;
        }
        else{
            //params = cam.getParameters();
            //params.setFlashMode(Parameters.FLASH_MODE_OFF);
            //cam.setParameters(params);
            cam.stopPreview();
            isFlashOn = false;  
        }


    }
    else{
        return;
    }
}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    mHolder = holder;
    try {
        cam.setPreviewDisplay(mHolder);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    cam.stopPreview();
    mHolder = null;

}

private final Runnable mRunnable = new Runnable() {

    public void run() {
        if (mActive) {
            if (mSwap) {
                flashLightOn();
                mSwap = false;
                mHander.postDelayed(mRunnable, strobeOnDelay);
            } else {
                flashLightOff();
                mSwap = true;
                mHander.postDelayed(mRunnable, strobeOffDelay);
            }
        }
    }
};

My class is implemention SurfaceHolder.Callback

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47

1 Answers1

2

The run method calls the delay operations with fixed parameters. That means the delay will take the processing time of everything else on the computer plus the delay period. The "everything else" may appear to be small and quick within your code but it will also include work by all the other processes that compete for processor time etc.

Your code simplifies to:

LoopForever
    Do some processing to toggle light ON or OFF;
    Delay for a fixed time;
EndLoop

A common way of handling regular activities is something like:

LoopForever
    Do some processing to toggle light ON or OFF;
    Calculate time interval until next toggle wanted.
    Delay for the calculated interval;
EndLoop

Note that the calculation of the time interval would refer to the time-of-day clock and compare time-now against time-wanted. That way any variations between how fast one loop executes compared to another would be ignored. The code always works out what time the next toggle is wanted and waits until then. Hence over a long period the rate should be correct, although the individual ON and OFF times may be slightly inaccurate.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87