SL100_FactoryTestTool/FactoryTestTool/SourceCode/Media/Media.cpp

119 lines
3.8 KiB
C++

// Media.cpp
#include "Media.h"
// YUV 转换为 RGB 的辅助函数
void yuvToRgb(int y, int u, int v, int& r, int& g, int& b)
{
r = y + 1.402 * (v - 128);
g = y - 0.344136 * (u - 128) - 0.714136 * (v - 128);
b = y + 1.772 * (u - 128);
r = qBound(0, r, 255);
g = qBound(0, g, 255);
b = qBound(0, b, 255);
}
// 将 YUV422 数据转换为 QImage
QImage convertYUV422ToQImage(const QByteArray& yuv422Data, int width, int height)
{
QImage image(width, height, QImage::Format_RGB888);
const uchar* yuvPtr = reinterpret_cast<const uchar*>(yuv422Data.constData());
const uchar* yuvDataEnd = yuvPtr + yuv422Data.size();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; x += 2) {
if (yuvPtr + 4 > yuvDataEnd) {
qWarning() << "YUV data access out of range!";
return image;
}
int y0 = yuvPtr[0];
int u = yuvPtr[1];
int y1 = yuvPtr[2];
int v = yuvPtr[3];
yuvPtr += 4;
int r0, g0, b0;
int r1, g1, b1;
yuvToRgb(y0, u, v, r0, g0, b0);
yuvToRgb(y1, u, v, r1, g1, b1);
image.setPixel(x, y, qRgb(r0, g0, b0));
image.setPixel(x + 1, y, qRgb(r1, g1, b1));
}
}
return image;
}
// 将 YUV420 数据转换为 QImage
QImage convertYUV420ToQImage(const QByteArray& yuv420Data, int width, int height)
{
QImage image(width, height, QImage::Format_RGB888);
const uchar* yData = reinterpret_cast<const uchar*>(yuv420Data.constData());
const uchar* uData = yData + width * height;
const uchar* vData = uData + (width * height) / 4;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int yValue = yData[y * width + x];
int uValue = uData[(y / 2) * (width / 2) + (x / 2)];
int vValue = vData[(y / 2) * (width / 2) + (x / 2)];
int r, g, b;
yuvToRgb(yValue, uValue, vValue, r, g, b);
image.setPixel(x, y, qRgb(r, g, b));
}
}
return image;
}
void showPic(QLabel* leftLens_m_imageLabel, QLabel* rightLens_m_imageLabel, const QString& client, const QByteArray& valData)
{
int lens_n = static_cast<unsigned char>(valData[0]);
int width = (static_cast<unsigned char>(valData[2]) << 8) | static_cast<unsigned char>(valData[1]);
int height = (static_cast<unsigned char>(valData[4]) << 8) | static_cast<unsigned char>(valData[3]);
int format = static_cast<unsigned char>(valData[5]);
qDebug() << "lens_n = " << lens_n;
qDebug() << "format = " << format;
qDebug() << "width = " << width;
qDebug() << "height = " << height;
QByteArray yuvData = valData.mid(6);
qDebug() << "yuvData size = " << yuvData.size();
QImage image;
if (format == YUV422) {
image = convertYUV422ToQImage(yuvData, width, height);
}
else if (format == YUV420) {
image = convertYUV420ToQImage(yuvData, width, height);
}
else {
qWarning() << "Unsupported image format!";
return;
}
// 旋转图像90度
QTransform transform;
transform.rotate(90); // 可以调整旋转角度
QImage rotatedImage = image.transformed(transform);
QImage scaledImage = rotatedImage.scaled(leftLens_m_imageLabel->size(), Qt::KeepAspectRatio);
if (lens_n == 0) {
leftLens_m_imageLabel->setPixmap(QPixmap::fromImage(scaledImage));
//rightLens_m_imageLabel->setPixmap(QPixmap::fromImage(scaledImage));
}
else if (lens_n == 1) {
rightLens_m_imageLabel->setPixmap(QPixmap::fromImage(scaledImage));
}
else {
qWarning() << "Unsupported image lens!";
}
}