Preview Surface

Also before we can get started using the camera, we need to create some type of Surface for the Camera to draw viewfinder or preview images on. A Surface is an abstract class in Android representing a place to draw graphics or images. One straightforward way to provide a drawing Surface is to use the SurfaceView class. SurfaceView is a concrete class providing a Surface within a standard View.

To specify a SurfaceView in our layout, we simply use the <SurfaceView /> element within any normal layout XML. Here is a basic layout that just implements a SurfaceView within a LinearLayout for a camera preview.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<SurfaceView android:id="@+id/CameraView" android:layout_width="fill_parent"^ android:layout_height="fill_parent"></SurfaceView> </LinearLayout>

In our code, for the purposes of using this SurfaceView with the Camera class, we'll need to add a SurfaceHolder to the mix. The SurfaceHolder class can act as a monitor on our Surface, giving us an interface through callbacks to let us know when the Surface is created, destroyed, or changed. The SurfaceView class conveniently gives us a method, getHolder, to obtain a SurfaceHolder for its Surface.

Here is a snippet of code that accesses the SurfaceView as declared in the layout XML and obtains a SurfaceHolder from it. It also sets the Surface to be a "push" type of Surface, which means that the drawing buffers are maintained external to the Surface itself. In this case, the buffers are managed by the Camera class. A "push" type of Surface is required for the Camera preview.

SurfaceView cameraView = (CameraView) this.findViewById(R.id.CameraView); SurfaceHolder surfaceHolder = cameraView.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Additionally, we'll want to implement SurfaceHolder.Callback in our activity. This allows our activity to be notified when the Surface is created, when it changes and when it is destroyed. To implement the Callback, we'll add the following methods.

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {} public void surfaceCreated(SurfaceHolder holder) {} public void surfaceDestroyed(SurfaceHolder holder) {}

To finish up, we'll need to tell our SurfaceHolder to use this activity as the Callback handler.

surfaceHolder.addCallback(this);

Our activity should now look something this.

package com.apress.proandroidmedia.ch2.snapshot;

import android.app.Activity; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView;

public class SnapShot extends Activity implements SurfaceHolder.Callback {

SurfaceView cameraView; SurfaceHolder surfaceHolder;

@Override public void onCreate(Bundle savedInstanceState) {

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

cameraView = (SurfaceView) this.findViewById(R.id.CameraView); surfaceHolder = cameraView.getHolder();

surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceHolder.addCallback(this);

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { }

public void surfaceCreated(SurfaceHolder holder) { }

public void surfaceDestroyed(SurfaceHolder holder) { }

Was this article helpful?

0 0

Post a comment