Qt QImage和Base64之间的转换

今天同事让帮忙将bmp图像转换到Base64格式的字符串,尝试了一下,

直接上代码吧!!(环境:vs2015 + Qt5.9)

 

头文件:

#pragma once

#include 
#include "ui_Bmp2Base64vs.h"

class QImage;

class Bmp2Base64vs : public QMainWindow
{
    Q_OBJECT

public:
    Bmp2Base64vs(QWidget *parent = Q_NULLPTR);

    bool load_img_from_file(const QString &file_name);

    //image 转 base64 字符串
    QString bmp_2_base64_string(const QImage& img);

    QString bmp_2_base64_string_from_file(const QString& file_name);

    //base64 转 QImage
    QImage base64_string_2_img(const QString& str);

private slots:
    void on_click_btn_open_img();
    void on_click_btn_convert_2_Base64_string();
    void on_click_btn_base64_2_img();

private:
    Ui::Bmp2Base64vsClass ui;

    QString m_file_name;
    QImage m_src_img;
    QString m_base64_string;
};

 

cpp文件:

#include "Bmp2Base64vs.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

Bmp2Base64vs::Bmp2Base64vs(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);

    connect(ui.btn_open_img, SIGNAL(clicked(bool)), this, SLOT(on_click_btn_open_img()));
    connect(ui.btn_convert_2_Base64_string, SIGNAL(clicked(bool)), this, SLOT(on_click_btn_convert_2_Base64_string()));
    connect(ui.btn_base64_2_img, SIGNAL(clicked(bool)), this, SLOT(on_click_btn_base64_2_img()));
}

bool Bmp2Base64vs::load_img_from_file(const QString & file_name)
{
    QImageReader rd(file_name);
    rd.setAutoTransform(true);

    m_src_img = rd.read();

    ui.lb_show_src_img->setPixmap(QPixmap::fromImage(m_src_img));

    return true;
}

QString Bmp2Base64vs::bmp_2_base64_string(const QImage & img)
{
    QByteArray ba;
    QBuffer buf(&ba);
    buf.open(QIODevice::WriteOnly);

    img.save(&buf, "BMP");
    QByteArray ba2 = ba.toBase64();
    QString b64str = QString::fromLatin1(ba2);

    return b64str;
}

QString Bmp2Base64vs::bmp_2_base64_string_from_file(const QString & file_name)
{
    QImage img(m_file_name);
    QByteArray ba;
    QBuffer buf(&ba);
    buf.open(QIODevice::WriteOnly);

    img.save(&buf, "BMP");
    QByteArray ba2 = ba.toBase64();
    QString b64str = QString::fromLatin1(ba2);

    return b64str;
}

//base64 转 QImage
QImage Bmp2Base64vs::base64_string_2_img(const QString & str)
{
    QImage img;
    QByteArray arr_base64 = str.toLatin1();
    img.loadFromData(QByteArray::fromBase64(arr_base64));

    //ok too
    //QPixmap pic;
    //pic.loadFromData(QByteArray::fromBase64(arr_base64));

    return img;
}

//QImage转到Base64字符串
void Bmp2Base64vs::on_click_btn_convert_2_Base64_string()
{
    //调用方法一
    QString str = bmp_2_base64_string_from_file(m_file_name);
    qDebug() << "convert to base64 string" << str << endl;
    ui.ldt_show_base64_string->setText(str);
    m_base64_string = str; //保存base64字符串

    //调用方法二
    //QString str = bmp_2_base64_string(m_src_img);
    //qDebug() << "convert to base64 string" << str << endl;
    //ui.ldt_show_base64_string->setText(str);
    //m_base64_string = str; //保存base64字符串
}

//base64字符串转到qimage
void Bmp2Base64vs::on_click_btn_base64_2_img()
{
    QImage img = base64_string_2_img(m_base64_string);

    ui.lb_show_src_img_2->setPixmap(QPixmap::fromImage(img));
}

//打开图像
void Bmp2Base64vs::on_click_btn_open_img()
{
    qDebug() << "open image" << endl;

    QFileDialog dlg(this);
    dlg.setNameFilter(tr("Images (*.bmp *png)"));

    m_file_name = QFileDialog::getOpenFileName(
        this, "select a image", "D:",
        "Images (*.bmp *png)");

    qDebug() << m_file_name << endl;

    load_img_from_file(m_file_name);
}

 

如果您有更高效更好的方法请告诉我,谢谢,

愿上帝赐福与你!?

 

转载于:https://www.cnblogs.com/craigtao/p/10179583.html

你可能感兴趣的:(Qt QImage和Base64之间的转换)