Elliott
Elliott

Reputation: 1376

Storing binary data in a PNG file using QImage::setText()

I need to read/store binary data in the metadata of a PNG image. Specifically, it will be gzipped data which has been serialized by Google's protobuf library.

I'm using the QImage library of Qt5 to load and store the images, so it would be extremely convenient if I could find a way to use some Qt library to add this binary data to the QImage I'm already using.

I see that the QImage class has a setText method which appears to do exactly what I want, except for one caveat: It takes a QString as an argument and not a QByteArray, and the QString constructor mangles my binary data.

How can I force QString to preserve my binary data, both when storing and loading? If that's not possible, is there some other way in Qt5 to add metadata to a PNG image?

Upvotes: 2

Views: 1427

Answers (1)

Reunanen
Reunanen

Reputation: 8001

You could try to encode the data as Base64 before converting to QString, and then decode when reading back. I do understand that this significantly reduces the benefits of your gzip compression, but at least you could try to see if that helps.

Upvotes: 2

Related Questions