2011-12-12 31 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) 
相關問題