Friday, June 4, 2010

Opening Camera from Application

The basic things for augmented reality are camera, Sensor to know the direction, and the GPS or network provider to get the current location details. Gradually, I will show you the Augmented Reality in Android. In this, showing you how to open a camera from application.

CameraPreview.java


 
class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder holder;
Camera camera;

public CameraPreview(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder holder2, int format, int w,
int h) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
camera.setParameters(parameters);
camera.startPreview();
}

@Override
public void surfaceCreated(SurfaceHolder holder1) {
try {
camera = Camera.open();
camera.setPreviewDisplay(holder1);
} catch (Exception e) {
Log.i("Exception surfaceCreated()", "e = " + e);
camera.release();
camera = null;
}

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
camera.stopPreview();
camera.release();
camera = null;
}
}



Create an object of CameraPreview and using setContentView(), just display the camera

OpenCamera.java

 
public class OpenCamera extends Activity {

CameraPreview preview;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
preview = new CameraPreview(this);
setContentView(preview);
}
}


And ofcourse, the permission is needed,


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

No comments:

Post a Comment

Androidians