当前位置 : 主页 > 网页制作 > css >

在YU中从YUV转换为RGB(android-ndk)

来源:互联网 收集:自由互联 发布时间:2021-06-13
我在 android中开发,并希望将字节数组从相机的previewCallback(YUV格式)转换为rgb格式. 我使用了这个答案中给出的函数:Getting frames from Video Image in Android 它在java中完美运行,但我的问题是我想
我在 android中开发,并希望将字节数组从相机的previewCallback(YUV格式)转换为rgb格式.

我使用了这个答案中给出的函数:Getting frames from Video Image in Android

它在java中完美运行,但我的问题是我想在c中创建函数(我使用的是ndk,而不是非常熟悉c).

我试图在c中创建函数,但它总是会产生奇怪的结果(例如图片全部为绿色).

有没有人有类似的功能或这个功能在c中工作?

谢谢.

在C中从YUYV转换为RGB:

unsigned char* rgb_image = new unsigned char[width * height * 3]; //width and height of the image to be converted

int y;
int cr;
int cb;

double r;
double g;
double b;

for (int i = 0, j = 0; i < width * height * 3; i+=6 j+=4) {
    //first pixel
    y = yuyv_image[j];
    cb = yuyv_image[j+1];
    cr = yuyv_image[j+3];

    r = y + (1.4065 * (cr - 128));
    g = y - (0.3455 * (cb - 128)) - (0.7169 * (cr - 128));
    b = y + (1.7790 * (cb - 128));

    //This prevents colour distortions in your rgb image
    if (r < 0) r = 0;
    else if (r > 255) r = 255;
    if (g < 0) g = 0;
    else if (g > 255) g = 255;
    if (b < 0) b = 0;
    else if (b > 255) b = 255;

    rgb_image[i] = (unsigned char)r;
    rgb_image[i+1] = (unsigned char)g;
    rgb_image[i+2] = (unsigned char)b;

    //second pixel
    y = yuyv_image[j+2];
    cb = yuyv_image[j+1];
    cr = yuyv_image[j+3];

    r = y + (1.4065 * (cr - 128));
    g = y - (0.3455 * (cb - 128)) - (0.7169 * (cr - 128));
    b = y + (1.7790 * (cb - 128));

    if (r < 0) r = 0;
    else if (r > 255) r = 255;
    if (g < 0) g = 0;
    else if (g > 255) g = 255;
    if (b < 0) b = 0;
    else if (b > 255) b = 255;

    rgb_image[i+3] = (unsigned char)r;
    rgb_image[i+4] = (unsigned char)g;
    rgb_image[i+5] = (unsigned char)b;
}

此方法假定您的yuyv_image也是unsigned char *.

有关YUYV的更多信息,请访问here

有关YUYV的更多说明 – > RGB检查this

网友评论