//
you're reading...
HCI, Tech

Four Steps to Start Using Camera in Your Android Application

As a hello world of Android camera, our goal is, with a minimum of code, to show the camera image on the screen (similar to the camera app). After creating a normal Android project:

1. Add user permission to the manifest xml file

This line is added as a node of <manifest>:

<uses-permission android:name="android.permission.CAMERA"/>

2. Create and initialize a Camera object


private Camera mCamera;

// ...

mCamera = Camera.open();

3. Add a private CameraView class extending SurfaceView to display images captured from the camera

Detailed code see below. Whenever you want non-static view (animation, camera, etc.) you might need to extend the SurfaceView.

4. Create and initialize a CameraView object and setContentView it


private CameraView mView;

// ...

mView = new CameraView(this);
setContentView(mView);

Sample Code:


package me.xiangchen.hellostuff;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class HelloCameraActivity extends Activity {

private Camera mCamera;
 private CameraView mView;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 mCamera = Camera.open();
 mView = new CameraView(this);

 setContentView(mView);
 }

 // extending SurfaceView to render the camera images
 private class CameraView extends SurfaceView implements SurfaceHolder.Callback{
 private SurfaceHolder mHolder;

 public CameraView(Context context) {
 super(context);

 mHolder = this.getHolder();
 mHolder.addCallback(this);
 mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

 setFocusable(true);

 }

 @Override
 public void surfaceChanged(SurfaceHolder holder, int format, int width,
 int height) {
 }
 @Override
 public void surfaceCreated(SurfaceHolder holder) {

 try {
 mCamera.setPreviewDisplay(mHolder);
 } catch (IOException e) {
 mCamera.release();
 }
 mCamera.startPreview();
 }

@Override
 public void surfaceDestroyed(SurfaceHolder holder) {

 mCamera.stopPreview();
 mCamera.release();

 }

 }

}

Advertisements

About Xiang 'Anthony' Chen

Making an Impact in Your Life

Discussion

2 thoughts on “Four Steps to Start Using Camera in Your Android Application

  1. Hey, your tutorial very useful but it can’t work for me. when i run with my device(or emulator) it display “the application has stopped unexpectedly” if your code has updated please tell me. sorry for my bad english. thanks.

    Posted by jannii | November 7, 2012, 6:33 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Twitter Updates

%d bloggers like this: