I want to write function which saves input base64 string as a png image to local. How can I do it in Qt?
data:image/png;base64 tells the browser that the data is inline, is a png image and is in this case base64 encoded. The encoding is needed because png images can contain bytes that are invalid inside a HTML document (or within the HTTP protocol even).
To save a base64-encoded image to disk with Node. js, we can use the fs. writeFile method.
Base64 encoding is a way to encode binary data in ASCII text. It's primarily used to store or transfer images, audio files, and other media online. It is also often used when there are limitations on the characters that can be used in a filename for various reasons.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With