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. Distinguish between text message and image message sent from server (both encoded in qstring)

Distinguish between text message and image message sent from server (both encoded in qstring)

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 7.4k Views 2 Watching
  • 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.
  • V Offline
    V Offline
    Vasudha
    wrote on last edited by
    #1

    How to distinguish between a text message and an image (being sent as a QString to server) which the server sends to client? Since both are in QString format.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      U can try some thing like this. Take the string as QByteArray and load this into QImage or QPixmap. Check if QImage.isNull() or QPixmap.isNull(). If it is NULL it confirms that it is not a image & possibly it is text.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      V 1 Reply Last reply
      3
      • L Offline
        L Offline
        luca
        wrote on last edited by
        #3

        Some images format has a kind of header (some starting byte sequence) so you could check it.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Out of curiosity, are you just sending raw data from your server ? Isn't there any protocol between your server and client ?

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

          V 1 Reply Last reply
          2
          • dheerendraD dheerendra

            U can try some thing like this. Take the string as QByteArray and load this into QImage or QPixmap. Check if QImage.isNull() or QPixmap.isNull(). If it is NULL it confirms that it is not a image & possibly it is text.

            V Offline
            V Offline
            Vasudha
            wrote on last edited by
            #5

            @dheerendra I think this will do the trick :) Thanks.

            1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              Out of curiosity, are you just sending raw data from your server ? Isn't there any protocol between your server and client ?

              V Offline
              V Offline
              Vasudha
              wrote on last edited by
              #6

              @SGaist

              QImage image(fileName);
              QByteArray ba;
              qDebug() << "Sending Image of size " << image.byteCount();
              
              ba.append((char *)image.bits(),image.byteCount());
              if (image.byteCount() != ba.size()) {
                  qDebug() << "Image not encoded correctly";
              }
              qDebug() << "Image json- " << QJsonDocument::fromJson(ba);
              

              I'm sending the image like this. Image to bytearray to jsonDocument. But now the issue is that this JsonDocument is null. The documentation says we can directly use bytearray (http://doc.qt.io/qt-5/qjsondocument.html#fromJson) but its not working.
              I have to send the message to server in a specified json format having "id" , "method", and "params".
              Params contain the message field (either text or image).

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vasudha
                wrote on last edited by
                #7

                I also tried this-
                QImage image(fileName);
                QByteArray ba;

                ba.append((char *)image.bits(),image.byteCount());
                if (image.byteCount() != ba.size()) {
                    qDebug() << "Image not encoded correctly";
                }
                
                QString message(ba);
                QString roomID("rID");
                QString json = "{\"rid\": \"%1\", \"msg\": \"%2\"}";
                json = json.arg(roomID, message);
                qDebug() << "Image Json- " << QJsonDocument::fromJson(json.toUtf8());
                

                But even QJsonDocument::fromJson(json.toUtf8()); returns NULL for images; works fine for text messages.

                1 Reply Last reply
                1
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  This is wrong on several levels:

                  • Why load your image in a QImage ? You want to transmit the content of the file, no ?
                  • JSON is text based, you are trying to send binary data

                  If you want to send binary data in JSON you have to first convert them using e.g. base64.

                  Since you are sending JSON anyway, why not have a field that states the type of the content of params rather than doing guesses ?

                  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
                  4
                  • V Offline
                    V Offline
                    Vasudha
                    wrote on last edited by
                    #9

                    Having a type field is a good idea.
                    And yeah, I just want to transmit the content of the file. Shouldn't I be loading the image first? How else can it be done?

                    jsulmJ 1 Reply Last reply
                    0
                    • V Vasudha

                      Having a type field is a good idea.
                      And yeah, I just want to transmit the content of the file. Shouldn't I be loading the image first? How else can it be done?

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by jsulm
                      #10

                      @Vasudha You need to encode the content of the image file (no need to use QImage, just read the file using QFile) as base64 and add this encoded string as a field in your JSON:

                      {"type": "image", "data": ".PUT HERE BASE64 ENCODED IMAGE DATA"}
                      

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      3
                      • V Offline
                        V Offline
                        Vasudha
                        wrote on last edited by
                        #11

                        Okay So I'm sending msgs like this-

                            QFile file(fileName);
                            if (!file.open(QFile::ReadOnly)) {
                                qDebug() << "Cannot open the selected file";
                                return;
                            }
                            QByteArray block;
                            block = file.readAll();
                            block.toBase64();
                        
                            QString message(block);
                            QString roomID("rID");
                            QString type("image");
                            qDebug() << "base64 image- " << message;
                        
                            QString json = "{\"rid\": \"%1\", \"msg\": \"%2\", \"type\": \"%3\"}";
                            json = json.arg(roomID, message, type);
                        
                            sendMessage("sendMessage", QJsonDocument::fromJson(json.toUtf8()));
                        

                        And receiving them like-

                                    QString type = result.value("type").toString();
                                    QString msg = result.value("msg").toString();
                                    
                                    if (type == "image"){
                                        QByteArray decodedImage;
                                        QByteArray image;
                                        QPixmap pixmap;
                                        QLabel label;
                        
                                        decodedImage.append(msg);
                                        image = QByteArray::fromBase64(decodedImage);
                                        pixmap.loadFromData(image,0,Qt::AutoColor);
                                        label.setPixmap(pixmap);
                                        label.show();
                                     }
                        

                        But only "����" this is displayed instead of the image.

                        VRoninV 1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Use QByteArray directly rather than QString, that will avoid unless conversions.

                          Also your label variable only exists for the lifetime of the if block in case of type being image so it won't even be shown.

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

                          V 1 Reply Last reply
                          4
                          • SGaistS SGaist

                            Use QByteArray directly rather than QString, that will avoid unless conversions.

                            Also your label variable only exists for the lifetime of the if block in case of type being image so it won't even be shown.

                            V Offline
                            V Offline
                            Vasudha
                            wrote on last edited by
                            #13

                            @SGaist After correcting this part too the result was same. I realize I have been mixing QML with QWidgets and that's what is causing problems. Is there any other way of receiving images without using QWidgets?

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Do you mean showing images ?

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

                              V 1 Reply Last reply
                              0
                              • V Vasudha

                                Okay So I'm sending msgs like this-

                                    QFile file(fileName);
                                    if (!file.open(QFile::ReadOnly)) {
                                        qDebug() << "Cannot open the selected file";
                                        return;
                                    }
                                    QByteArray block;
                                    block = file.readAll();
                                    block.toBase64();
                                
                                    QString message(block);
                                    QString roomID("rID");
                                    QString type("image");
                                    qDebug() << "base64 image- " << message;
                                
                                    QString json = "{\"rid\": \"%1\", \"msg\": \"%2\", \"type\": \"%3\"}";
                                    json = json.arg(roomID, message, type);
                                
                                    sendMessage("sendMessage", QJsonDocument::fromJson(json.toUtf8()));
                                

                                And receiving them like-

                                            QString type = result.value("type").toString();
                                            QString msg = result.value("msg").toString();
                                            
                                            if (type == "image"){
                                                QByteArray decodedImage;
                                                QByteArray image;
                                                QPixmap pixmap;
                                                QLabel label;
                                
                                                decodedImage.append(msg);
                                                image = QByteArray::fromBase64(decodedImage);
                                                pixmap.loadFromData(image,0,Qt::AutoColor);
                                                label.setPixmap(pixmap);
                                                label.show();
                                             }
                                

                                But only "����" this is displayed instead of the image.

                                VRoninV Offline
                                VRoninV Offline
                                VRonin
                                wrote on last edited by
                                #15

                                @Vasudha said in Distinguish between text message and image message sent from server (both encoded in qstring):

                                block.toBase64();

                                That is a const method. Do you mean block=block.toBase64();?

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                V 1 Reply Last reply
                                2
                                • SGaistS SGaist

                                  Do you mean showing images ?

                                  V Offline
                                  V Offline
                                  Vasudha
                                  wrote on last edited by
                                  #16

                                  @SGaist yes

                                  1 Reply Last reply
                                  1
                                  • VRoninV VRonin

                                    @Vasudha said in Distinguish between text message and image message sent from server (both encoded in qstring):

                                    block.toBase64();

                                    That is a const method. Do you mean block=block.toBase64();?

                                    V Offline
                                    V Offline
                                    Vasudha
                                    wrote on last edited by
                                    #17

                                    @VRonin yeah

                                    1 Reply Last reply
                                    1
                                    • VRoninV Offline
                                      VRoninV Offline
                                      VRonin
                                      wrote on last edited by VRonin
                                      #18
                                      
                                      QFile file(fileName);
                                          if (!file.open(QFile::ReadOnly)) {
                                              qDebug() << "Cannot open the selected file";
                                              return;
                                          }
                                          const QString block = QString::fromLatin1(file.readAll().toBase64());
                                      const QString roomID("rID");
                                          const QString type("image")
                                      QJsonObject jSonImage;
                                      jSonImage.insert("rid",roomID);
                                      jSonImage.insert("type",type);
                                      jSonImage.insert("msg",block);
                                      sendMessage("sendMessage", QJsonDocument(jSonImage));
                                      

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      V 1 Reply Last reply
                                      4
                                      • VRoninV VRonin
                                        
                                        QFile file(fileName);
                                            if (!file.open(QFile::ReadOnly)) {
                                                qDebug() << "Cannot open the selected file";
                                                return;
                                            }
                                            const QString block = QString::fromLatin1(file.readAll().toBase64());
                                        const QString roomID("rID");
                                            const QString type("image")
                                        QJsonObject jSonImage;
                                        jSonImage.insert("rid",roomID);
                                        jSonImage.insert("type",type);
                                        jSonImage.insert("msg",block);
                                        sendMessage("sendMessage", QJsonDocument(jSonImage));
                                        
                                        V Offline
                                        V Offline
                                        Vasudha
                                        wrote on last edited by
                                        #19

                                        @VRonin This was very smart. The image is being sent successfully but how to show it (at receiving end) without using QWidgets (after decoding it)?

                                        1 Reply Last reply
                                        1
                                        • SGaistS Offline
                                          SGaistS Offline
                                          SGaist
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          A QtQuick Image element ?

                                          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
                                          3

                                          • Login

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