重写base64编码转换demo
parent
47daf5e186
commit
f3a944679c
|
@ -37,7 +37,7 @@ SUBDIRS += imageswitch #图片开关控件
|
|||
SUBDIRS += netserver #网络中转服务器
|
||||
SUBDIRS += base64 #图片文字base64互换
|
||||
SUBDIRS += smoothcurve #平滑曲线
|
||||
SUBDIRS += frameless #无边框方案支持win、linux、mac等
|
||||
SUBDIRS += frameless #跨平台无边框窗体
|
||||
|
||||
#限定windows系统加载下面的项目
|
||||
win32 {
|
||||
|
|
|
@ -49,7 +49,7 @@
|
|||
| 41 | miniblink | miniblink示例 |
|
||||
| 42 | base64 | 图片文字base64互换 |
|
||||
| 43 | smoothcurve | 平滑曲线 |
|
||||
| 44 | frameless | 地表最强最简单无边框方案 |
|
||||
| 44 | frameless | 跨平台无边框窗体 |
|
||||
|
||||
### 二、学习群
|
||||
1. **Qt交流大会群 853086607(雨田哥)**
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
#include "base64.h"
|
||||
#include "qbuffer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
QString Base64::imageToBase64(const QImage &image)
|
||||
{
|
||||
return QString(imageToBase64x(image));
|
||||
}
|
||||
|
||||
QByteArray Base64::imageToBase64x(const QImage &image)
|
||||
{
|
||||
//这个转换可能比较耗时建议在线程中执行
|
||||
QByteArray data;
|
||||
QBuffer buffer(&data);
|
||||
image.save(&buffer, "JPG");
|
||||
data = data.toBase64();
|
||||
return data;
|
||||
}
|
||||
|
||||
QImage Base64::base64ToImage(const QString &data)
|
||||
{
|
||||
return base64ToImagex(data.toUtf8());
|
||||
}
|
||||
|
||||
QImage Base64::base64ToImagex(const QByteArray &data)
|
||||
{
|
||||
//这个转换可能比较耗时建议在线程中执行
|
||||
QImage image;
|
||||
image.loadFromData(QByteArray::fromBase64(data));
|
||||
return image;
|
||||
}
|
||||
|
||||
QString Base64::textToBase64(const QString &text)
|
||||
{
|
||||
return QString(text.toLocal8Bit().toBase64());
|
||||
}
|
||||
|
||||
QString Base64::base64ToText(const QString &text)
|
||||
{
|
||||
return QString(QByteArray::fromBase64(text.toLocal8Bit()));
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef BASE64_H
|
||||
#define BASE64_H
|
||||
|
||||
/**
|
||||
* base64编码转换类 作者:feiyangqingyun(QQ:517216493) 2016-12-16
|
||||
* 1. 图片转base64字符串
|
||||
* 2. base64字符串转图片
|
||||
* 3. 字符转base64字符串
|
||||
* 4. base64字符串转字符
|
||||
* 5. 后期增加数据压缩
|
||||
* 6. Qt6对base64编码转换进行了重写效率提升至少200%
|
||||
*/
|
||||
|
||||
#include <QImage>
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_EXPORT Base64
|
||||
#else
|
||||
class Base64
|
||||
#endif
|
||||
|
||||
{
|
||||
public:
|
||||
//图片转base64字符串
|
||||
static QString imageToBase64(const QImage &image);
|
||||
static QByteArray imageToBase64x(const QImage &image);
|
||||
|
||||
//base64字符串转图片
|
||||
static QImage base64ToImage(const QString &data);
|
||||
static QImage base64ToImagex(const QByteArray &data);
|
||||
|
||||
//字符串与base64互转
|
||||
static QString textToBase64(const QString &text);
|
||||
static QString base64ToText(const QString &text);
|
||||
};
|
||||
|
||||
#endif // BASE64_H
|
|
@ -8,7 +8,12 @@ DESTDIR = $$PWD/../bin
|
|||
CONFIG += warn_off
|
||||
|
||||
SOURCES += main.cpp
|
||||
|
||||
SOURCES += frmbase64.cpp
|
||||
SOURCES += base64.cpp
|
||||
|
||||
HEADERS += frmbase64.h
|
||||
HEADERS += base64.h
|
||||
|
||||
FORMS += frmbase64.ui
|
||||
|
||||
|
|
|
@ -2,8 +2,9 @@
|
|||
|
||||
#include "frmbase64.h"
|
||||
#include "ui_frmbase64.h"
|
||||
#include "base64.h"
|
||||
#include "qfiledialog.h"
|
||||
#include "qbuffer.h"
|
||||
#include "qelapsedtimer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
frmBase64::frmBase64(QWidget *parent) : QWidget(parent), ui(new Ui::frmBase64)
|
||||
|
@ -16,39 +17,6 @@ frmBase64::~frmBase64()
|
|||
delete ui;
|
||||
}
|
||||
|
||||
QString frmBase64::getImageData(const QImage &image)
|
||||
{
|
||||
return QString(getImageData2(image));
|
||||
}
|
||||
|
||||
QByteArray frmBase64::getImageData2(const QImage &image)
|
||||
{
|
||||
QByteArray imageData;
|
||||
QBuffer buffer(&imageData);
|
||||
image.save(&buffer, "JPG");
|
||||
imageData = imageData.toBase64();
|
||||
return imageData;
|
||||
}
|
||||
|
||||
QImage frmBase64::getImage(const QString &data)
|
||||
{
|
||||
QByteArray imageData = QByteArray::fromBase64(data.toLatin1());
|
||||
QImage image;
|
||||
image.loadFromData(imageData);
|
||||
return image;
|
||||
}
|
||||
|
||||
QString frmBase64::getBase64(const QString &data)
|
||||
{
|
||||
return QString(data.toLocal8Bit().toBase64());
|
||||
}
|
||||
|
||||
QString frmBase64::getData(const QString &base64)
|
||||
{
|
||||
QByteArray data = QByteArray::fromBase64(base64.toLocal8Bit());
|
||||
return QString(data);
|
||||
}
|
||||
|
||||
void frmBase64::on_btnOpen_clicked()
|
||||
{
|
||||
QString fileName = QFileDialog::getOpenFileName(this, "选择文件", "", "图片(*.png *.bmp *.jpg)");
|
||||
|
@ -70,27 +38,53 @@ void frmBase64::on_btnClear_clicked()
|
|||
|
||||
void frmBase64::on_btnImageToBase64_clicked()
|
||||
{
|
||||
//计时
|
||||
QElapsedTimer time;
|
||||
time.start();
|
||||
|
||||
QString fileName = ui->txtFile->text().trimmed();
|
||||
if (!fileName.isEmpty()) {
|
||||
ui->txtBase64->setText(getImageData(QImage(fileName)));
|
||||
ui->txtBase64->setText(Base64::imageToBase64(QImage(fileName)));
|
||||
}
|
||||
|
||||
//统计用时
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
||||
double elapsed = (double)time.nsecsElapsed() / 1000000;
|
||||
#else
|
||||
double elapsed = (double)time.elapsed();
|
||||
#endif
|
||||
QString strTime = QString::number(elapsed, 'f', 3);
|
||||
qDebug() << QString("用时 %1 毫秒").arg(strTime);
|
||||
}
|
||||
|
||||
void frmBase64::on_btnBase64ToImage_clicked()
|
||||
{
|
||||
//计时
|
||||
QElapsedTimer time;
|
||||
time.start();
|
||||
|
||||
QString text = ui->txtBase64->toPlainText().trimmed();
|
||||
if (!text.isEmpty()) {
|
||||
QPixmap pix = QPixmap::fromImage(getImage(text));
|
||||
QPixmap pix = QPixmap::fromImage(Base64::base64ToImage(text));
|
||||
pix = pix.scaled(ui->labImage->size() - QSize(4, 4), Qt::KeepAspectRatio);
|
||||
ui->labImage->setPixmap(pix);
|
||||
}
|
||||
|
||||
//统计用时
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
|
||||
double elapsed = (double)time.nsecsElapsed() / 1000000;
|
||||
#else
|
||||
double elapsed = (double)time.elapsed();
|
||||
#endif
|
||||
QString strTime = QString::number(elapsed, 'f', 3);
|
||||
qDebug() << QString("用时 %1 毫秒").arg(strTime);
|
||||
}
|
||||
|
||||
void frmBase64::on_btnTextToBase64_clicked()
|
||||
{
|
||||
QString text = ui->txtText->text().trimmed();
|
||||
if (!text.isEmpty()) {
|
||||
ui->txtBase64->setText(getBase64(text));
|
||||
ui->txtBase64->setText(Base64::textToBase64(text));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,6 +92,6 @@ void frmBase64::on_btnBase64ToText_clicked()
|
|||
{
|
||||
QString text = ui->txtBase64->toPlainText().trimmed();
|
||||
if (!text.isEmpty()) {
|
||||
ui->txtText->setText(getData(text));
|
||||
ui->txtText->setText(Base64::base64ToText(text));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,18 +18,6 @@ public:
|
|||
private:
|
||||
Ui::frmBase64 *ui;
|
||||
|
||||
private slots:
|
||||
//图片转base64编码
|
||||
QString getImageData(const QImage &image);
|
||||
QByteArray getImageData2(const QImage &image);
|
||||
//base64编码数据转图片
|
||||
QImage getImage(const QString &data);
|
||||
|
||||
//汉字转base64编码
|
||||
QString getBase64(const QString &data);
|
||||
//base64编码转汉字
|
||||
QString getData(const QString &base64);
|
||||
|
||||
private slots:
|
||||
void on_btnOpen_clicked();
|
||||
void on_btnClear_clicked();
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>644</width>
|
||||
<height>530</height>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
|
@ -15,13 +15,17 @@
|
|||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLineEdit" name="txtFile"/>
|
||||
<widget class="QLineEdit" name="txtFile">
|
||||
<property name="text">
|
||||
<string>E:/myFile/美女图片/2.jpg</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QPushButton" name="btnOpen">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>100</width>
|
||||
<width>110</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
|
@ -34,7 +38,7 @@
|
|||
<widget class="QPushButton" name="btnImageToBase64">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>100</width>
|
||||
<width>110</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
|
@ -47,7 +51,7 @@
|
|||
<widget class="QPushButton" name="btnBase64ToImage">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>100</width>
|
||||
<width>110</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
|
@ -59,7 +63,7 @@
|
|||
<item row="1" column="0">
|
||||
<widget class="QLineEdit" name="txtText">
|
||||
<property name="text">
|
||||
<string>feiyangqingyun QQ: 517216493</string>
|
||||
<string>游龙 feiyangqingyun QQ: 517216493</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
|
@ -67,7 +71,7 @@
|
|||
<widget class="QPushButton" name="btnClear">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>100</width>
|
||||
<width>110</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
|
@ -80,7 +84,7 @@
|
|||
<widget class="QPushButton" name="btnTextToBase64">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>100</width>
|
||||
<width>110</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
|
@ -93,7 +97,7 @@
|
|||
<widget class="QPushButton" name="btnBase64ToText">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>100</width>
|
||||
<width>110</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
|
|
|
@ -9,7 +9,7 @@ int main(int argc, char *argv[])
|
|||
QApplication a(argc, argv);
|
||||
a.setFont(QFont("Microsoft Yahei", 9));
|
||||
|
||||
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
|
||||
#if _MSC_VER
|
||||
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
||||
#else
|
||||
|
|
Loading…
Reference in New Issue