From a744c61a436480d493a6228f5159a79114bd3982 Mon Sep 17 00:00:00 2001 From: feiyangqingyun Date: Sat, 20 Feb 2021 09:26:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9B=BE=E7=89=87=E6=96=87?= =?UTF-8?q?=E5=AD=97base64=E4=BA=92=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- QWidgetDemo.pro | 1 + base64/base64.pro | 20 +++++++ base64/frmbase64.cpp | 103 +++++++++++++++++++++++++++++++++ base64/frmbase64.h | 42 ++++++++++++++ base64/frmbase64.ui | 135 +++++++++++++++++++++++++++++++++++++++++++ base64/main.cpp | 31 ++++++++++ 6 files changed, 332 insertions(+) create mode 100644 base64/base64.pro create mode 100644 base64/frmbase64.cpp create mode 100644 base64/frmbase64.h create mode 100644 base64/frmbase64.ui create mode 100644 base64/main.cpp diff --git a/QWidgetDemo.pro b/QWidgetDemo.pro index 12f7dd5..881835d 100644 --- a/QWidgetDemo.pro +++ b/QWidgetDemo.pro @@ -34,6 +34,7 @@ SUBDIRS += videowidget #通用视频控件 SUBDIRS += screenwidget #屏幕截图控件 SUBDIRS += imageswitch #图片开关控件 SUBDIRS += netserver #网络中转服务器 +SUBDIRS += base64 #图片文字base64互换 win32 { SUBDIRS += ffmpegdemo #视频流播放ffmpeg内核 diff --git a/base64/base64.pro b/base64/base64.pro new file mode 100644 index 0000000..850513e --- /dev/null +++ b/base64/base64.pro @@ -0,0 +1,20 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2017-02-08T09:21:04 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = base64 +TEMPLATE = app +DESTDIR = $$PWD/../bin +CONFIG += warn_off + +SOURCES += main.cpp +SOURCES += frmbase64.cpp +HEADERS += frmbase64.h +FORMS += frmbase64.ui + diff --git a/base64/frmbase64.cpp b/base64/frmbase64.cpp new file mode 100644 index 0000000..f8f3aab --- /dev/null +++ b/base64/frmbase64.cpp @@ -0,0 +1,103 @@ +#pragma execution_character_set("utf-8") + +#include "frmbase64.h" +#include "ui_frmbase64.h" +#include "qfiledialog.h" +#include "qbuffer.h" +#include "qdebug.h" + +frmBase64::frmBase64(QWidget *parent) : QWidget(parent), ui(new Ui::frmBase64) +{ + ui->setupUi(this); +} + +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)"); + if (!fileName.isEmpty()) { + ui->txtFile->setText(fileName); + QPixmap pix(fileName); + pix = pix.scaled(ui->labImage->size() - QSize(4, 4), Qt::KeepAspectRatio); + ui->labImage->setPixmap(pix); + } +} + +void frmBase64::on_btnClear_clicked() +{ + ui->txtFile->clear(); + ui->txtText->clear(); + ui->txtBase64->clear(); + ui->labImage->clear(); +} + +void frmBase64::on_btnImageToBase64_clicked() +{ + QString fileName = ui->txtFile->text().trimmed(); + if (!fileName.isEmpty()) { + ui->txtBase64->setText(getImageData(QImage(fileName))); + } +} + +void frmBase64::on_btnBase64ToImage_clicked() +{ + QString text = ui->txtBase64->toPlainText().trimmed(); + if (!text.isEmpty()) { + QPixmap pix = QPixmap::fromImage(getImage(text)); + pix = pix.scaled(ui->labImage->size() - QSize(4, 4), Qt::KeepAspectRatio); + ui->labImage->setPixmap(pix); + } +} + +void frmBase64::on_btnTextToBase64_clicked() +{ + QString text = ui->txtText->text().trimmed(); + if (!text.isEmpty()) { + ui->txtBase64->setText(getBase64(text)); + } +} + +void frmBase64::on_btnBase64ToText_clicked() +{ + QString text = ui->txtBase64->toPlainText().trimmed(); + if (!text.isEmpty()) { + ui->txtText->setText(getData(text)); + } +} diff --git a/base64/frmbase64.h b/base64/frmbase64.h new file mode 100644 index 0000000..2762294 --- /dev/null +++ b/base64/frmbase64.h @@ -0,0 +1,42 @@ +#ifndef FRMBASE64_H +#define FRMBASE64_H + +#include + +namespace Ui { +class frmBase64; +} + +class frmBase64 : public QWidget +{ + Q_OBJECT + +public: + explicit frmBase64(QWidget *parent = 0); + ~frmBase64(); + +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(); + void on_btnImageToBase64_clicked(); + void on_btnBase64ToImage_clicked(); + void on_btnTextToBase64_clicked(); + void on_btnBase64ToText_clicked(); +}; + +#endif // FRMBASE64_H diff --git a/base64/frmbase64.ui b/base64/frmbase64.ui new file mode 100644 index 0000000..db6836c --- /dev/null +++ b/base64/frmbase64.ui @@ -0,0 +1,135 @@ + + + frmBase64 + + + + 0 + 0 + 644 + 530 + + + + Widget + + + + + + + + + + 100 + 0 + + + + 打开文件 + + + + + + + + 100 + 0 + + + + 图片转base64 + + + + + + + + 100 + 0 + + + + base64转图片 + + + + + + + feiyangqingyun QQ: 517216493 + + + + + + + + 100 + 0 + + + + 清空数据 + + + + + + + + 100 + 0 + + + + 文字转base64 + + + + + + + + 100 + 0 + + + + base64转文字 + + + + + + + + 0 + 0 + + + + QFrame::Box + + + QFrame::Sunken + + + + + + Qt::AlignCenter + + + + + + + + + + + + diff --git a/base64/main.cpp b/base64/main.cpp new file mode 100644 index 0000000..b5acda2 --- /dev/null +++ b/base64/main.cpp @@ -0,0 +1,31 @@ +#pragma execution_character_set("utf-8") + +#include "frmbase64.h" +#include +#include + +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 _MSC_VER + QTextCodec *codec = QTextCodec::codecForName("gbk"); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); +#endif + QTextCodec::setCodecForLocale(codec); + QTextCodec::setCodecForCStrings(codec); + QTextCodec::setCodecForTr(codec); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); + QTextCodec::setCodecForLocale(codec); +#endif + + frmBase64 w; + w.setWindowTitle("图片文字base64编码互换"); + w.show(); + + return a.exec(); +}