简体   繁体   中英

QImage from unsigned char buffer (jpg format)

I have buffer of type unsigned char* which I fill with JPG image. I want to use this buffer to draw the image to my application screen in a QLabel.

I've done this, but the image is incorrect.

Can anyone tell me what the best way to do this is?

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::load or the QImage constructor expect the image buffer to be in an uncompressed format.

If you don't intend to modify the image, use QPixmap and its loadFromData() function:

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

You can also load a Jpeg buffer with QImage::fromData / QImage::loadFromData , or QImageReader + QBuffer .

Probably this has to do with the image format. It depends on the way you load the image into the buffer. QImage expects to find an image stored line by line in the provided buffer.

Try the Format_Indexed8 or Format_RGB32 image formats. If it still does not work let us know how you have loaded the jpeg image into the buffer.

Notice that QImage provides a constructor with the image filename as argument.

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

you said that you fill the buffer with an jpg, so it seem that you have an image already saved. if so the best way is to use:

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

where :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM