其他分享
首页 > 其他分享> > android – 会话已关闭;进一步的变化是非法的

android – 会话已关闭;进一步的变化是非法的

作者:互联网

我正在阅读有关此问题的许多报道,但仍无法找到答案.
有时,在使用camera2捕获之后,会抛出异常:java.lang.IllegalStateException:会话已关闭;进一步的变化是非法的
我试图检查所有会话不是null,什么时候做某事,但仍然得到这个.
有什么建议吗?我应该处理什么?=,我没有?

 private CameraCaptureSession.CaptureCallback mCaptureCallback
            = new CameraCaptureSession.CaptureCallback() {

        private void process(CaptureResult result) {
            switch (mState) {
                case STATE_PREVIEW: {

                    if(touchFocusEnabled) {
                        letSetCaptureSessionOnce = true;
                        mState = STATE_WAITING_LOCK;
                        try {
                            // Reset the auto-focus trigger
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                                    CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
                            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
                                    CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

                            mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                                    mHandler);
                        } catch (CameraAccessException e) {
                            L.e("CameraLolipop --- CameraCaptureSession.CaptureCallback " + e);
                        }
                    } else {
                        if(letSetCaptureSessionOnce) {
                            try {
                                if ((null != mCaptureSession) && (isCameraOpen)) {
                                    mState = STATE_PREVIEW;
                                    mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                                            mHandler);
                                    letSetCaptureSessionOnce = true;
                                }

                            } catch (CameraAccessException e) {
                                L.e("CameraLolipop --- CameraCaptureSession.CaptureCallback " + e);
                            }
                        }
                    }
                    break;
                }
                case STATE_WAITING_LOCK: {
                    if(touchFocusEnabled) {
                        mState = STATE_PICTURE_TAKEN;
                        touchFocusEnabled = false;

                        try {
                            if((mCaptureSession != null)  && (isCameraOpen)){
                                mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
                                        mHandler);
                            }
                        } catch (CameraAccessException e) {
                            L.e("CameraLolipop --- STATE_WAITING_LOCK " + e);
                        }

                        return;
                    }
                    Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
                    L.d("Focus state ", "STATE_WAITING_LOCK");
                    boolean fixedFocus = isFixedFocus();
                    if (afState == null) {
//                        if ((burstMode) && (getSupportedHardwareLevel() == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL)) {
//                            captureStillPictureBurst();
//                        } else {
                            captureStillPicture();
//                        }
                    } else if ((CaptureResult.CONTROL_AF_STATE_FOCUSED_LOCKED == afState ||
                            CaptureResult.CONTROL_AF_STATE_NOT_FOCUSED_LOCKED == afState) || fixedFocus) {
                        L.d("Focus state ", "CONTROL_AF_STATE_FOCUSED_LOCKED or CONTROL_AF_STATE_NOT_FOCUSED_LOCKED");
                        // CONTROL_AE_STATE can be null on some devices
                        Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                        if (aeState == null ||
                                aeState == CaptureResult.CONTROL_AE_STATE_CONVERGED) {
                            mState = STATE_PICTURE_TAKEN;
                            if ((burstMode) && (getSupportedHardwareLevel() == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL)) {
//                                captureStillPictureBurst();
                                captureStillPicture();
                            } else {
                                captureStillPicture();
                            }
                        } else {
                            runPrecaptureSequence();
                        }
                    }
                    break;
                }
                case STATE_WAITING_PRECAPTURE: {
                    L.d("Focus state ", "STATE_WAITING_PRECAPTURE");
                    // CONTROL_AE_STATE can be null on some devices
                    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                    if (aeState == null ||
                            aeState == CaptureResult.CONTROL_AE_STATE_PRECAPTURE ||
                            aeState == CaptureRequest.CONTROL_AE_STATE_FLASH_REQUIRED || isFixedFocus()) {
                        mState = STATE_WAITING_NON_PRECAPTURE;
                    }

                    break;
                }
                case STATE_WAITING_NON_PRECAPTURE: {
                    L.d("Focus state ", "STATE_WAITING_NON_PRECAPTURE");
                    // CONTROL_AE_STATE can be null on some devices
                    Integer aeState = result.get(CaptureResult.CONTROL_AE_STATE);
                    if (aeState == null || aeState != CaptureResult.CONTROL_AE_STATE_PRECAPTURE) {
                        mState = STATE_PICTURE_TAKEN;
                        if ((burstMode) && (getSupportedHardwareLevel() == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_FULL)) {
//                            captureStillPictureBurst();
                            captureStillPicture();
                        } else {
                            captureStillPicture();
                        }
                    }
                    break;
                }

                case STATE_PICTURE_TAKEN: {
                    Integer afState = result.get(CaptureResult.CONTROL_AF_STATE);
                    L.d("TOUCH", " afState " + afState);
                    mState = STATE_PREVIEW;
                    if(afState == 4) {
                        mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
                    }

                    if(afState == 0) {
                            Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    mPreviewRequestBuilder
                                            .set(CaptureRequest.CONTROL_AF_REGIONS, null);
                                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                                            CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

                                }
                            }, 2500);
                    }
                    break;
                }
            }
        }
@Override
        public void onCaptureProgressed(CameraCaptureSession session,
                                        CaptureRequest request,
                                        CaptureResult partialResult) {
            super.onCaptureProgressed(session, request, partialResult);
//            process(partialResult);
        }

        @Override
        public void onCaptureCompleted(CameraCaptureSession session,
                                       CaptureRequest request,
                                       TotalCaptureResult result) {
//            if ((mState != STATE_PREVIEW) || (touchFocusEnabled)) {
                super.onCaptureCompleted(session, request, result);
                if(session != null) {
                    process(result);
                }
        }
    };

这里是创建会话的地方:

 private CameraCaptureSession.StateCallback mSessionPreviewStateCallback = new CameraCaptureSession.StateCallback() {
    @Override
    public void onConfigured(CameraCaptureSession cameraCaptureSession) {
        L.i("Thread", "onConfigured---->" + Thread.currentThread().getName());
        // The camera is already closed
        if(null == mCameraDevice) {
            return;
        }
        try {
            mCaptureSession = cameraCaptureSession;
            mCameraSessionIsClosed = false;
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

            if((isCameraOpen) && (mCaptureSession != null)){
                mPreviewRequest = mPreviewRequestBuilder.build();
                mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mHandler);
            }
            L.d("ex0003", "Test... mSessionPreviewStateCallback ");
        } catch (CameraAccessException e) {
            L.e("CameraLolipop --- openCamera() " + e);
        }
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
        Toast.makeText(getActivity(), "onConfigureFailed---Preview", Toast.LENGTH_SHORT).show();
    }
};

并在onPause中调用closeCamera():

try {
        if(mCameraDevice == null && mCameraIsClosed) {
            return;
        }
        if((mCaptureSession != null) && (isCameraOpen)) {
            try {
                mCaptureSession.abortCaptures();
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }
        }
        mCameraOpenCloseLock.acquire();
        if (null != mCaptureSession) {
            mCaptureSession.close();
            mCaptureSession = null;
            mCameraSessionIsClosed = true;
        }
        if (null != mCameraDevice) {
            mCameraDevice.close();
            mCameraDevice = null;
            isCameraOpen = false;
            mCameraIsClosed = true;
        }
        if (null != mImageReader) {
            mImageReader.close();
            mImageReader = null;
        }
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
    } finally {
        mCameraOpenCloseLock.release();
    }

并且在调用stopBackgroundThread()和super.onPause()之后.

解决方法:

我不确定这是否有效,但请尝试一下,让我知道它是否有效.

private CameraCaptureSession.StateCallback mSessionPreviewStateCallback = new CameraCaptureSession.StateCallback() {
    @Override
    public void onConfigured(CameraCaptureSession cameraCaptureSession) {
        L.i("Thread", "onConfigured---->" + Thread.currentThread().getName());
        // The camera is already closed
        if(null == mCameraDevice) {
            return;
        }
        try {
            mCaptureSession = cameraCaptureSession;
            mCameraSessionIsClosed = false;
            mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
                    CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);

            if((isCameraOpen) && (mCaptureSession != null)){
                mPreviewRequest = mPreviewRequestBuilder.build();
                mCaptureSession.setRepeatingRequest(mPreviewRequest, mCaptureCallback, mHandler);
            }
            L.d("ex0003", "Test... mSessionPreviewStateCallback ");
        } catch (CameraAccessException e) {
            Log.e(TAG, "Failed to start camera preview because it couldn't access camera", e);
        } catch (IllegalStateException e) {
            Log.e(TAG, "Failed to start camera preview.", e);
        }
    }

    @Override
    public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
        Toast.makeText(getActivity(), "onConfigureFailed---Preview", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClosed(@NonNull CameraCaptureSession session) {
        if (mCaptureSession != null && mCaptureSession.equals(session)) {
            mCaptureSession = null;
        }
    }
};

标签:android,camera,android-camera2
来源: https://codeday.me/bug/20190711/1429491.html