Rahul
Rahul

Reputation: 131

Access Device hardware (Camera Flash) Programically ina android

I have one question that can we access android device camera's flash light
programmically.Is this possible to turn on and off flash light programmically? please give me my answer . thanks in advance.

    package com.thedevelopersinfo.tutorial.android.soundrecordingexample;

         import java.io.File;

         import android.app.Activity;
       import android.content.ContentResolver;
       import android.content.ContentValues;
     import android.content.Intent;
       import android.hardware.Camera;

     import android.media.MediaRecorder;
      import android.net.Uri;
     import android.os.Bundle;
      import android.os.Environment;
     import android.provider.MediaStore;
     import android.provider.MediaStore.MediaColumns;
      import android.view.View;
     import android.view.View.OnClickListener;
        import android.widget.Button;


         public class Main extends Activity {
    private MediaRecorder mediaRecorder;
    private File file = null;
    static final String PREFIX = "record";
    static final String EXTENSION = ".3gpp";
    private Button b1;
    Camera mCamera;
    public boolean isOn=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         mCamera = Camera.open();

        Camera.Parameters params = mCamera.getParameters();


        b1=(Button)findViewById(R.id.startBtn);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                 setFlashlight();



            }
        });

    }
    public void setFlashlight()
    {
        if (mCamera == null)
        {

        }
        Camera.Parameters params = mCamera.getParameters();
        String value;
        if (isOn) // we are being ask to turn it on
        {
            value = Camera.Parameters.FLASH_MODE_TORCH;
        }
        else  // we are being asked to turn it off
        {
            value =  Camera.Parameters.FLASH_MODE_AUTO;
        }

        try{    
            params.setFlashMode(value);
            mCamera.setParameters(params);

            String nowMode = mCamera.getParameters().getFlashMode();

            if (isOn)
            {
                 nowMode.equals(Camera.Parameters.FLASH_MODE_TORCH);
            }
            if (!isOn)
            {
                 nowMode.equals(Camera.Parameters.FLASH_MODE_AUTO);
            }

        }
        catch (Exception ex)
        {
        }
        }

}

but i got following exception 03-10 18:47:34.907: ERROR/QualcommCameraHardware(1280): Parameter AntiBanding is not supported for this sensor

Upvotes: 0

Views: 3624

Answers (2)

himen patel
himen patel

Reputation: 404

it is possible. first you have to get the properties of hardware camera.using below method.

String flattenString;
camera = Camera.open();
Camera.Parameters param = camera.getParameters();
flattenString = param.flatten();

where flatten can give u a parameter string with key value pair. you have to split it by "," and find the flash-mode key and its value then apply this parameter in below method

Camera.Parameters parameters = mCamera.getParameters();         
parameters.set("Key", value);

where your key is "flash-mode" and value is "On"

and then add this parameters to setParameters method and start your camera preview

mCamera.setParameters(parameters);
mCamera.startPreview(); 

your flash light is now ON.

Upvotes: 0

Eric Levine
Eric Levine

Reputation: 13564

You can do so by using Camera.Parameters.setFlashMode. See: http://developer.android.com/reference/android/hardware/Camera.Parameters.html

Upvotes: 1

Related Questions