如何在没有用户界面的情况下在Android应用程序中拍照..?

时间:2013-07-25 13:43:21

标签: android camera

我必须设计一个应用程序,该应用程序必须从相机拍摄照片而不在布局中显示它并且没有任何用户界面(如点击按钮......)并存储图片?

1 个答案:

答案 0 :(得分:3)

Camera mCamera; 
private boolean safeCameraOpen(int id) {
boolean qOpened = false;

    try {
        releaseCamera();
        mCamera = Camera.open(id);
        qOpened = (mCamera != null);
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    return qOpened;    
}

private void releaseCamera() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

//somewhere in your code call this:
mCamera.takePicture(null, null, mCall);


Camera.PictureCallback mCall = new Camera.PictureCallback() {

   public void onPictureTaken(byte[] data, Camera camera) {
         //decode the data obtained by the camera into a Bitmap

         FileOutputStream outStream = null;
              try {
                  outStream = new FileOutputStream("/sdcard/Image.jpg");
                  outStream.write(data);
                  outStream.close();
              } catch (FileNotFoundException e){
                  Log.d("CAMERA", e.getMessage());
              } catch (IOException e){
                  Log.d("CAMERA", e.getMessage());
              }

   }
};