Home > android java > Using the Camera

Using the Camera

April 15th, 2009 Leave a comment Go to comments

(Android phone – how-to/example)

In order to use the Android Camera you need your activity to implement android.view.SurfaceHolder.Callback, example:

public class CameraTest extends Activity implements SurfaceHolder.Callback
{
// Camera object
private Camera m_oCamera;

In your Activity, you’ll also need:
- an android.view.SurfaceView.SurfaceView to display the picture preview and
- an android.view.SurfaceHolder that is an <<abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface.>>

private SurfaceView m_oSurfaceView;
private SurfaceHolder m_oSurfaceHolder;

Than you initialize them:
m_oSurfaceView = new SurfaceView(this);
setContentView(m_oSurfaceView);

Connect SurfaceHolder to SurfaceView
mSurfaceHolder = mSurfaceView.getHolder();

After that, you will need to add a callback to the surface holder. The callback will inform you when the surface was created/changed/destroyed:
mSurfaceHolder.addCallback(this);

Set surface not to have its own buffers in order to immediately display the image
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Also you may want to implement surfaceCreated(SurfaceHolder holder). This method is called when surface is created, for example to initialize the camera object:

public void surfaceCreated(SurfaceHolder holder)
{
m_oCamera = Camera.open();
}

The method surfaceChanged(SurfaceHolder holder, int format, int width, int height) is called after surface is created. Here you can start to preview and set camera/preview parameters.

public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int heigth)
{
/* if the preview is already running (for example the application was paused) */
if (m_bPreviewRunning)
m_oCamera.stopPreview();

/* set preview parameters */
Camera.Parameters params = m_oCamera.getParameters();
params.setPreviewSize(width, heigth);
m_oCamera.setParameters(params);

try
{
/* Sets the SurfaceHolder to be used for a picture preview. */
m_oCamera.setPreviewDisplay(holder);
}
catch (IOException e) {}

/* start camera preview */
m_oCamera.startPreview();
m_bPreviewRunning = true;
}

The surfaceDestroyed(SurfaceHolder holder) method is called immediately before a surface is being destroyed. You must stop camera preview because the surface will no longer exist.

public void surfaceDestroyed(SurfaceHolder holder)
{
m_oCamera.stopPreview();
m_bPreviewRunning = false;
m_oCamera.release();
}

When you want to take a picture you will call:

m_oCamera.takePicture(null, null, m_cbPicture);

This will take a picture and after the picture is jpeg compressed the callback m_cbPicture will be called.
NOTE: In Android SDK 1.5 the raw picture callback has a bug and image buffer is empty.

This is how your jpeg callback should look like.
Camera.PictureCallback m_cbPicture = new Camera.PictureCallback()
{
public void onPictureTaken(byte[] data, Camera c)
{
/* do something with picture from data array
do something with picture from data array
Hint: use the Bitmap class, but do not do much processing in the callback, send yourself a message with the message handler, and do the processing at some other point (maybe in a different thread if you will implement lots of processing)
*/

/* restart camera preview */
m_oCamera.startPreview();
}
};

Code example: you may find useful to see an implementation of this in the Mezzofanti application – google code. The code is released under Apache License, ver 2.0, so you can freely use it free of charge in your own code. Consult the class CameraManager.java.

Categories: android java Tags:
  1. No comments yet.
  1. No trackbacks yet.