简体   繁体   English

如何使用 Android Camera2 API 访问 ImageReader 队列中的所有图像

[英]How to access all Images in the ImageReader Queue using Android Camera2 API

In my project, I need to capture the frames of the camera streams continuously.在我的项目中,我需要连续捕捉摄像机流的帧。 Here is the current code snippet I used.这是我使用的当前代码片段。

To set up the ImageReader, I set the maxImages to 20. Let is every time when callback is triggered, there would have 20 frames in the ImageReader Queue.为了设置 ImageReader,我将maxImages设置为 20。假设每次触发回调时,ImageReader 队列中都会有 20 帧。

imageReader = ImageReader.newInstance(
            optimumSize.getWidth(),
            optimumSize.getHeight(),
            ImageFormat.YUV_420_888,
            20
);

Then to access each image of these 20 frames.然后访问这 20 帧的每个图像。 I used the following snippet.我使用了以下代码段。

imageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
    @Override
    public void onImageAvailable(ImageReader reader) {
        Image image = reader.acquireNextImage();
        while (image != null) {
            // some processing here.....
            image.close();
            image = reader.acquireNextImage();
        }
        if (image != null) {
            image.close();
        }
    }
}, processingHandler);

The key obstacle here is to be able to access each of 20 frames in a callback, for further image processing.这里的主要障碍是能够在回调中访问 20 帧中的每一帧,以进行进一步的图像处理。 However the aforementioned code seems have some problems (I can only access the latest image in the underlying queue).然而,上述代码似乎有一些问题(我只能访问底层队列中的最新图像)。 In fact, I only need to access a small patch (50 x 50 pixels) in each frames, specified by users.事实上,我只需要访问用户指定的每一帧中的一个小补丁(50 x 50 像素)。

The reason for doing this is that I need to get the 20 continuous frames data with sampling frequency being ~60Hz.这样做的原因是我需要获取采样频率为 ~60Hz 的 20 个连续帧数据。 This seems really hard to achieve if we can only access single frame in each callback, which can only achieve up to 30fps.如果我们只能在每个回调中访问单帧,这似乎很难实现,最多只能达到 30fps。

Any suggestions would be super welcome!任何建议都会非常受欢迎! Thanks!谢谢!

Setting maxImages to 20 just means the queue will allow you to acquire 20 Images at the same time;将 maxImages 设置为 20 只是意味着队列将允许您同时获取 20 个图像; it does not mean the onImageAvailable callback will fire only once 20 images are queued.这并不意味着 onImageAvailable 回调只会在 20 个图像排队时触发。 The callback fires as soon as a single image is present.只要存在单个图像,回调就会触发。

Most camera devices run at 30fps max, so it's not surprising that's the speed you're seeing.大多数相机设备最高运行速度为 30fps,因此您所看到的速度也就不足为奇了。 Some cameras do have 60fps modes, but you have to explicitly switch to a CONTROL_AE_TARGET_FPS_RANGE of (60,60) to get that, and that's only if the device's CONTROL_AE_AVAILABLE_TARGET_FPS_RANGE values include that range.有些相机确实有 60fps 模式,但您必须显式切换到 (60,60) 的 CONTROL_AE_TARGET_FPS_RANGE 才能获得该模式,并且只有当设备的 CONTROL_AE_AVAILABLE_TARGET_FPS_RANGE 值包含该范围时。

60fps may also be resolution-limited (check the StreamConfigurationMap for minimum frame durations to find what resolutions can support 60fps, if you want to double-check). 60fps 也可能受到分辨率限制(如果您想仔细检查,请查看 StreamConfigurationMap 以了解最短帧持续时间以找到可以支持 60fps 的分辨率)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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