Qt将base64转换为QImage将返回null

时间:2017-08-09 06:53:58

标签: c++ image qt base64 converter

我有问题。我将base64转换为QImage,但之后该图像始终为null。这是代码:

void convertAll(QString path)
{
    QDir *dir = new QDir(path);
    QDirIterator *iter = new QDirIterator(*dir);
    QVector<QFile*> files;

    QTextStream cout(stdout);

    cout << "Finding files...\n";
    cout.flush();

    while (iter->hasNext())
    {
        iter->next();
        if (iter->fileInfo().completeSuffix() == "txt")
            files.push_back(new QFile(iter->filePath()));
    }

    delete iter;

    cout << "Converting files to images...\n";
    cout.flush();

    foreach (QFile *file, files)
    {
        QString filename = QFileInfo(*file).fileName();
        filename.remove(".txt");

        file->open(QIODevice::ReadOnly);
        QTextStream stream(file);

        cout << "Reading file...\n";
        cout.flush();

        QByteArray base64Data = stream.readAll().toUtf8();
        cout << "Reading is completed!\n";
        cout.flush();
        file->close();


        QImage *image = new QImage();
        cout << "Converting...\n";
        cout.flush();


        image->loadFromData(QByteArray::fromBase64(base64Data), "GIF");
        if (!image->isNull())
        {
            cout << "Converting is completed! Saving...\n";
            cout.flush();
        }
        else
        {
            cout << "Error converting to image!\n\n";
            cout.flush();
            delete image;
            delete file;
            continue;
        }

        if (image->save("/home/prosperis/ProgramFiles/Content/" + filename + ".bmp", "BMP"))
        {
            cout << "Saved successful!\nFile " << filename << ".bmp has been made!\n\n";
            cout.flush();
        }
        else
        {
            cout << "Error saving the file!\n\n";
            cout.flush();
        }


        delete image;
        delete file;
    }

    delete dir;
}

结果截图:

Result

Base64示例:

  

数据:图像/ GIF; BASE64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM / rhtAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAADxSURBVFhH7ZSxDYMwEEW9RdqU9MhFOho6WrwAE0SsESGlyABpvEU2yB5ZItLFnG1kkJEgipUjuSdZWG78 + HdnwTAMAR73J7gtXTYjOSd61Wc6PxCTJCXYM5X8muDldJi9OJQkKdjj + 5KsoGcTgrHhSc5PJei2iVA1qNvRrBoyIYbLiAjmUBi5Sjc0BbPOJKdL2LVG0HzdMbJEMG16WNoGpBQUBW1pC2VLikl2OR3BqRAtQSzteCBigkv4vKAsoQpK65HaTHK7JyA4vHnxtVYy4ZCEjAdmDSyIYF / a99CdLCaxoE0u7MF3UmQYhmGYv0SIF0Zn9rmd3QoAAAAAAElFTkSuQmCC

如果我删除data:image/gif;base64,

,它也无法正常工作

1 个答案:

答案 0 :(得分:1)

我用这个简单的例子尝试了它,它可以在我的机器上运行。

{  
   "StartAddress":"Colombo",
   "EndAddress":"Kandy",
   "DepartureAddress":"Kollupitiya, Colombo",
   "ArrivalAddress":"Peradeniya, Kandy",
   "CreatedDate":"2017-07-30",
   "Date":"2017-07-30",
   "Time":"2017-07-30",
   "IsLadiesOnly":true,
   "IpAddress":"fe80::8638:38ff:fec8:ea50%wlan0",
   "Country":"Srilanka",
   "VehicleId":"1129",
   "StartLocation":[  
      6.9270974,
      79.8607731
   ],
   "EndLocation":[  
      7.2916216,
      80.6341326
   ],
   "Points":"k}gi@y{lf",
   "Route":{  
      "Bounds":{  
         "NorthEast":[  
            7.2916216,
            80.6341326
         ],
         "SouthWest":[  
            6.9270974,
            79.8607731
         ]
      },
      "Legs":[  
         {  
            "LegId":1,
            "Distance":14904,
            "Duration":1941,
            "StartAddress":"Colombo",
            "EndAddress":"Kadawatha",
            "StartLocation":[  
               6.9270974,
               79.8612478
            ],
            "EndLocation":[  
               7.0011125,
               79.95000750000001
            ],
            "Ancestors":[  

            ],
            "Price":745
         },
         {  
            "LegId":2,
            "Distance":63040,
            "Duration":6209,
            "StartAddress":"Kadawatha",
            "EndAddress":"Kegalle",
            "StartLocation":[  
               7.0011125,
               79.95000750000001
            ],
            "EndLocation":[  
               7.251436200000001,
               80.3466076
            ],
            "Ancestors":[  
               "Colombo"
            ],
            "Price":3152
         },
         {  
            "LegId":3,
            "Distance":38990,
            "Duration":4430,
            "StartAddress":"Kegalle",
            "EndAddress":"Kandy",
            "StartLocation":[  
               7.251436200000001,
               80.3466076
            ],
            "EndLocation":[  
               7.2901864,
               80.6338425
            ],
            "Ancestors":[  
               "Colombo",
               "Kadawatha"
            ],
            "Price":1950
         }
      ]
   },
   "TotalPrice":"5847.0",
   "SeatCount":1,
   "Detour":1,
   "Luggage":2,
   "DetoursDescription":"10 Minutes",
   "LuggageDescription":"Small Luggage",
   "Notes":"new ride"
}