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



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



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



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


  • Lifetime Qt Champion

    Hi,

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



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



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



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


  • Lifetime Qt Champion

    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 ?



  • 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?


  • Moderators

    @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"}
    


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


  • Lifetime Qt Champion

    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.



  • @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?


  • Lifetime Qt Champion

    Do you mean showing images ?



  • @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();?



  • @SGaist yes



  • @VRonin yeah



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


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


  • Lifetime Qt Champion

    A QtQuick Image element ?



  • Thank you everyone :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.