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

最佳答案

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());
              }

   }
};

关于android - 如何在没有用户界面的情况下在android应用程序中拍照?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17859777/

10-10 22:34