From f3a944679c3c435af2f079a883c787288c8beedc Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Sat, 31 Jul 2021 15:02:53 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99base64=E7=BC=96=E7=A0=81?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QWidgetDemo.pro | 2 +- README.md | 2 +- base64/base64.cpp | 41 ++++++++++++++++++++++++++ base64/base64.h | 37 +++++++++++++++++++++++ base64/base64.pro | 5 ++++ base64/frmbase64.cpp | 70 ++++++++++++++++++++------------------------ base64/frmbase64.h | 12 -------- base64/frmbase64.ui | 24 ++++++++------- base64/main.cpp | 2 +- 9 files changed, 132 insertions(+), 63 deletions(-) create mode 100644 base64/base64.cpp create mode 100644 base64/base64.h diff --git a/QWidgetDemo.pro b/QWidgetDemo.pro index d511f2e..82271a7 100644 --- a/QWidgetDemo.pro +++ b/QWidgetDemo.pro @@ -37,7 +37,7 @@ SUBDIRS += imageswitch #图片开关控件 SUBDIRS += netserver #网络中转服务器 SUBDIRS += base64 #图片文字base64互换 SUBDIRS += smoothcurve #平滑曲线 -SUBDIRS += frameless #无边框方案支持win、linux、mac等 +SUBDIRS += frameless #跨平台无边框窗体 #限定windows系统加载下面的项目 win32 { diff --git a/README.md b/README.md index 6ce02bd..0b47245 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ | 41 | miniblink | miniblink示例 | | 42 | base64 | 图片文字base64互换 | | 43 | smoothcurve | 平滑曲线 | -| 44 | frameless | 地表最强最简单无边框方案 | +| 44 | frameless | 跨平台无边框窗体 | ### 二、学习群 1. **Qt交流大会群 853086607(雨田哥)** diff --git a/base64/base64.cpp b/base64/base64.cpp new file mode 100644 index 0000000..1e0b100 --- /dev/null +++ b/base64/base64.cpp @@ -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())); +} diff --git a/base64/base64.h b/base64/base64.h new file mode 100644 index 0000000..2e864b7 --- /dev/null +++ b/base64/base64.h @@ -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 + +#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 diff --git a/base64/base64.pro b/base64/base64.pro index edec550..3b0f7fa 100644 --- a/base64/base64.pro +++ b/base64/base64.pro @@ -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 diff --git a/base64/frmbase64.cpp b/base64/frmbase64.cpp index f8f3aab..164f522 100644 --- a/base64/frmbase64.cpp +++ b/base64/frmbase64.cpp @@ -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)); } } diff --git a/base64/frmbase64.h b/base64/frmbase64.h index 2762294..cc76748 100644 --- a/base64/frmbase64.h +++ b/base64/frmbase64.h @@ -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(); diff --git a/base64/frmbase64.ui b/base64/frmbase64.ui index db6836c..a19e3f6 100644 --- a/base64/frmbase64.ui +++ b/base64/frmbase64.ui @@ -6,8 +6,8 @@ 0 0 - 644 - 530 + 800 + 600 @@ -15,13 +15,17 @@ - + + + E:/myFile/美女图片/2.jpg + + - 100 + 110 0 @@ -34,7 +38,7 @@ - 100 + 110 0 @@ -47,7 +51,7 @@ - 100 + 110 0 @@ -59,7 +63,7 @@ - feiyangqingyun QQ: 517216493 + 游龙 feiyangqingyun QQ: 517216493 @@ -67,7 +71,7 @@ - 100 + 110 0 @@ -80,7 +84,7 @@ - 100 + 110 0 @@ -93,7 +97,7 @@ - 100 + 110 0 diff --git a/base64/main.cpp b/base64/main.cpp index b5acda2..5260c98 100644 --- a/base64/main.cpp +++ b/base64/main.cpp @@ -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