I want to write function which saves input base64 string as a png image to local. How can I do it in Qt?
This is a simple program I wrote sometime ago for testing png and base64. It accepts a png encoded in base64 from standard input, show it and than save it to specified path (output.png if nothing has been specified). This wont work if base64 string is not a png.
#include <QtCore>
#include <QApplication>
#include <QImage>
#include <QByteArray>
#include <QTextStream>
#include <QDebug>
#include <QLabel>
int main(int argc, char *argv[]) {
QString filename = "output.png";
if (argc > 1) {
filename = argv[1];
}
QApplication a(argc, argv);
QTextStream stream(stdin);
qDebug() << "reading";
//stream.readAll();
qDebug() << "read complete";
QByteArray base64Data = stream.readAll().toAscii();
QImage image;
qDebug() << base64Data;
image.loadFromData(QByteArray::fromBase64(base64Data), "PNG");
QLabel label(0);
label.setPixmap(QPixmap::fromImage(image));
label.show();
qDebug() << "writing";
image.save(filename, "PNG");
qDebug() << "write complete";
return a.exec();
}