Jake Evans
Jake Evans

Reputation: 1048

Possible to load QImage file contents to QString?

Is it possible to load the raw contents of an image file using QT and have it as a QString?

Reason I ask is because I need to load an image, encrypt the image data and then send it off to a server. So I need to read the raw data from the image and perform rijndael-128 encryption before sending it off.

Thanks!

Upvotes: 1

Views: 725

Answers (2)

László Papp
László Papp

Reputation: 53155

The image data is not like a QString which aids the representation of UTF characters, so this conversion idea sounds strange. If anything, I would suggest to send the raw file data over with QByteArray and/or QDataStream.

Either way, here are the methods that you could use for your original question. This would be the first step, and then you could build your QByteArray or QString. Note that these methods return unsigned char as opposed to signed what the constructor of the aforementioned classes expects.

uchar * QImage::bits()

Returns a pointer to the first pixel data. This is equivalent to scanLine(0).

Note that QImage uses implicit data sharing. This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.

const uchar * QImage::bits() const

This is an overloaded function.

const uchar * QImage::constBits() const

Returns a pointer to the first pixel data.

Note that QImage uses implicit data sharing, but this function does not perform a deep copy of the shared pixel data, because the returned data is const.

Upvotes: 1

Simon Warta
Simon Warta

Reputation: 11408

Use QImage::bits() or QImage::constBits() to get binary (uncompressed) image data.

See example in function FreedesktopImage::FreedesktopImage(const QImage &img) here: https://github.com/laanwj/bitcoin/blob/master/src/qt/notificator.cpp#L119 on how to copy the data to a QByteArray.

But the question is, do you really need to load image files to QImage before you encrypt them? Do you do heavy image manipulation?

Upvotes: 0

Related Questions