Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved How to get a picture from QString in Qt

    General and Desktop
    6
    23
    904
    Loading More Posts
    • 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.
    • M
      Mikeeeeee last edited by

      Hi!
      So I translate the picture into a QString :

      QString AppCore::getImage(QString addressFile)
      {
          QImage myImage(addressFile);
      
          QByteArray bArray;
          QBuffer buffer(&bArray);
          buffer.open(QIODevice::WriteOnly);
          myImage.save(&buffer, "JPEG");
      
          QString image("data:image/jpg;base64,");
          image.append(QString::fromLatin1(bArray.toBase64().data()));
      
          //qDebug()<<"bArray.size() : "<<bArray.size();
          QPixmap pixamp1;
          QByteArray bArray2;
          //bArray2 = QByteArray::fromHex(image.toLocal8Bit());
          /*bArray2.append(image);
          pixamp1.loadFromData(bArray2,nullptr,Qt::AutoColor);
          qDebug()<<"pixamp1.size() : "<<pixamp1.size();*/
      
          return image;
      }
      

      Array b Array is translated into a picture, but to perform the transformation with the QString in image does not work.
      Please tell me how this can be done?

      Christian Ehrlicher 1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion @Mikeeeeee last edited by

        So where/how do you display this base64 encoded image?

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 0
        • M
          Mikeeeeee last edited by

          I do not display it anywhere yet, just transfer it to the server. But not in the program can transfer QString in the picture

          1 Reply Last reply Reply Quote 0
          • M
            Mikeeeeee last edited by

            Just tried to string so

            QString imageString = QString::fromStdString(bArray.toStdString());
            
            1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              Can you please explain what you're really trying to do...

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 0
              • M
                Mikeeeeee last edited by

                I need to translate the picture into Qstring, transfer to another device and again translate into a picture.

                1 Reply Last reply Reply Quote 0
                • Christian Ehrlicher
                  Christian Ehrlicher Lifetime Qt Champion last edited by

                  And how do you transfer the data?

                  Qt has to stay free or it will die.

                  1 Reply Last reply Reply Quote 1
                  • M
                    Mikeeeeee last edited by

                    I use TcpSocket

                    Gojir4 aha_1980 2 Replies Last reply Reply Quote 0
                    • Gojir4
                      Gojir4 @Mikeeeeee last edited by Gojir4

                      @mikeeeeee Hi,

                      I think it's easier if you use QDataStream.
                      I never tested but it seems quite straightforward

                      Write

                      QDataStream out(&socket);   // serialize the data 
                      out << myImage;             // serialize an image
                      

                      Read

                      QDataStream in(&socket);    // read the data serialized from the socket
                      QImage myImage;
                      in >> myImage;              // extract image
                      
                      1 Reply Last reply Reply Quote 4
                      • aha_1980
                        aha_1980 Lifetime Qt Champion @Mikeeeeee last edited by

                        @mikeeeeee said in How to get a picture from QString in Qt:

                        I use TcpSocket

                        Then you don't need QString. You need QByteArray, probably QDataStream as @Gojir4 pointed out.

                        QString imageString = QString::fromStdString(bArray.toStdString());

                        Sorry, but never do that!

                        QString imageString = QString::fromUtf8(bArray); is most often correct, sometimes fromLatin1() or fromLocal8Bit() might be needed too.

                        But as said, your problem cannot be solved with QString.

                        Regards

                        Qt has to stay free or it will die.

                        1 Reply Last reply Reply Quote 4
                        • M
                          Mikeeeeee last edited by

                          But I pass the picture to json , and with the picture I pass additional information to the server. So I need a QString.

                          1 Reply Last reply Reply Quote 0
                          • SGaist
                            SGaist Lifetime Qt Champion last edited by

                            Hi,

                            As already explained by my fellows, use QByteArray. QString is for string manipulation, it's not what you are doing. You are sending data through network. Therefore you are creating a base64 QByteArray that represent your image's binary data. There's no need for any QString in between.

                            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 Reply Quote 1
                            • M
                              Mikeeeeee last edited by

                              But as I write in Qbytearray json?
                              And why this doesn't it work:

                                  /*QString imageString = QString::fromLocal8Bit(bArray);
                                  bArray2 = QByteArray::fromHex(imageString.toLocal8Bit());*/
                                  QString imageString = QString::fromLatin1(bArray);
                                  bArray2 = QByteArray::fromHex(imageString.toLatin1());
                                  if (bArray != bArray2) {qDebug()<<"bArray != bArray2";}
                                  else {"bArray == bArray2";}
                              

                              debug return: bArray != bArray2

                              1 Reply Last reply Reply Quote 0
                              • SGaist
                                SGaist Lifetime Qt Champion last edited by

                                At one point you are encoding in base64 and the other you are decoding from hex. These are two completely different things.

                                Also, QString does a conversion to UTF16. So again, the fact that JSON contains text doesn't mean that you need to use QString.

                                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 Reply Quote 3
                                • M
                                  Mikeeeeee last edited by

                                  But as a json, you can write a qbytearray? And how to write the qbytearray to a string and then again in qbytearray?

                                  1 Reply Last reply Reply Quote 0
                                  • SGaist
                                    SGaist Lifetime Qt Champion last edited by

                                    Take a look at QJsonDocument, no QString there.

                                    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 Reply Quote 2
                                    • M
                                      Mikeeeeee last edited by

                                      But I can't write qbytearray to json. And how to write the qbytearray to a string and then again in qbytearray?

                                      aha_1980 1 Reply Last reply Reply Quote 0
                                      • SGaist
                                        SGaist Lifetime Qt Champion last edited by

                                        You have to understand that it's the QByteArray that will contain the JSON data.

                                        Again, read the documentation of QJsonDocument, the related classes and example code.

                                        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 Reply Quote 1
                                        • M
                                          Mikeeeeee last edited by

                                          I read the documentation, it does not specify the ability to add QByteArray to json. So how can I translate QByteArray to Qstring and then to QByteArray?

                                          1 Reply Last reply Reply Quote 0
                                          • aha_1980
                                            aha_1980 Lifetime Qt Champion @Mikeeeeee last edited by

                                            @mikeeeeee Then you need to read more carefully.

                                            https://doc.qt.io/qt-5/qjsondocument.html#toJson
                                            https://doc.qt.io/qt-5/qjsondocument.html#fromJson

                                            Qt has to stay free or it will die.

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post