2011-08-02 85 views

回答

4

这是一个简单的程序,我写较早前测试PNG和BASE64。 它接受来自标准输入base64编码一个PNG,显示它并比它保存到指定的路径(如果没有已经指定output.png)。 这不会工作,如果base64字符串不是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(); 
} 
1

您可以阅读常见问题,并要求具体问题...

过程是这样的: 的Base64(QString的) - >QByteArray - >QImage - >save to file

当然,你需要考虑到插件和出口能力写png和知道你的base64文件如何表示的图像...而且能够做到的逆过程最有可能。

+1

请看http://doc.qt.nokia.com/latest/qpixmap.html#loadFromData-3。 – Bill

相关问题