Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I save base64 string as png image in Qt?

Tags:

c++

base64

png

qt

I want to write function which saves input base64 string as a png image to local. How can I do it in Qt?

like image 540
Gürcan Kavakçı Avatar asked Aug 02 '11 12:08

Gürcan Kavakçı


People also ask

What format is data image PNG Base64?

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).

How do I save an image in Base64 node JS?

To save a base64-encoded image to disk with Node. js, we can use the fs. writeFile method.

What is Base64 to image?

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.


1 Answers

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();
}
like image 194
Alessandro Pezzato Avatar answered Sep 27 '22 22:09

Alessandro Pezzato