Wednesday, January 18, 2012

How to use Android Camera to Take Pictures Or Camera Integration with Surface View

I am going to display today, how to take a picture from camera.We use surface view to show camera's content.So first make layout with Surface_View

Camera Layout

Make one XML file path res/layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<SurfaceView android:id="@+id/surface_camera"
android:layout_width="fill_parent" android:layout_height="10dip"
android:layout_weight="1">
</SurfaceView>
</LinearLayout>

Camera Implementation code

create an activity called “CameraView” that implements the interface SurfaceHolder.Callback.
public class CamaraView extends Activity implements SurfaceHolder.Callback
This interface “SurfaceHolder.Callback” is used to receive information about changes that occur in the surface (in this case, the camera preview). SurfaceHolder.Callback implements three methods:
surfaceChanged
This method is called when the size or the format of the surface changes.
surfaceCreated
When, in the first instance, the surface is created, this method is called.
surfaceDestroyed
This is called when the surface is destroyed.

Camera Code 

 We'll use the onCreate method in our activity.

super.onCreate(icicle);
setContentView(R.layout.camera_surface);
mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
We set the layout to the Activity with the setContentView to the camera_surface (the one we created some lines above!) and we create a SurfaceView object, getting it from the xml file.
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
With these lines, we get the holder from the surfaceview, and we add the callback function to “this”. This means that our activity is going to manage the surfaceview.
Now see how the callback functions are implemented.
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
mCamera is an Object of the class “Camera”. In the surfaceCreated we “open” the camera. This is how to start it!!
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(w, h);
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
mPreviewRunning = true;
}
This is the method that prepares the camera, sets parameters and starts the preview of the image in our Android screen. I have used a “semaphore” parameter to prevent crashing: when the mPreviewRunning is true, it means that the camera is active and has not “closed”, so this way we can work with it.
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
}
This is the method where we stop the camera and release the resources associated to the camera. See, that here we set the mPreviewRunning flat to “false”, with this, we prevent crashing in the surfaceChanged method. Why? Because this means that we have “closed” the camera, and we cannot set parameters or start image previews in our camera (stuff that the surfaceChanged method does) if it is closed!.
And let's finish with the most important method:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};
This method is called when a picture is taken. For example, you could create an OnClickListener on the screen that calls the PictureCallBack method when you click on the screen. This method just gives you thebyte[] of the image. So, just convert it from byte[] to some image format you want, using the Bitmap and BitmapFactory class that Android offers us. 

No comments:

Post a Comment