Strange start byte PNG from QDataStream
-
Hello,
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:
@
QFile fileBinOut(exportOutBin+"output.bin");
fileBinOut.open(QIODevice::WriteOnly);
QDataStream streamOut(&fileBinOut);
QFile fileimg(exportOut+"test.png");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,
xedi.[Use @ chars for formatting code, peppe]
-
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.
-
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.
-
-
Hello,
I just know that 00 00 2A A9(hex) or 10921 (dec) is length of byte array which is added automatically by Qt.
:D
-
Hi Volker,
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.