实时处理Android相机帧 [英] Processing Android camera frames in real time
问题描述
我正在尝试创建一个实时处理相机帧的 Android 应用程序.首先,我只想显示相机看到的灰度版本.我已经设法从 onPreviewFrame
方法中的字节数组中提取适当的值.以下只是我的代码片段:
I'm trying to create an Android application that will process camera frames in real time. To start off with, I just want to display a grayscale version of what the camera sees. I've managed to extract the appropriate values from the byte array in the onPreviewFrame
method. Below is just a snippet of my code:
byte[] pic;
int pic_size;
Bitmap picframe;
public void onPreviewFrame(byte[] frame, Camera c)
{
pic_size = mCamera.getParameters().getPreviewSize().height * mCamera.getParameters().getPreviewSize().width;
pic = new byte[pic_size];
for(int i = 0; i < pic_size; i++)
{
pic[i] = frame[i];
}
picframe = BitmapFactory.decodeByteArray(pic, 0, pic_size);
}
byte[]
帧数组的第一个 [width*height]
值是亮度(灰度)值.提取它们后,如何将它们作为图像显示在屏幕上?它也不是二维数组,那么如何指定宽度和高度?
The first [width*height]
values of the byte[]
frame array are the luminance (greyscale) values. Once I've extracted them, how do I display them on the screen as an image? Its not a 2D array as well, so how would I specify the width and height?
推荐答案
您可以从 OpenCV4Android SDK.查看他们可用的示例,特别是 Tutorial 1 Basic.0 安卓相机
You can get extensive guidance from the OpenCV4Android SDK. Look into their available examples, specifically Tutorial 1 Basic. 0 Android Camera
但是,就我的情况而言,对于密集的图像处理,这将比实时图像处理应用程序所接受的要慢.一个很好的替代他们的 onPreviewFrame
的字节数组转换为 YUVImage:
But, as it was in my case, for intensive image processing, this will get slower than acceptable for a real-time image processing application.
A good replacement for their onPreviewFrame
's byte array conversion to YUVImage:
YuvImage yuvImage = new YuvImage(frame, ImageFormat.NV21, width, height, null);
创建一个与图像大小相同的矩形.
Create a rectangle the same size as the image.
创建一个 ByteArrayOutputStream 并将其、矩形和压缩值传递给 compressToJpeg():
Create a ByteArrayOutputStream and pass this, the rectangle and the compression value to compressToJpeg():
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(imageSizeRectangle, 100, baos);
byte [] imageData = baos.toByteArray();
位图预览Bitmap = BitmapFactory.decodeByteArray(imageData , 0, imageData .length);
在表面上渲染这些 previewFrames 和所涉及的最佳实践是一个新的维度.=)
Rendering these previewFrames on a surface and the best practices involved is a new dimension. =)
这篇关于实时处理Android相机帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!