2012年4月8日星期日

[Android] Using camera to capture pictures.

To prepare the surface:
  this.surface = (SurfaceView)findViewById(R.id.yourSurface);
  this.surfaceHolder = this.surface.getHolder();
  this.surfaceHolder.addCallback(this);
  this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

To start the camera:
  this.camera = Camera.open()
  this.camera.setPreviewDisplay(this.surfaceHolder);
  Camera.parameters parameters = this.camera.getParameters();
  parameters.getPreviewSize(x, y);
  this.camera.startPreview();

To reset or pause the camera:
  this.camera.stopPreview();
  this.camera.release();

To take a picture:
  this.camera.takePicture(shutterCallback, rawCallback, jpegCallback);

To check SD card inside:
  android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);

To save picture:
  Bitmap bm = BitmapFactory.decodeByteArray(_data, 0, _data.length);
  File myCaptureFile = new File(strCaptureFilePath);
  BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
  bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
  bos.flush();
  bos.close();