Using the Camera Preview

Write App Reviews

Build your own Android App Dev Empire

Get Instant Access

Access to the camera's streaming video means that you can incorporate live video into your applications.

Some of the most exciting Android applications use this functionality as the basis for implementing augmented reality (the process of overlaying dynamic contextual data —such as details for landmarks or points of interest —on top of a live camera feed).

Much like the Media Player and Media Recorder classes, the camera preview is displayed onto a SurfaceHolder. To view the live camera stream within your application, you must include a Surface View within your UI. Implement a SurfaceHolder.Callback to listen for the construction of a valid surface, before passing it in to the setPreviewDisplay method of your Camera object.

A call to startPreview will begin the streaming and stopPreview will end it, as shown in Listing 11-16.

LISTING 11-16: Previewing real-time camera stream public class MyActivity extends Activity implements SurfaceHolder.Callback {

Available for download on private Camera camera;

@Override public void onCreate(Bundle savedInstanceState) {


LISTING 11-16 (continued)

super.onCreate(savedlnstanceState); setContentView(R.layout.main);

SurfaceView surface = (SurfaceView)findViewByld(; SurfaceHolder holder = surface.getHolder(); holder.addCallback(this);

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.setFixedSize(400, 300);

public void surfaceCreated(SurfaceHolder holder) { if (mediaRecorder == null) { try {

camera =; camera.setPreviewDisplay(holder); camera.startPreview(); [ ... Draw on the Surface ... ] } catch (IOException e) {

public void surfaceDestroyed(SurfaceHolder holder) { camera.stopPreview(); camera.release();

You'll learn more about Surfaces in Chapter 15, although the Android SDK includes an excellent example of using a SurfaceView to display the camera preview in real time.

You can also assign a PreviewCallback to be fired for each preview frame, allowing you to manipulate or display each preview frame individually.

Call the setPreviewCallback method on the Camera object, passing in a new PreviewCallback implementation overriding the onPreviewFrame method as shown in Listing 11-17.

LISTING 11-17: Assigning a preview frame callback Available for download on camera.setPreviewCallback(new PreviewCallback() { public void onPreviewFrame(byte[] _data, Camera _camera) {

// TODO Do something with the preview image.

Each frame will be received by the onPreviewFrame event with the image passed in through the byte array.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


  • azzeza
    How to surfaceview to display the camera preview in real time?
    8 years ago

Post a comment