Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED]QByteArray to PNG image
QtWS25 Last Chance

[SOLVED]QByteArray to PNG image

Scheduled Pinned Locked Moved General and Desktop
qbytqimageserializationjson
7 Posts 3 Posters 8.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GGAllin
    wrote on 23 Jun 2015, 14:37 last edited by GGAllin
    #1

    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.

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Pablo J. Rogina
      wrote on 23 Jun 2015, 16:06 last edited by
      #2

      @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)

      Upvote the answer(s) that helped you solve the issue
      Use "Topic Tools" button to mark your post as Solved
      Add screenshots via postimage.org
      Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

      G 1 Reply Last reply 24 Jun 2015, 07:00
      0
      • P Pablo J. Rogina
        23 Jun 2015, 16:06

        @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)

        G Offline
        G Offline
        GGAllin
        wrote on 24 Jun 2015, 07:00 last edited by GGAllin
        #3

        @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 ...

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 24 Jun 2015, 21:58 last edited by
          #4

          Hi and welcome to devnet,

          What is the original color format of your image ? And what result are you currently getting ? RB channels swapped ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            GGAllin
            wrote on 25 Jun 2015, 07:14 last edited by
            #5

            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! ;)

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Jun 2015, 19:56 last edited by
              #6

              Looks like you are channels are mixed, try with QImage::invertPixels

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              G 1 Reply Last reply 26 Jun 2015, 10:47
              0
              • S SGaist
                25 Jun 2015, 19:56

                Looks like you are channels are mixed, try with QImage::invertPixels

                G Offline
                G Offline
                GGAllin
                wrote on 26 Jun 2015, 10:47 last edited by
                #7

                @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[i
                4+3]);
                to.append( ba[i4+2]);
                to.append( ba[i
                4+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!

                1 Reply Last reply
                0

                4/7

                24 Jun 2015, 21:58

                • Login

                • Login or register to search.
                4 out of 7
                • First post
                  4/7
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved