使用 Android L 和 Camera2 API 处理相机预览图像数据 [英] Camera preview image data processing with Android L and Camera2 API
问题描述
我正在开发一个 Android 应用程序,该应用程序处理来自相机的输入图像并将其显示给用户.这相当简单,我使用 setPreviewCallbackWithBuffer
在相机对象上注册了一个 PreviewCallback
.这很容易,并且可以与旧的相机 API 配合使用
public void onPreviewFrame(byte[] data, Camera cam) {//自定义图像数据处理}
我正在尝试移植我的应用程序以利用新的 Camera2 API,但我不确定我应该如何做到这一点.我遵循了允许录制视频的 L 预览示例中的 Camera2Video.但是,样本中并没有直接的图像数据传输,所以我不明白具体从哪里获取图像像素数据以及如何处理.
有人可以帮助我或建议如何在 android L 中获得 PreviewCallback
的功能,或者如何在将其显示到屏幕之前处理来自相机的预览数据?(相机对象没有预览回调)
谢谢!
由于 Camera2
API 与当前的 Camera
API 有很大不同,可能有助于通过文档.
一个好的起点是 camera2basic
示例.它演示了如何使用Camera2
API 并配置ImageReader
来获取JPEG 图像并注册ImageReader.OnImageAvailableListener
以接收这些图像
要接收预览帧,您需要将 ImageReader
的表面添加到 setRepeatingRequest
的 CaptureRequest.Builder
.
此外,您应该将 ImageReader
的格式设置为 YUV_420_888
,这将为您提供 30fps 的 8MP(文档保证 Nexus 5 的 8MP 为 30fps).>
I'm working on an android app that is processing the input image from the camera and displays it to the user. This is fairly simple, I register a PreviewCallback
on the camera object with the setPreviewCallbackWithBuffer
.
This is easy and works smoothly with the old camera API
public void onPreviewFrame(byte[] data, Camera cam) {
// custom image data processing
}
I'm trying to port my app to take advantage of the new Camera2 API and I'm not sure how exactly shall I do that. I followed the Camera2Video in L Preview samples that allows to record a video. However, there is no direct image data transfer in the sample, so I don't understand where exactly shall I get the image pixel data and how to process it.
Could anybody help me or suggest the way how one can get the the functionality of PreviewCallback
in android L, or how it's possible to process preview data from the camera before displaying it to the screen? (there is no preview callback on the camera object)
Thank you!
Since the Camera2
API is very different from the current Camera
API, it might help to go through the documentation.
A good starting point is camera2basic
example. It demonstrates how to use Camera2
API and configure ImageReader
to get JPEG images and register ImageReader.OnImageAvailableListener
to receive those images
To receive preview frames, you need to add your ImageReader
's surface to setRepeatingRequest
's CaptureRequest.Builder
.
Also, you should set ImageReader
's format to YUV_420_888
, which will give you 30fps at 8MP (The documentation guarantees 30fps at 8MP for Nexus 5).
这篇关于使用 Android L 和 Camera2 API 处理相机预览图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!