0
点赞
收藏
分享

微信扫一扫

QImage转yuv420

数数扁桃 2022-08-16 阅读 58


void RGB24ToYUV420(int Width,int Height,uint8_t* pRGBBuffer,uint8_t*pYuvBuffer)
{
uint8_t* yuvBuf=pYuvBuffer;//YUV空间
int nWidth=Width;
int nHeight=Height;
//下面转换算法是网上查到的
int i, j;
uint8_t*bufY = yuvBuf;
uint8_t*bufU = yuvBuf + nWidth * nHeight;
uint8_t*bufV = bufU + (nWidth* nHeight* 1/4);
uint8_t*Y=bufY;
uint8_t*U=bufU;
uint8_t*V=bufV;
uint8_t*bufRGB;
unsigned char y, u, v, r, g, b,testu,testv;
if (NULL==pRGBBuffer)
{
return ;
}
for (j = 0; j<nHeight;j++)
{
bufRGB = pRGBBuffer + nWidth * (nHeight - 1-j) * 3 ;
for (i = 0;i<nWidth;i++)
{
int pos = nWidth * i + j;
r= *(bufRGB++);
g = *(bufRGB++);
b = *(bufRGB++);
y =(unsigned char)(( 66 * r + 129 * g + 25 * b + 128) >>8) + 16;//16
v = (unsigned char)((-38 * r - 74 * g + 112 * b + 128) >>8) +128 ; //128
u = (unsigned char)((112 * r - 94 * g - 18 * b + 128) >> 8) + 128 ;
*(bufY++)=__max(0,__min(y, 255 ));

if (j%2==0&&i%2 ==0)
{
if (u>255)
{
u=255;
}
if (u<0)
{
u = 0;
}
*(bufU++) =u;
//存u分量
}
else
{
//存v分量
if (i%2==0)
{
if (v>255)
{
v = 255;
}
if (v<0)
{
v = 0;
}
*(bufV++) =v;
}
}
}
}
// pVideoEncoder->m_pYUVFrame->data[0]=pVideoEncoder->yuv_buff;
// pVideoEncoder->m_pYUVFrame->data[1]=pVideoEncoder->yuv_buff+y_size;
// pVideoEncoder->m_pYUVFrame->data[2]=pVideoEncoder->yuv_buff+(y_size*5)/4;

}

注意的是:QImage一定要转成Format_RGB888格式,否则会出现错误,倒立等情况

void SaveObject::acceptData(QImage t_image,int w,int h)
{
t_image = t_image.convertToFormat(QImage::Format_RGB888);


举报

相关推荐

yuv420并转为bgr

0 条评论