实时处理Android摄像头帧实时、摄像头、Android

2023-09-05 05:45:27 作者:天生傲骨、怎能屈服

我想创建一个Android应用程序,将实时处理摄像头帧。刚开始时,我只是想显示的摄像头看到的灰度版本。我已经成功地从上previewFrame方法的字节数组中提取适当的值。下面是我的code只是一个片段:

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);
}

第一个[宽*高]字节[]框数组的值是亮度(灰度)值。一旦我提取他们,我怎么显示出来的画面的图像?它不是一个二维数组一样,所以我将如何指定的宽度和高度?感谢您的帮助。

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? Thanks for any help.

推荐答案

您可以从的 OpenCV4Android人员。看看他们提供的例子,特别是教程1基础。 0 Android摄像头

You can get extensive guidance from the OpenCV4Android SDK. Look into their available examples, specifically Tutorial 1 Basic. 0 Android Camera

不过,因为它是在我的情况下,密集的图像处理,这会比接受更慢的实时图像处理应用程序。 一个很好的替代他们的在previewFrame 的字节数组转换为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 =新YuvImage(帧,ImageFormat.NV21,宽度,高度,NULL);

创建的矩形尺寸相同的图像。

Create a rectangle the same size as the image.

创建一个ByteArrayOutputStream,并通过这一点,矩形和COM pression价值融为一体pressToJpeg():

Create a ByteArrayOutputStream and pass this, the rectangle and the compression value to compressToJpeg():

ByteArrayOutputStream BAOS =新ByteArrayOutputStream(); yuvimage.com pressToJpeg(imageSizeRectangle,100,BAOS);

字节[]为imageData = baos.toByteArray();

位图previewBitmap = BitmapFactory.de codeByteArray(为imageData,0,为imageData .length);

渲染表面上的这些previewFrames以及所涉及的最佳实践是一个新的层面。 =)

Rendering these previewFrames on a surface and the best practices involved is a new dimension. =)