简体   繁体   English

从Camera Preview类创建/使用YUV图像缓冲区

[英]Create / Use YUV Image buffer from Camera Preview class

In our application, we need to use capture frame from camera @ 33 fps and pass it to compression before sending it to server, 在我们的应用程序中,我们需要使用相机以33 fps的速度捕获帧并将其传递到压缩状态,然后再发送到服务器,

My compression module can take YUV Image, to compress the image, my camera configuration as follows, 我的压缩模块可以拍摄YUV图像,以压缩图像,我的相机配置如下,

Width = 500 px, 宽度= 500像素,
height = 300 px , 高度= 300 px,
Image format : YV12 图片格式:YV12

On the preview callback 在预览回调中

camera.setPreviewCallback(new PreviewCallback() {

                public void onPreviewFrame(byte[] data, Camera arg1) {

                                }
}

data size is coming out to be 230400, but i suppose it would be around 数据大小即将为230400,但我想它会在

500*300(Y) + 500*300/4(U) + 500*300/4(V) ie 2250000 500 * 300(Y)+ 500 * 300/4(U)+ 500 * 300/4(V),即2250000

ie 5400 bytes more, does that mean, i can ignore the reamining one ? 即5400字节,这是否意味着,我可以忽略重生一个?

Also i need to create YUVImage object , but stride info is not coming, so how we can create YUVImage from above data. 另外,我需要创建YUVImage对象,但是没有大步信息,因此我们如何从上述数据创建YUVImage。

Sincere Thanks for reading and i really appreciate if anyone can help me out on this. 真诚的感谢您的阅读,如果有人可以帮助我,我也非常感谢。

Cant help with the data size question but to get YUV from Camera preview you have 2 choices. 无法解决数据大小问题,但要从“摄像机预览”中获取YUV,您有2个选择。 If running Android 2.2 or later your can use the android.graphics.YuvImage class and just pass it's constructor your bytearray from PreviewCallback. 如果运行的是Android 2.2或更高版本,则可以使用android.graphics.YuvImage类,并将其构造函数从PreviewCallback传递给您的字节数组。

If you need to support pre 2.2 then you need to do something like: 如果您需要支持2.2之前的版本,则需要执行以下操作:

/**
 * Decodes YUV frame to a buffer which can be use to create a bitmap.
 * use this for OS < FROYO which has a native YUV decoder
 * decode Y, U, and V values on the YUV 420 buffer described as YCbCr_422_SP by Android 
 * @param rgb the outgoing array of RGB bytes
 * @param fg the incoming frame bytes
 * @param width of source frame
 * @param height of source frame
 * @throws NullPointerException
 * @throws IllegalArgumentException
 */
private static void decodeYUV_impl(int[] rgb, byte[] fg, int width, int height) throws NullPointerException, IllegalArgumentException 
{
    int sz = width * height;
    if (rgb == null)
        throw new NullPointerException("buffer out is null");
    if (rgb.length < sz)
        throw new IllegalArgumentException("buffer out size " + rgb.length
                + " < minimum " + sz);
    if (fg == null)
        throw new NullPointerException("buffer 'fg' is null");

    if (fg.length < sz)
        throw new IllegalArgumentException("buffer fg size " + fg.length
                + " < minimum " + sz * 3 / 2);

    int i, j;
    int Y, Cr = 0, Cb = 0;
    for (j = 0; j < height; j++) {
        int pixPtr = j * width;
        final int jDiv2 = j >> 1;
    for (i = 0; i < width; i++) {
        Y = fg[pixPtr];
        if (Y < 0)
            Y += 255;
        if ((i & 0x1) != 1) {
            final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
            Cb = fg[cOff];
            if (Cb < 0)
                Cb += 127;
            else
                Cb -= 128;
            Cr = fg[cOff + 1];
            if (Cr < 0)
                Cr += 127;
            else
                Cr -= 128;
        }
        int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
        if (R < 0)
            R = 0;
        else if (R > 255)
            R = 255;
        int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1)
                + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
        if (G < 0)
            G = 0;
        else if (G > 255)
            G = 255;
        int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
        if (B < 0)
            B = 0;
        else if (B > 255)
            B = 255;
        rgb[pixPtr++] = (0xff000000 + (B << 16) + (G << 8) + R);
    }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM