//interface to the display surface SurfaceHolder mHolder;//static instance for the camerapublic Camera camera;

Preview(Context context) {super(context);

// Install a SurfaceHolder.Callback so we get notified when the// underlying surface is created and destroyed.mHolder = getHolder();mHolder.addCallback(this);mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);}

try {//set the camera display to the holdercamera.setPreviewDisplay(holder);camera.startPreview();

} catch (IOException e) {e.printStackTrace();}}

//happen when we destroyed the surfacepublic void surfaceDestroyed(SurfaceHolder holder) { if (camera!=null){
camera.stopPreview();//free the resources
camera.release();}}public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {//when the surface size changed, first happen SurfaceDestroyed//so we need to reconnect to the camera to the surface//getting the parameters from the camera and //change the width and the height of the camera

public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//create new instance to the surfacepreview = new Preview(this);//set the surface to the xml in the surface((FrameLayout) findViewById(R.id.preview)).addView(preview);