我正在用android开发,并希望将字节数组从相机的YUV格式的PreviewCallback转换为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也是一个无符号的char *。
有关YUYV的更多信息,请参见here。
有关YUYV的更多说明-> RGB,请查看this