我现在正在使用camera2Basic并尝试获取每个帧数据来进行一些图像处理。我在Android5.0中使用camera2 API,仅在进行相机预览时一切都很好,而且很流畅。但是,当我使用ImageReader.OnImageAvailableListener回调获取每个帧数据时,预览卡住了,这会导致不良的用户体验。
以下是我的相关代码:

这是相机和ImageReader的设置,我将图像的格式设置为YUV_420_888

public<T> Size setUpCameraOutputs(CameraManager cameraManager,Class<T> kClass, int width, int height) {
    boolean flagSuccess = true;
    try {
        for (String cameraId : cameraManager.getCameraIdList()) {
            CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
            // choose the front or back camera
            if (FLAG_CAMERA.BACK_CAMERA == mChosenCamera &&
                    CameraCharacteristics.LENS_FACING_BACK != characteristics.get(CameraCharacteristics.LENS_FACING)) {
                continue;
            }
            if (FLAG_CAMERA.FRONT_CAMERA == mChosenCamera &&
                    CameraCharacteristics.LENS_FACING_FRONT != characteristics.get(CameraCharacteristics.LENS_FACING)) {
                continue;
            }
            StreamConfigurationMap map = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

            Size largestSize = Collections.max(
                    Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888)),
                    new CompareSizesByArea());

            mImageReader = ImageReader.newInstance(largestSize.getWidth(), largestSize.getHeight(),
                    ImageFormat.YUV_420_888, 3);

            mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);
            ...
            mCameraId = cameraId;
       }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {

    }
    ......
}

成功打开相机后,我将创建一个CameraCaptureSession进行相机预览
private void createCameraPreviewSession() {
    if (null == mTexture) {
        return;
    }

    // We configure the size of default buffer to be the size of camera preview we want.
    mTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

    // This is the output Surface we need to start preview
    Surface surface = new Surface(mTexture);

    // We set up a CaptureRequest.Builder with the output Surface.
    try {
        mPreviewRequestBuilder =
                mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewRequestBuilder.addTarget(mImageReader.getSurface());
        mPreviewRequestBuilder.addTarget(surface);

        // We create a CameraCaptureSession for camera preview
        mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onConfigured(CameraCaptureSession session) {
                        if (null == mCameraDevice) {
                            return;
                        }

                        // when the session is ready, we start displaying the preview
                        mCaptureSession = session;

                        // Finally, we start displaying the camera preview
                        mPreviewRequest = mPreviewRequestBuilder.build();
                        try {
                            mCaptureSession.setRepeatingRequest(mPreviewRequest,
                                    mCaptureCallback, mBackgroundHandler);
                        } catch (CameraAccessException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession session) {

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

最后是ImageReader.OnImageAvailableListener回调
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Log.d(TAG, "The onImageAvailable thread id: " + Thread.currentThread().getId());
                Image readImage = reader.acquireLatestImage();
                readImage.close();
            }
        };

也许我设置错误,但尝试了几次却无法正常工作。也许还有另一种获取帧数据的方法,而不是ImageReader,但我不知道。
有人知道如何实时获取每个帧的数据吗?

最佳答案

我不相信陈是正确的。图像格式对我测试过的设备的速度几乎有零影响。相反,问题似乎出在图像尺寸上。在图像格式为YUV_420_888的Xperia Z3 Compact上,我在StreamConfigurationMapgetOutputSizes方法中提供了很多不同的选项:

[1600x1200, 1280x720, 960x720, 720x480, 640x480, 480x320, 320x240, 176x144]

对于这些各自的大小,将mImageReader.getSurface()设置为mPreviewRequestBuilder的目标时获得的最大fps是:
[13, 18, 25, 28, 30, 30, 30, 30 ]

因此,一种解决方案是使用较低的分辨率来达到所需的速率。出于好奇...请注意,这些时间似乎不受生产线的影响
    mPreviewRequestBuilder.addTarget(surface);
...
    mCameraDevice.createCaptureSession(Arrays.asList(surface, mImageReader.getSurface()),

我担心在屏幕上添加表面可能会增加开销,但是如果我删除第一行并将第二行更改为
    mCameraDevice.createCaptureSession(Arrays.asList(mImageReader.getSurface()),

然后我看到时间变化少于1 fps。因此,是否还在屏幕上显示图像似乎并不重要。

我认为,camera2 API或ImageReader的框架中仅存在一些开销,使得无法获得TextureView明显获得的全部速率。

最令人失望的事情之一是,如果切换回已弃用的Camera API,则可以通过PreviewCallback方法设置Camera.setPreviewCallbackWithBuffer来轻松获得30 fps。使用这种方法,无论分辨率如何,我都能获得30fps。具体来说,尽管它不直接为我提供1600x1200,但它确实提供了1920x1080,甚至是30fps。

10-07 19:26