简体   繁体   中英

Camera.release() is not working in the code whenever i tried to call it

I have this code in which it gets the parameters the first time but when I try to resume the activity or again start the activity using intent the app crashes. I have tried to release the camera in different parts of my code but it seems to be not working. here is the code for surface changed

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

I have this code. in on surface destroyed I called the stop preview and release camera but keep getting the error as mentioned in title. Note: First time it runs smooth but if i press back and open activity again the error arises

Error seems to be in this code

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

Use this TO RELEASE CAMERA :

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

Use this TO RESUME CAMERA PREVIEW IN MAIN ACTIVITY AFTER onCreate METHOD :

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

surfaceChanged() has nothing to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM