Camera2 可以连续访问相机预览图像 [英] Camera2 get continuous access to camera preview images

查看:62
本文介绍了Camera2 可以连续访问相机预览图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据 API 将应用程序从 Camera1 扩展到 Camera2.该应用程序的一个核心机制是以每秒约 20 张图片的速度拍摄预览图片.使用 Camera1,我意识到通过创建 SurfaceView,在其支架上添加回调,并在创建表面后通过定期 setOneShotPreviewCallbacks 访问预览图片.这非常简单可靠.

I want to extend an app from Camera1 to Camera2 depending on the API. One core mechanism of the app consists in taking preview pictures at a rate of about 20 pics per second. With Camera1 I realized that by creating a SurfaceView, adding a Callback on its holder and after creation of the surface accessing the preview pics via periodic setOneShotPreviewCallbacks. That was pretty easy and reliable.

现在,在学习 Camera2 时,我从头到尾"并设法将 YUV420_888 转换为位图(参见 YUV420_888 到位图转换).但是,我现在正在努力使用捕获技术".从 Google 示例中,我看到您需要使用 CameraDevice.TEMPLATE_PREVIEW 进行setRepeating"CaptureRequest 以显示预览,例如在表面视图上.那没关系.但是,为了拍摄实际照片,我需要使用(这次)builder.addTarget(imageReader.getSurface()) 发出另一个捕获请求.IE.数据将在 imageReader 的 onImageAvailable 方法中可用.

Now, when studying Camera2, I came "from the end" and managed to convert YUV420_888 to Bitmap (see YUV420_888 to Bitmap Conversion ). However I am struggling now with the "capture technique". From the Google example I see that you need to make a "setRepeating" CaptureRequest with CameraDevice.TEMPLATE_PREVIEW for displaying the preview e.g. on a surface view. That is fine. However, in order to take an actual picture I need to make another capture request with (this time) builder.addTarget(imageReader.getSurface()). I.e. data will be available within the onImageAvailable method of the imageReader.

问题:在我的设备上创建 captureRequest 是一个相当繁重的操作,大约需要 200 毫秒.因此,捕获请求的使用(无论是模板 STILL_CAPTUR 还是 PREVIEW)不可能是每秒捕获 20 个图像的可行方法,因为我需要它.我在 SO 上找到的建议主要基于(教育效率中等)Google 示例,我不太了解...

The problem: the creation of the captureRequest is a rather heavy operation taking about 200ms on my device. Therefore, the usage of a capture request (whether with Template STILL_CAPTUR nor PREVIEW) can impossibly be a feasible approach for capturing 20 images per second, as I need it. The proposals I found here on SO are primarily based on the (educationally moderately efficient) Google example, which I don't really understand...

我觉得解决方案必须是向 ImageReader 提供连续的预览图片流,这些图片可以以给定的频率从那里挑选出来.有人可以就如何实现这一点提供一些指导吗?非常感谢.

I feel the solution must be to feed the ImageReader with a contiuous stream of preview pics, which can be picked from there in a given frequency. Can someone please give some guidance on how to implement this? Many thanks.

推荐答案

如果您想为每一帧向预览 SurfaceView 和 YUV ImageReader 发送缓冲区,只需将两个 Surface 作为目标添加到重复的预览请求中.

If you want to send a buffer to both the preview SurfaceView and to your YUV ImageReader for every frame, simply add both Surfaces to the repeating preview request as targets.

通常,捕获请求可以针对会话配置的输出目标.

Generally, a capture request can target any subset (or all) of the session's configured output targets.

此外,如果您只想使用 .capture() 将偶尔的帧捕获到您的 YUV ImageReader,您不必每次都重新创建捕获请求构建器;只需在同一个构建器上再次调用 .build() ,或者如果您不更改任何设置,则仅重用实际构造的 CaptureRequest .

Also, if you do want to only capture an occasional frame to your YUV ImageReader with .capture(), you don't have to recreate the capture request builder each time; just call .build() again on the same builder, or just reuse the actual constructed CaptureRequest if you're not changing any settings.

即使有这种偶尔的捕获,您也可能希望将预览 Surface 作为目标包含在 YUV 捕获请求中,以便在显示的预览中没有跳帧.

Even with this occasional capture, you probably want to include the preview Surface as a target in the YUV capture request, so that there's no skipped frame in the displayed preview.

这篇关于Camera2 可以连续访问相机预览图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆