使用 QImage 存储的图片,如果想转成yuv420p 发送出去,该怎么办呢?
QImage 存储图片有多种格式,可以通过image.format() 查看当前的格式;
建议通过将格式转换为mage.convertToFormat(QImage::Format_RGB888),这样rgb的存储每个八字节,按照rgb rgb 的格式存储;image.bitPlaneCount() 可以查看一个像素点需要用的bit数;
默认是 QImage::Format_ARGB32_Premultiplied格式,此时一个像素用四个字节存储,按照argb argb的格式,不同的格式转换为yuv420p的时候,由于rgb的数据排列方式不同,需要采用不同的计算方式,因此建议转化为QImage::Format_RGB888;
这里给出计算方式:
#include <QtGui/QImage>
#include <QtCore/QByteArray>
/*
origin_image QImage图片
width /height 图片的宽度/高度
yuvData yuv420p数据
*/
void convertRGBToYUV420P1(const QImage& origin_image, int width, int height, QByteArray& yuvData)
{
QImage image ;
// 将图片转换为指定大小
image = origin_image.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug() << "Format:" << image.format();
image = image.convertToFormat(QImage::Format_RGB888);
qDebug() << "Format:" << image.format();
// 提取RGB数据
QByteArray rgbData;
const int bytesPerLine = image.width() * image.bitPlaneCount()/8;
qDebug() << "image:" << "width= " << image.width() << ";height:" << image.height()
<< "bytesPerLine:" <<bytesPerLine;
for (int y = 0; y < image.height(); ++y) {
const uchar* scanline = image.constScanLine(y);
rgbData.append(reinterpret_cast<const char*>(scanline), bytesPerLine);
}
qDebug() << "image:bitPlaneCount" << image.bitPlaneCount()/8;
// 将RGB数据转换为YUV420P
FILE* fpyuv = fopen("d:/test/out_200x200_yuv420p.yuv", "wb");
const uchar* rgbPtr = reinterpret_cast<const uchar*>(rgbData.constData());
int w = image.width(), h = image.height();
unsigned char b, g, r;
unsigned char* ybuf = new unsigned char[w * h];
unsigned char* ubuf = new unsigned char[w * h / 4];
unsigned char* vbuf = new unsigned char[w * h / 4];
unsigned char* y = ybuf;
unsigned char* u = ubuf;
unsigned char* v = vbuf;
// //图像是以RGB排序的,图像数据是左上角为图像第一个像素,从左往右,从上往下排列的
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
// rgbPtr++;
r = *rgbPtr++;
g = *rgbPtr++;
b = *rgbPtr++;
unsigned char Y = (unsigned char)((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
*y = Y;
y++;
/*
yuv420的uv采样规则:
一行一行的扫描采样,每一行都会采集U或V
在偶数行的偶数列位置采集U,奇数行的奇数列位置采集V
整体采样比例y:u:v=4:1:1
YUV 4:2:0 并不是说不采样V分量,这个0是因为每次都是隔行采集U和V,
如果第一行是 4:2:0,下一行就是 4:0:2,再下一行又是 4:2:0,以此类推
如图:每一个2x2的子块中,左上角采集u,右小角采集v
*/
if (i % 2 == 0&& j % 2 == 0) {
unsigned char U = (unsigned char)((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
*(u++) = U;
}
else if (i % 2 != 0 && j % 2 != 0) {
unsigned char V = (unsigned char)((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
*(v++) = V;
}
}
}
yuvData.append(reinterpret_cast<const char*>(ybuf), w * h);
yuvData.append(reinterpret_cast<const char*>(ubuf), w * h/ 4);
yuvData.append(reinterpret_cast<const char*>(vbuf), w * h/ 4);
//yuv420p,先写y,再写u,在写v
fwrite(ybuf, 1, w * h, fpyuv);
fwrite(ubuf, 1, w * h / 4, fpyuv);
fwrite(vbuf, 1, w * h / 4, fpyuv);
fclose(fpyuv);
delete[] ybuf;
delete[] ubuf;
delete[] vbuf;
}