簡體   English   中英

來自unsigned char緩沖區的QImage(jpg格式)

[英]QImage from unsigned char buffer (jpg format)

unsigned char*類型的緩沖區,我用JPG圖像填充。 我想使用此緩沖區將圖像繪制到QLabel中的應用程序屏幕。

我做到了這一點,但圖像不正確。

誰能告訴我最好的方法是什么?

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.

QImage::loadQImage構造函數期望圖像緩沖區處於未壓縮格式。

如果您不打算修改圖像,請使用QPixmap及其loadFromData()函數:

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

您還可以使用QImage::fromData / QImage::loadFromDataQImageReader + QBuffer加載Jpeg緩沖區。

這可能與圖像格式有關。 這取決於您將圖像加載到緩沖區的方式。 QImage期望在提供的緩沖區中逐行查找存儲的圖像。

嘗試Format_Indexed8Format_RGB32圖像格式。 如果它仍然無法正常工作,請告訴我們如何將jpeg圖像加載到緩沖區中。

請注意, QImage提供了一個構造函數 ,其圖像文件名為參數。

正確的方法是不將jpg數據解釋為RGB格式,並使用適當的類,例如QImageReader ,或者事件使用構造函數直接加載圖像。

你說你用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM