Search code examples
androidcamerasamsung-mobile

Nexus S Flash torch mode not working


I'm using Nexus S Android 2.3 stock version. Whenever I'm trying to set the flash to torch mode:

if (camera == null) {
    mCamera = camera = Camera.open();
}
final Parameters params = camera.getParameters();
params.setFlashMode(MODE_TORCH);
camera.setParameters(params);

nothing happens, the flash is not working at all. This piece of code works perfectly on nexus one.

I've found this - How to use camera flash/led as torch on a Samsung Galaxy Tab? and also this one - http://forum.xda-developers.com/showthread.php?t=923786 none of them seems to help.

Any idea what am I missing ?


Solution

  • private Camera _camera;
    protected static final String MODE_TORCH = Camera.Parameters.FLASH_MODE_TORCH;
    protected static final String MODE_OFF = Camera.Parameters.FLASH_MODE_OFF;
    
    private void initCamera(){
                if(this._camera == null){
                this._camera = Camera.open();
                this._camera.startPreview();
                }
        }
    
    private void releaseCamera(){
            if(this._camera != null)
            {
                this._camera.stopPreview();
                this._camera.release();
            }
    
            this._camera = null;
        }
    
    private void setCameraParameter(String value){
            if(this._camera != null){
                Camera.Parameters params = this._camera.getParameters();
                params.setFlashMode(value);
                this._camera.setParameters(params);
            }
        }
    
    //To turn on just use:
    private void turnOn(){
        initCamera();
        setCameraParameter(MODE_TORCH);
    }
    
    //to turn off just use:
    private void turnOff(){
        setCameraParameter(MODE_OFF);
    }
    
    //To release resorces use:
    private void releaseResources(){
       releaseCamera();
    }
    

    This will work on android 2.3. To work on android 2.3 and 4.0.3 you will have to play with surfaceView and surfaceHolder.