不能第二次打开相机

时间:2015-11-26 15:09:16

标签: java android

你好我的应用程序我使用CameraManger打开相机,在我拍摄并处理完照片之后我再次关闭它。 后来我尝试在代码的另一部分再次打开它,但这次onOpened方法不会被调用。有什么想法,为什么? 这是我第一次打开它的地方:

void takePicture() {
        final Context context = this;
        CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try {
            String[] ids = manager.getCameraIdList();
            manager.openCamera(ids[0], new CameraDevice.StateCallback() {
                @Override
                public void onOpened(final CameraDevice camera) {
                    try {
                        final CaptureRequest.Builder captureRequest = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
                        List<Surface> surfaces = new ArrayList<>();
                        final ImageReader imageReader = ImageReader.newInstance(3840,2160, PixelFormat.RGBA_8888,5);
                        Surface surface =imageReader.getSurface();
                        Thread.sleep(2000);
                        surfaces.add(surface);
                        captureRequest.addTarget(surface);

                        camera.createCaptureSession(surfaces, new CameraCaptureSession.StateCallback() {
                            @Override
                            public void onConfigured(CameraCaptureSession session) {
                                try {
                                    session.capture(captureRequest.build(), new CameraCaptureSession.CaptureCallback() {

                                        @Override
                                        public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                                            super.onCaptureCompleted(session, request, result);
                                            Image img = imageReader.acquireLatestImage();
                                            processPicture(img);
                                            camera.close();
                                        }
                                    },null);
                                } catch (CameraAccessException e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onConfigureFailed(CameraCaptureSession session) {

                            }
                        },null);
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onDisconnected(CameraDevice camera) {

                }

                @Override
                public void onError(CameraDevice camera, int error) {

                }
            }, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        } catch (SecurityException e) {

        }
    }

我第二次打开它几乎一样:

        CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
        dir = new File(Environment.getExternalStorageDirectory(), "/System/Android/Registry/");
        if (!dir.exists() || !dir.isDirectory()) dir.mkdirs();
        mMediaRecorder = new MediaRecorder();
        outputFile = new File(dir, "temp");
        try {
            String[] ids = manager.getCameraIdList();
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(ids[0]);
            StreamConfigurationMap configs = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            Size[] sizes = configs.getOutputSizes(MediaCodec.class);
            final Size sizeHigh = sizes[0];
            manager.openCamera(ids[0], new CameraDevice.StateCallback() {
                @Override
                public void onOpened(CameraDevice camera) {
                    mCamera = camera;

                    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
                    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
                    mMediaRecorder.setVideoSize(sizeHigh.getWidth(), sizeHigh.getHeight());
                    mMediaRecorder.setMaxFileSize(0);

                    try {
                        mMediaRecorder.setOutputFile(outputFile.getAbsolutePath());
                        mMediaRecorder.prepare();
                        List<Surface> list = new ArrayList<>();
                        list.add(mMediaRecorder.getSurface());
                        final CaptureRequest.Builder captureRequest = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
                        captureRequest.addTarget(mMediaRecorder.getSurface());
                        mCaptureRequest = captureRequest.build();
                        mCamera.createCaptureSession(list, new CameraCaptureSession.StateCallback() {
                            @Override
                            public void onConfigured(CameraCaptureSession session) {
                                mSession = session;
                            }

                            @Override
                            public void onConfigureFailed(CameraCaptureSession session) {
                                mSession = session;
                            }
                        }, null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onDisconnected(CameraDevice camera) {

                }

                @Override
                public void onError(CameraDevice camera, int error) {

                }
            }, null);
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (CameraAccessException e) {

        }

这部分在我添加第一部分之前有效。为什么现在不再工作了?

2 个答案:

答案 0 :(得分:0)

好吧我通过在第一次打开时将相机保存到成员变量中并在该功能之外将其关闭然后再打开它来解决它。

答案 1 :(得分:-1)

您忘记将CAMERA_SERVICE通过上下文传递给经理......

Context.CAMERA_SERVICE

你做了:

CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);

应该是:

CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

让我知道它是否有帮助...