Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Strange start byte PNG from QDataStream
I want to save PNG image to file through byte array because I will add other info in that byte array later. First , I try to save PNG like this:
if (!fileimg.open(QIODevice::ReadOnly)) return; QByteArray baImg= fileimg.readAll(); streamOut <<baImg; fileBinOut.close();
this code work well, but when I read the the output.bin in another program (java's program) , it cannot produce image. (error).
after i see the "output.bin" with hex editor I notice strange byte in start byte. ( 00 00 2A A9 )
output.bin: 00 00 2A A9 89 50 4E 47 . . . . .
normal PNG : 89 50 4E 47 . . . . . . . . . . . . . .
the rest byte is same. if i delete 00 00 2A A9 my png reader worked fine. my question is what the 00 00 2A A9 . how to remove this automatically when saving?
thank in advance,
[Use @ chars for formatting code, peppe]
dangelog last edited by
That's the wrong way of saving images. You're not saving an image, you're serializing a QImage object. Use QImage::save or QImageWriter instead.
fast replied! . thank you. As I said, I use QByteArray instead of QImageWriter (or other way) because I want to add other data on that array later.
goetz last edited by
An image, is an image, is an image. If you want to add other data, you corrupt the image anyways. It will not be readable by another program either.
Check the documentation: http://developer.qt.nokia.com/doc/qt-4.8/QDataStream.html
QDataStream stores version of your data that is readable cross-platform. It will change the data you put into it by adding meta information (version of QDataStream used to store the stream) as well as changing data (e.g. adjusting byte order).
It is not a way to faithfully copy a set of bytes to disk as-is, which is what you are trying to use it for.
I think you still didn't get my point, I read those data by my own reader. so I will split between my added-data and image.
yes . for now I accepted that output, so my solution is adjust my reader. (skip reading unnecessary bytes)
btw, thank you very much all. :)
I just know that 00 00 2A A9(hex) or 10921 (dec) is length of byte array which is added automatically by Qt.
goetz last edited by
You can look up in the sources what's written into the stream. The datastream itself contains a version too. If you want to manipulate the bytes yourself, do not use a QDataStream, but write directly to a QFile. The API is similar.
Thank for suggestion, I'll try it.
Any PNG file starts with some magic bytes. So using QDataStream is not an option for you if you want to produce PNG output. Modifying your reader to accept a non-PNG file as PNG is not a solution IMHO. Better do what Volker suggested and use the QFile API instead of the datastream.