QImage of unsigned char buffer (jpg format)

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

I did this, but the image is incorrect.

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

 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. 
+4
source share
4 answers

QImage::load or the QImage constructor expects the image buffer to be in uncompressed format.

If you do not intend to change the image, use QPixmap and its loadFromData() function:

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

You can also load the Jpeg buffer using QImage::fromData / QImage::loadFromData or QImageReader + QBuffer .

+6
source

Perhaps this is due to the image format. It depends on how the image is loaded into the clipboard. QImage expects to find the image stored in the string in the specified buffer.

Try the image formats Format_Indexed8 or Format_RGB32 . If it still doesn't work, let us know how you uploaded the jpeg image to the buffer.

Note that QImage provides a constructor with the image file name as an argument.

0
source

The correct way is not to interpret the jpg data as RGB format and use the appropriate class, for example QImageReader , or use the event constructor to load the image directly.

0
source

you said that you fill the buffer with jpg, so it seems that you have already saved the image. if this is the best way:

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)
0
source

Source: https://habr.com/ru/post/1385885/


All Articles