camera.release()不会释放相机以供下次使用

时间:2017-03-01 19:38:34

标签: android

我想拍摄两张图片,一张是正面照片,一张是带有背面照相机,只需按一下按钮。我写的代码抛出java.lang.RuntimeException: Fail to connect to camera service,我知道当相机已经被使用时我们会抛出这个异常并且我们会在发布之前尝试使用它,但是在我的代码中可以清楚地看到它在拍摄第二张照片之前我是使用以下三行代码释放相机:camera.stopPreview();camera.release();以及camera = null;。所以问题是如果我在第二次使用之前释放相机,那么为什么抛出这个异常呢?请帮忙。

代码:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.os.Environment;
import android.view.SurfaceView;
import java.io.File;
import java.io.FileOutputStream;


public class PhotoCapture {

    static Camera camera = null;
    static int pic_number = 0;

    public static void takeSnapShots(Context context, int face) {

        SurfaceView surface = new SurfaceView(context);
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();

        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == face) {
                try {
                    camera = Camera.open(camIdx);
                    camera.setPreviewDisplay(surface.getHolder());
                    camera.startPreview();
                    camera.takePicture(null, null, jpegCallback);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    break;
                }
            }
        }
    }

    private static Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator+"picure"+(++pic_number)+".jpg";
            File pictureFile = new File(path);

            try {
                FileOutputStream outputStream = new FileOutputStream(pictureFile);
                Bitmap realImage = BitmapFactory.decodeByteArray(data, 0, data.length);
                Matrix mtx = new Matrix();
                mtx.setRotate(90);
                realImage = Bitmap.createBitmap(realImage, 0, 0, realImage.getWidth(), realImage.getHeight(), mtx, true);
                realImage.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                camera.stopPreview();
                camera.release();
                camera = null;
            }
        }
    };
}

按钮点击:

public void onClick(View v) {
    PhotoCapture.takeSnapShots(this, Camera.CameraInfo.CAMERA_FACING_BACK);
    PhotoCapture.takeSnapShots(this, Camera.CameraInfo.CAMERA_FACING_FRONT);
}

1 个答案:

答案 0 :(得分:0)

尝试以下:

      public static void takeSnapShots(Context context, int face) {

        SurfaceView surface = new SurfaceView(context);
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        int cameraCount = Camera.getNumberOfCameras();

        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == face) {
                try {
                    camera = Camera.open(camIdx);
                    camera.setPreviewDisplay(surface.getHolder());
                    camera.startPreview();
                    camera.takePicture(null, null, jpegCallback);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    camera.release();
                }
            }
        }
    }