take Picture using Camera class - Android android.hardware

Android examples for android.hardware:Camera

Description

take Picture using Camera class

Demo Code

import java.io.File;
import java.io.FileOutputStream;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;

public class Main {

  public static void takePicture(final String fileName) {
    Camera camera = null;//from w w w  .  j  a va  2 s  . c om
    try {
      int camIdx = 1; // front camera
      Camera.getCameraInfo(camIdx, new Camera.CameraInfo());
      camera = Camera.open(camIdx);

      camera.setPreviewTexture(new SurfaceTexture(0));
      camera.startPreview();
      camera.takePicture(null, null, new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
          saveFile(data, fileName);
          camera.release();
        }
      });
    } catch (Exception e) {
      if (camera != null) {
        camera.release();
      }
    }
  }

  private static final File DESTINY_DIR = new File("/sdcard/unlocksecurity/");

  public static void saveFile(byte[] data, String fileName) throws RuntimeException {
    if (!DESTINY_DIR.exists() && !DESTINY_DIR.mkdirs()) {
      return;
    }
    File mainPicture = new File(DESTINY_DIR, fileName);
    try {
      FileOutputStream fos = new FileOutputStream(mainPicture);
      fos.write(data);
      fos.close();
    } catch (Exception e) {
      throw new RuntimeException("Image could not be saved.", e);
    }
  }

}

Related Tutorials