2011-12-12 24 views
4

我有我填写JPG图像的unsigned char*类型的缓冲区。我想用这个缓冲区在QLabel中将图像绘制到我的应用程序屏幕上。从无符号字符缓冲区(JPG格式)QImage

我已经这样做了,但图像不正确。

任何人都可以告诉我什么是最好的方法是?

QPixmap pix = QPixmap::fromImage(
    QImage(buff, 460, 345, QImage::Format_RGB888)); //Not sure what format to use for a jpg image? 

one_img->setPixmap(pix); //one_img is of type QLabel. 

回答

6

QImage::loadQImage构造期望的图像缓冲器为未压缩格式。

如果您不打算修改图像,使用QPixmap及其loadFromData()功能:

QPixmap pix; 
pix.loadFromData(buff, sizeOfBuff, "JPG"); 

您也可以加载一个JPEG缓冲带QImage::fromData/QImage::loadFromData,或QImageReader + QBuffer

0

可能这与图像格式有关。这取决于您将图像加载到缓冲区的方式。 QImage预计会在提供的缓冲区中逐行查找存储的图像。

尝试使用Format_Indexed8Format_RGB32图像格式。如果它仍然不起作用,请告诉我们如何将jpeg图像加载到缓冲区中。

请注意,QImage提供了一个constructor,图像文件名作为参数。

0

正确的方法不是将jpg数据解释为RGB格式,而是使用适当的类,例如QImageReader或事件使用the constructor直接加载图像。

0

你说你用jpg填充缓冲区,所以看起来你已经保存了一个图像。 如果因此最好的办法是使用:

QPixmap (const QString & fileName, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor)

其中:

 
filename is the path to the image 
format is in your case "JPG" or JPEG 
flags is to specify if the image is black white or color (see the documentation for details) 
相关问题