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. QImage from JSON double encoded data
Qt 6.11 is out! See what's new in the release blog

QImage from JSON double encoded data

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 131 Views 1 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.
  • O Offline
    O Offline
    ollarch
    wrote last edited by
    #1

    Hi,

    I'm receiving an image through JSON.

    I cannot share file data due char limitation. Please download it on WeTransfer:

    https://we.tl/t-Z3tgCT7NSK

    It seems that the image has been double encoded: "Windows-1252" -> "UTF-8" -> "base64".

    How do I revert the double encoding?

    QFile qFile("image.json");
    if (qFile.open(QIODeviceBase::ReadOnly))
    {
    	QTextStream qStream(&qFile);
    	QByteArray qBuffer = qStream.readAll().toUtf8();
    	QJsonParseError* pqError = nullptr;
    	QJsonDocument qJsonDoc = QJsonDocument::fromJson(qBuffer, pqError);
    	if (!qJsonDoc.isNull())
    	{
    		QJsonObject qJsonObject = qJsonDoc.object();
    		if (qJsonObject.contains("IMG"))
    		{
                        QString base64 = qJsonObject.value("IMG").toString();
    
                        //Here I need to do the double decoding
                        QImage qImage;
    		}
    	}
    }
    

    Regards,

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

      Hi,

      I haven't used that class yet but I think you are looking for QStringDecoder.

      Hope it helps

      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
      1
      • O Offline
        O Offline
        ollarch
        wrote last edited by
        #3

        Hi,

        Finally I found a solution. It seems that the error starts with the data extracted from a Database that is bad encoded. The solution:

        QString qImgB64 = qJsonObject.value("IMG").toString();
        
        // Base64 → "corrupted bytes"
        QByteArray qDecoded = QByteArray::fromBase64(qImgB64.toLatin1());
        
        // UTF-8
        QString utf8Text = QStringDecoder(QStringDecoder::Utf8)(qDecoded);
        
        // Convert to CP1252
        QStringEncoder encoder(QStringEncoder::System); // or Windows-1252 "QStringEncoder encoder("Windows-1252");"
        QByteArray qJpeg = encoder(utf8Text);
        
        // Load into image
        QImage qImage;
        bool ok = qImage.loadFromData(qJpeg);
        

        Thanks

        1 Reply Last reply
        2
        • O ollarch has marked this topic as solved

        • Login

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