[SOLVED]QByteArray to PNG image
-
Hi everyone!
first of all sorry for my bad english! :)
This is my first post !
I'm a newbie with qt and c++I've a file called notes.json saved from an application written in flex/actionscript
in this file i have serialized a encoded png
this is a parto of my json<code>"bitmaps": [
{
"height": 329,
"width": 171,
"id": "5B264299-621F-DFBE-0FD6-A9BF5E921319",
"rect": {
"position": 225036,
"bytesAvailable": 0,
"objectEncoding": 3,
"endian": "bigEndian",
"length": 225036
}
},
</code>So in my QT Application i'm trying to import notes.json read the bytearray and save the related png
this is what i'm doing<code>
qDebug() << "importBitmaps()";
int bW = bitmap.value("width").toInt();
int bH = bitmap.value("height").toInt();
QString shortFileName = bitmap.value("id").toString();
QString fileName = btb::ResourceManager().getClipDir().absolutePath().append("/"+shortFileName+".png");
QByteArray ba = bitmap.value("rect").toByteArray();
QImage image(fileName);
QBuffer buff(&ba);
buff.open(QBuffer::WriteOnly);
image.save(&buff,"PNG");
</code>
obviously doesn't work ....
What's wrong in my code ?thanks in advanced!
GG. -
@GGAllin
is that all your JSON or just a fragment? It looks like your serialization is missing the actual bytes (data) of the PNG image. From the example above, if "length": 225036 I assume you have received 225036 more bytes with your JSON which will be the byte array to create the PNG image from . Maybe I'm missing something.Regarding your post, please note that it uses Markdown syntax, please check your tags, especially how to mark code snippets (` start and end of a code snippet)
-
@Pablo-J.-Rogina
Hi pablo!
I've solverd my problem (partially)qDebug() << "importBitmaps()"; int bW = bitmap.value("width").toInt(); int bH = bitmap.value("height").toInt(); QString shortFileName = bitmap.value("id").toString(); QString fileName = btb::ResourceManager().getClipDir().absolutePath().append("/"+shortFileName+".png"); QByteArray ba = bitmap.value("rect").toByteArray(); QImage image((const unsigned char*)ba.data(),bW,bH,QImage::Format_ARGB32); image.save(fileName,"PNG");
the Images is saved correctly but with the incorrect color format ...
-
Hi and welcome to devnet,
What is the original color format of your image ? And what result are you currently getting ? RB channels swapped ?
-
Hi SGaist
in my notes.json i write an unsigned integer (a 32-bit unmultiplied pixel value) for each pixel of my pic,
In the old application to recreate the png from data
(actionscript code)
for(i=0;i<o.bitmaps.length;i++)
{
var fileToSave:String = Config.PATH_LOCAL_STATUS+Database.id+"/"+o.bitmaps[i]["id"]+".png";Config.BTMPS.push(o.bitmaps[i]); var bmp:BitmapData = new BitmapData(o.bitmaps[i].width, o.bitmaps[i].height); var bt:ByteArray = o.bitmaps[i].rect as ByteArray; bt.position =0; bmp.setPixels(bmp.rect, bt); mdm.FileSystem.BinaryFile.setDataBA(new PNGEncoder().encode(bmp)); mdm.FileSystem.BinaryFile.writeDataBA(fileToSave); }
you can see the result looking at link, on the left there is the png saved with actionscript, on the right there is tha same pic saved with Qt code
http://postimg.org/image/wzkclmu45/
thanks for help me! ;)
-
Looks like you are channels are mixed, try with QImage::invertPixels
-
@SGaist
Hi SGaist,
i solved manupulating my BA manually
int bW = bitmap.value("width").toInt();
int bH = bitmap.value("height").toInt();
QString shortFileName = bitmap.value("id").toString();
QString fileName = btb::ResourceManager().getClipDir().absolutePath().append("/"+shortFileName+".png");
QByteArray ba = bitmap.value("rect").toByteArray();
QByteArray to = QByteArray();
for(int i=0; i<bWbH;i++)
{
// ARGB -> ABGR
to.append( ba[i4+3]);
to.append( ba[i4+2]);
to.append( ba[i4+1]);
to.append( ba[i4]);
}
QImage image((const unsigned char)to.data(),bW,bH,QImage::Format_RGB32);
//image.invertPixels();
image.save(fileName,"PNG");thanks for help!