This is a mirror of official site: http://jasper-net.blogspot.com/

CameraTutorial - Android Camera Integration

| Monday, February 28, 2011
Introduction
A simple guide for integrating the Camera Hardware inside your Android App.

Step 1: Create a class that extends SurfaceView and implements SurfaceHolder.Callback
Before you can integrate the Camera hardware with your Activity, you must provide a Preview screen. The camera's Preview output is supplied to this screen. The SurfaceView instance with a SurfaceHolder.Callback implementation provides such a View

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
       private SurfaceHolder holder;
       private Camera camera;
       
       public CameraSurfaceView(Context context)
       {
               super(context);
               
               //Initiate the Surface Holder properly
               this.holder = this.getHolder();
               this.holder.addCallback(this);
               this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
       }

Step 2: In the surfaceCreated method, get an instance of the Camera

       @Override
       public void surfaceCreated(SurfaceHolder holder)
       {
               try
               {
                       //Open the Camera in preview mode
                       this.camera = Camera.open();
                       this.camera.setPreviewDisplay(this.holder);
               }
               catch(IOException ioe)
               {
                       ioe.printStackTrace(System.out);
               }
       }

Step 3: In the surfaceChanged callback

       @Override
       public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
       {
               // Now that the size is known, set up the camera parameters and begin
               // the preview.
               Camera.Parameters parameters = camera.getParameters();
               parameters.setPreviewSize(width, height);
               camera.setParameters(parameters);
               camera.startPreview();
       }

During this callback, you get an idea of how big your preview screen is going to be. Based on that, assign the proper size to the Camera.

Step 4: Implement surfaceDestroyed to cleanup


Read more: Openmobster

Posted via email from Jasper-net

0 comments: