Programming camera in Android


Programming camera in Android

by Krishnaraj Varma onAugust 23, 2010

Download source code of this article.

Almost all mobile phones have a built in digital camera. Android phones are no exception. In Android device, to access the camera hardware we useCamera class along with SurfaceView.

The Camera class let you change the settings, preview and take still pictures. The SurfaceView class is used to show the preview to the user. To use the camera feature you should have the android.permission.CAMERA permission.

First we create a SurfaceView to show the live preview. The SurfaceView class holds the surface to draw. We use the interface SurfaceHolder to access the underlying surface. We implement the callback interface SurfaceHolfer.Callback and add this to the SurfaceHolder using SurfaceHolder.addCallback method. The SurfaceHolfer.Callback interface has three methods:

abstract void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
abstract void surfaceCreated(SurfaceHolder holder)
abstract void surfaceDestroyed(SurfaceHolder holder)

The surfaceCreated method will be called after the surface is created, surfaceChanged will be after any changes have been made to the surface and surfaceDestroyed will be called after the surface is destroyed.

To find more about the SurfaceView and SurfaceHolder interface refer to the Google documentation.

Once our surface is created we open the Camera using method. After opening the camera, we set the live preview surface usingsetPreviewDisplay() method of the Camera class. We open and set the preview surface once thesurfaceCreated() method is called because at this point the surface will be created and initialized properly.

Now we have to set the appropriate parameters to start the camera using the Camera.Parameters class. We can get the current camera parameter using thegetParameters() method of the Camera class. We set the camera parameter once thesurfaceChaneged() method is called. Once we set all the camera parameters we can start preview usingCamera.startPreview() method. This will start the live preview.

The Camera class uses different callback interface to report different events to the application. The following callback interfaces are used to notify the events;


The Camera.AutoFocusCallback interface is used to notify the completion of auto focus. Note that all devices may not have auto-focus feature. TheonAutoFocus() will be called once the auto focus in finished. We useCamera.autoFocus() method to start the auto focus.

The Camera.ErrorCallback interface is used to notify when an error occurred. TheonError() method will be called if an error occurred during the camera operation.

The Camera.PictureCallback interface is used to supply the image data after the picture is taken. TheonPictureTaken() method will be called once the data is available. The format of the data depends on the current camera picture format which can be set using Camera.Parameters class.

The Camera.PreviewCallback interface is used to notify the supply the current preview frame. TheonPreviewFrame() method will be called when the preview frame is available. Again the format of the data depends on the current camera picture format.

The Camera.ShutterCallback interface is to notify when the picture is captured from the camera. TheonShutter() method will be called once the picture is taken from the camera.

To take a still picture, we use the method Camera.takePicture() The takePicture() method has three parameters. First one is aCamera.ShutterCallback interface to notify when the picture is captured from the camera. Second one isCamera.PictureCallback interface to notify when raw data is available from the camera. The third one is anotherCamera.PictureCallback interface when the JPEG data is available from the device.

In API version 5 or above there is another variant of takePicture() method. This method accepts 4 parameters. The first and second are same as the previous one. The third is theCamera.PictureCallback interface which is used to notify when completely scaled postview image is available. The forth one is the same as the third parameter in the first variant.

The data will be supplied to the calling application, the application can use this data for post processing or saving to a file.

In API version 8 or above, we can use the method Camera.setDisplayOrientation() method to set the orientation of the display. This is used by portrait applications to correctly display the live preview. In versions prior to 8, we can safely use the landscape mode to correctly display the preview.

The sample application displays the live preview. When the take picture button is clicked the image is saved to sdcard using time as the filename.

Happy Camera Coding :)

个人分类: 移动设备与嵌入式
上一篇Developing SMART Goals
下一篇Trees and Other Hierarchies in MySQL
想对作者说点什么? 我来说一句