Saturday, July 31, 2010

Live Camera Preview on Android

Android SDK provides a full control on Android devices' hardware, so that you can use any component of your device in your applications. Thanks to this feature, you can use your device's camera to obtain live camera preview in your application. So that, you can build your own camera application with additional features.

Basically in a camera application, you need a camera surface and a button to take photos. First we start with getting required permission for Camera component. Also put a control into the activity tag to keep screen in landscape mode.


 <uses-permission android:name = "android.permission.CAMERA"/>

 <activity .......
    android:screenOrientation="landscape" ...... >


Then we create a simple layout includes a SurfaceView and a Button.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">

 <SurfaceView
  android:id="@+id/preview"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_weight="1">
 </SurfaceView>

 <Button
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/buttonTakePhoto"
  android:text="Click"
  android:gravity="center">
 </Button>

</LinearLayout>

Now we are ready to build our application to put the camera preview into our SurfaceView and to set the button's behavior for taking photos. Our activity must implement the SurfaceHolder.Callback interface in order to provide a camera preview.


 // Let's name our Activity as CamDroid.
public class CamDroid extends Activity implements SurfaceHolder.Callback

Then we create our Camera, Surface and Button fields which will be initialized when our activity is started.


    private Camera mCamera;
    private Button takePictureButton;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;

After this fields we need to declare which actions will be performed when user takes a photo.


    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera c) {
         
         FileOutputStream outStream = null;
          try {
          
          // Directory and name of the photo. We put system time
          // as a postfix, so all photos will have a unique file name.
          outStream = new FileOutputStream("/sdcard/CamDroid_" +
              System.currentTimeMillis()+".jpg");
          outStream.write(data);
          outStream.close();
          Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length);
         } catch (FileNotFoundException e) {
          e.printStackTrace();
         } catch (IOException e) {
          e.printStackTrace();
         } finally {
        }

            Log.e(TAG, "PICTURE CALLBACK: data.length = " + data.length);
            mCamera.startPreview();
        }
    };

Now we can implement the onCreate method of the Activity to initialize our surface and Button.


    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);

        // We don't need a title for our camera window.
        // It makes our view smaller.
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        Log.e(TAG, "onCreate");
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        // In my case, name of the layout file is camdroid.xml
        setContentView(R.layout.camdroid);

        // Initialize the surface
        mSurfaceView = (SurfaceView)findViewById(R.id.preview);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        // Initialize the Button
        takePictureButton = (Button)findViewById(R.id.buttonTakePhoto);
        
        // Set the button's behavior
        takePictureButton.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
          mCamera.takePicture(null, null, mPictureCallback);
         }
        });
    }

Then we must provide some methods for our surface to create first preview, change preview, and stop preview.


    // Create camera preview.
    public void surfaceCreated(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceCreated");
        mCamera = Camera.open();
        //mCamera.startPreview();
    }

    // Change preview's properties (i.e. size or format).
    public void surfaceChanged(SurfaceHolder holder,
          int format, int w, int h)
    {
        Log.e(TAG, "surfaceChanged");

        // XXX stopPreview() will crash if preview is not running
        if (mPreviewRunning) {
            mCamera.stopPreview();
        }

        Camera.Parameters p = mCamera.getParameters();
        p.setPreviewSize(w, h);
        mCamera.setParameters(p);
        try {
         mCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
        mCamera.startPreview();
        mPreviewRunning = true;
    }

    // Stop the preview.
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        Log.e(TAG, "surfaceDestroyed");
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();
    }

Now we have a basic camera application which has a camera preview and a button on screen. You can start taking photos with your applications by pressing the button on the screen. Or you can add this method to use your device's built-in "OK" Button (Trackpad for HTC, OK Button for Samsungs etc.) for taking photos.


    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        }
 
        if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
            mCamera.takePicture(null, null, mPictureCallback);
            return true;
        }

        return false;
    }

ScreenShot:

Enjoy your new camera applications and take good photos :)

6 comments:

  1. Hi Yasin,
    I hope to bring development, but it's too hard for me (mackerel time and I do not have the basics) but I have some ideas of which you might be interested, the goal is to take a picture urgent and of sufficient quality :

    http://en.androidwiki.com/wiki/SpeedPic

    u can contact me at billybug well on gmail ;-)

    friendly ...
    Billybug.

    ReplyDelete
  2. Is there a complete java file for this? I created a TAG, but I am unaware what to do about "mPreviewRunning." It says mPreviewRunning cannot be resolved to a variable. What do I do about that?

    ReplyDelete
  3. Yeah, there's something missing here. Can this please be updated so the tutorial is complete? Thanks.

    ReplyDelete
  4. @Daniel you should define a boolean mPreviewRunning in your class. That attribute prevents users to try to take photo while previewing the last photo.

    ReplyDelete
  5. Hello Yasin
    thanks for this nice preview
    while the camera is open ,how i can make a digital zoom for the surfacview
    I tred but with no luck ,something like if i have 2 buttons (zoom in ,zoom out) could you please tell me how I could do that
    thanks
    Anas

    ReplyDelete
  6. please just inbox me the full program files so i can just dowload it....
    e mail , limoraka@gmail.com
    thanks

    ReplyDelete