每当我尝试调用Camera.release()都不在代码中工作时

时间:2018-08-12 08:10:58

标签: android nullpointerexception camera

我有这段代码,它在第一次获取参数,但是当我尝试恢复活动或再次使用意图启动活动时,应用程序崩溃了。 我曾尝试在代码的不同部分发布相机,但似乎无法正常工作。 这是更改表面的代码

    private SurfaceHolder.Callback surfaceCallback = new SurfaceHolder.Callback() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {
                mCamera.setPreviewDisplay(previewHolder);
                mCamera.setPreviewCallback(previewCallback);
            } catch (Throwable t) {
                Log.e("MotionDetector", "Exception in setPreviewDisplay()", t);
            }
//            try {
//                if(!mInitSuccesful)
//                    initRecorder(previewHolder.getSurface());
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {


            Camera.Parameters parameters = mCamera.getParameters();
            List<String> focusModes = parameters.getSupportedFocusModes();
            if (focusModes.contains(parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
                parameters.setFocusMode(parameters.FOCUS_MODE_CONTINUOUS_PICTURE);

            Camera.Size size = getBestPreviewSize(width, height, parameters);
            if (size != null) {
                parameters.setPreviewSize(size.width, size.height);
                Log.d("MotionDetector", "Using width=" + size.width + " height=" + size.height);
            }
            mCamera.setDisplayOrientation(90);
            mCamera.setParameters(parameters);
            mCamera.startPreview();


            inPreview = true;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // Ignore
            mCamera.stopPreview();
            releaseCamera();
        }

我有这个代码。在表面上被破坏了,我叫了“停止预览并释放相机”,但仍然出现标题中提到的错误。 注意:第一次运行平稳,但是如果我按回去并再次打开活动,则会出现错误

该代码中似乎有错误

    @Override
        public void run() {
            while (isRunning.get()) {
                long now = System.currentTimeMillis();
                if (now - lastCheck > checkInterval) {
                    lastCheck = now;

                    if (nextData.get() != null) {
                        int[] img = ImageProcessing.decodeYUV420SPtoLuma(nextData.get(), nextWidth.get(), nextHeight.get());

                        // check if it is too dark
                        int lumaSum = 0;
                        for (int i : img) {
                            lumaSum += i;
                        }
                        if (lumaSum < minLuma) {
                            if (motionDetectorCallback != null) {
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                        motionDetectorCallback.onTooDark();
                                    }
                                });
                            }
                        } else if (detector.detect(img, nextWidth.get(), nextHeight.get())) {
                            // check
                            if (motionDetectorCallback != null) {
                                mHandler.post(new Runnable() {
                                    @Override
                                    public void run() {

                                            Camera.Parameters params = imgCam.getParameters();
                                            int w = params.getPreviewSize().width;
                                            int h = params.getPreviewSize().height;
                                            int format = params.getPreviewFormat();
                                            YuvImage image = new YuvImage(imgByte, format, w, h, null);

                                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                                            Rect area = new Rect(0, 0, w, h);
                                            image.compressToJpeg(area, 100, out);
                                            Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());


                                            float degrees = 90;//rotation degree
                                            Matrix matrix = new Matrix();
                                            matrix.setRotate(degrees);
                                            bOutput = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);

//                                        mMediaRecorder.start();
//                                        try {
//                                            Thread.sleep(10 * 1000); // This will recode for 10 seconds, if you don't want then just remove it.
//                                        } catch (Exception e) {
//                                            e.printStackTrace();
//                                        }
//                                        getOutputMediaFile(1);
                                            motionDetectorCallback.onMotionDetected();

                                    }
                                });
                            }
                        }
                    }
                }
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

使用此释放摄像头

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mcamera.stopPreview();
    mcamera.release();
}

使用此在创建方法后在主要活动中恢复摄像机预览

@Override
public void onResume() {
    super.onResume();
    mCamera = Camera.open();
    mCamera.startPreview();
}

surfaceChanged()与无关。