使用 NDK 在 Android 中进行实时图像处理
使用 Android (2.3.3) 手机,我可以使用相机通过 onPreviewFrame(byte[] data, Camera camera)
方法检索预览,以获取 YUV 图像。
对于某些图像处理,我需要将此数据转换为 RGB 图像并将其显示在设备上。使用基本的 java / android 方法,其运行速度低于 5 fps...
现在,使用 NDK,我想加快速度。问题是:如何在C中将YUV数组转换为RGB数组?有没有办法在本机代码中显示它(也许使用 OpenGL?)?实时应该是可能的(高通 AR 演示向我们展示了这一点)。
我无法使用 setTargetDisplay
并在其上放置覆盖层!
我了解 Java,最近开始使用 Android SDK,并且对 C 零经验
Using an Android (2.3.3) phone, I can use the camera to retrieve a preview with the onPreviewFrame(byte[] data, Camera camera)
method to get the YUV image.
For some image processing, I need to convert this data to an RGB image and show it on the device. Using the basic java / android method, this runs at a horrible rate of less then 5 fps...
Now, using the NDK, I want to speed things up. The problem is: How do I convert the YUV array to an RGB array in C? And is there a way to display it (using OpenGL perhaps?) in the native code? Real-time should be possible (the Qualcomm AR demos showed us that).
I cannot use the setTargetDisplay
and put an overlay on it!
I know Java, recently started with the Android SDK and have zero experience in C
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否考虑过使用OpenCV 的 Android 端口?它不仅仅可以做颜色转换,而且速度相当快。
Have you considered using OpenCV's Android port? It can do a lot more than just color conversion, and it's quite fast.
Google 搜索返回了此页面,以获取 YUV->RGB565 的 C 实现。作者甚至为其添加了 JNI 包装器。
A Google search returned this page for a C implementation of YUV->RGB565. The author even included the JNI wrapper for it.
继续使用 Java 也可以获得成功。我这样做是为了 androangelo 应用程序的图像检测。
我使用了您通过搜索“decodeYUV”在此处找到的示例代码。
为了处理帧,要考虑的重要部分是图像大小。
根据设备的不同,您可能会获得相当大的图像。即 Galaxy S2
支持的最小预览尺寸为 640*480。这是一个很大的像素量。
我所做的就是在 yuvtorgb 解码后仅使用每隔一行和每隔一列。因此,处理 320*240 图像效果非常好,并允许我获得 20fps 的帧速率。 (包括一些降噪、从 rgb 到 hsv 的颜色转换以及圆形检测)
此外,您应该仔细检查提供给 setPreview 函数的图像缓冲区的大小。如果它太小,垃圾收集就会破坏一切。
对于结果,您可以检查 androangelo 应用程序的校准屏幕。在那里,我在相机预览上覆盖了检测到的图像。
You can also succeed by staying with Java. I did this for the imagedetectíon of the androangelo-app.
I used the sample code which you find here by searching "decodeYUV".
For processing the frames, the essential part to consider is the image-size.
Depending on the device you may get quite large images. i.e. for the Galaxy S2
the smallest supported previewsize is 640*480. This is a big amount of pixels.
What I did, is to use only every second row and every second column, after yuvtorgb decoding. So processing a 320*240 image works quite well and allowed me to get frame-rates of 20fps. (including some noise-reduction, a color-conversion from rgb to hsv and a circledetection)
In addition You should carefully check the size of the image-buffer provided to the setPreview function. If it is too small, the garbage-collection will spoil everything.
For the result you can check the calibration-screen of the androangelo-app. There I had an overlay of the detected image over the camera-preview.