Distinguish between text message and image message sent from server (both encoded in qstring)
-
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.
-
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.
-
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.
-
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 ?
-
@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.
-
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. -
Do you mean showing images ?
-
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));
-
A QtQuick Image element ?