托管相机服务的进程意外终止

Process hosting the camera service has died unexpectedly

我已经尝试了所有方法,但我找不到我的相机应用向我抛出死服务异常的原因。

情况是这样的。我正在使用 HDR jni 库,我已经检查过它并且工作正常,它不是本机内存的内存引导,也不是 jni 问题。所以,问题一定出在我的代码中:

我只是在等待 CaptureResult return 我 AE_CONVERGED_STATE 检查传感器是否已经正确曝光,然后我调用我的方法:

    Log.performanceEnd("YUV capture");
        Log.d(TAG, "[onImageAvailable] YUV capture, mBurstCount: " + mBurstCount);
        Image image = imageReader.acquireNextImage();
        if (mBackgroundHandler != null) {
            mBackgroundHandler.post(new YuvCopy(image, mBurstCount));
        }
        mBurstCount++;

        if (mBurstState == BURST_STATE_HDR) {
            switch (mBurstCount) {
                case 1:
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, HDR_EXPOSURE_COMPENSATION_VALUE_HIGH);
                    break;
                case 2:
                    mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_EXPOSURE_COMPENSATION, HDR_EXPOSURE_COMPENSATION_VALUE_LOW);
                    break;
                case 3:
                    //Restore exposure compensation value
                    mCaptureCallback = mPhotoCaptureCallback;
                    mSettingsManager.setExposureCompensation(mPreviewRequestBuilder);
                    mActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            onPictureCaptured();
                        }
                    });
                    unlockFocus();
                    break;
            }
            if (mBurstCount != 3) {
                updatePreviewSession();
            }
            //Finish HDR session
            if (mBurstCount < YUV_BURST_LIMIT) mHdrState = STATE_PICTURE_TAKEN;
        }

这是我的 YUV 方法:

 /**
 * Transform YUV420 to NV21 readable frames
 */
private class YuvCopy implements Runnable {
    private final Image mImage;
    private final int mPictureIndex;

    public YuvCopy(Image image, int index) {
        mImage = image;
        mPictureIndex = index;
    }

    @Override
    public void run() {
        if (mImage != null) {
            if (mImage.getWidth() * mImage.getHeight() > 0) {
                Image.Plane[] planes = mImage.getPlanes();                   

                long startCopy = System.currentTimeMillis();

                int width = mImage.getWidth();
                int height = mImage.getHeight();
                int ySize = width * height;
                ByteBuffer yBuffer = mImage.getPlanes()[0].getBuffer();
                ByteBuffer uvBuffer = mImage.getPlanes()[1].getBuffer();
                ByteBuffer vuBuffer = mImage.getPlanes()[2].getBuffer();
                byte[] mData = new byte[ySize + (ySize / 2)];
                yBuffer.get(mData, 0, ySize);
                vuBuffer.get(mData, ySize, (ySize / 2) - 1);
                mData[mData.length - 1] = uvBuffer.get(uvBuffer.capacity() - 1);
                mImage.close();

                mHdrCaptureArray[mPictureIndex] = mData;

                Log.i(TAG, "[YuvCopy|run] Time to Copy data: " + (System.currentTimeMillis() - startCopy) + "ms");

                if (mPictureIndex == YUV_BURST_LIMIT - 1) {
                    startHdrProcessing();

                } else {
                    mImage.close();
                }

            }
        }
    }

我一共挑选了三张照片,然后我调用了我的 JNI 库的合并方法。我试图评论所有 jni 代码,但它仍然发生,所以我认为问题可能一定出在这里,在我的 YUV 方法中,或者可能在 Burst HDR 调用中。

最后这是我发生的日志错误:

01-01 12:30:27.531 21945-21957/com.myCamera W/AudioSystem: AudioFlinger server died!
01-01 12:30:27.532 21945-22038/com.myCamera W/AudioSystem: AudioPolicyService server died!
1-01 12:30:27.903 21945-21978/com.myCamera I/CameraManagerGlobal: Connecting to camera service
01-01 12:30:27.903 21945-21978/com.myCamera E/CameraManagerGlobal: Camera service is unavailable
01-01 12:30:27.903 21945-21978/com.myCamera W/System.err: android.hardware.camera2.CameraAccessException: Camera service is currently unavailable
01-01 12:30:29.103 21945-21945/com.myCamera W/System.err: android.hardware.camera2.CameraAccessException: Process hosting the camera service has died unexpectedly

有时只拍2张照片,有时拍300张照片,但最终还是发生了。此外,很多时候我所有的设备都快没电了,但一切正常,所以我需要重启我的 phone.

最终导致问题的原因是我的 ImageReader 配置错误,根据 Phone 的硬件级别,相机可以允许不同类型的 imageReader,每个图像读取器的大小都不同。 例如,INFO_SUPPORTED_HARDWARE_LEVEL == FULL 不支持配置为设备最大尺寸的 JPEG 图像 reader 和另一个 YUV 格式的图像超过当时的预览尺寸。反正有时候可以,有时候不行。

If an application tries to create a session using a set of targets that exceed the limits described in the below tables, one of three possibilities may occur. First, the session may be successfully created and work normally. Second, the session may be successfully created, but the camera device won't meet the frame rate guarantees as described in getOutputMinFrameDuration(int, Size). Or third, if the output set cannot be used at all, session creation will fail entirely, with onConfigureFailed(CameraCaptureSession) being invoked.

引用自:https://developer.android.com/reference/android/hardware/camera2/CameraDevice.html

这意味着我的设备无法将 YUV 图像 reader 配置为 4608x3456 大小,而我的 JPEG imageReader 也配置为相同大小。它只能支持我的预览尺寸(1920x1080)。您可以在 link.

中检查所有可能的配置