Qt绘制YUVI420数据并显示,不借用外部库
时间: 2023-05-19 12:07:11 浏览: 158
Qt播放yuv420
可以使用Qt的QImage类来绘制YUVI420数据并显示,以下是示例代码:
```
QImage yuv420p_to_image(unsigned char* yuv420p_data, int width, int height)
{
QImage image(width, height, QImage::Format_RGB888);
unsigned char* y_data = yuv420p_data;
unsigned char* u_data = yuv420p_data + width * height;
unsigned char* v_data = yuv420p_data + width * height * 5 / 4;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int y_value = y_data[y * width + x];
int u_value = u_data[(y / 2) * (width / 2) + (x / 2)];
int v_value = v_data[(y / 2) * (width / 2) + (x / 2)];
int r = y_value + 1.370705 * (v_value - 128);
int g = y_value - 0.698001 * (v_value - 128) - 0.337633 * (u_value - 128);
int b = y_value + 1.732446 * (u_value - 128);
r = qBound(0, r, 255);
g = qBound(0, g, 255);
b = qBound(0, b, 255);
image.setPixel(x, y, qRgb(r, g, b));
}
}
return image;
}
```
这个函数将YUVI420数据转换为RGB格式的QImage对象,然后可以使用QLabel或QPixmap等Qt控件来显示这个图像。
阅读全文