彻底改版2.0
This commit is contained in:
41
tool/base64helper/base64helper.cpp
Normal file
41
tool/base64helper/base64helper.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "base64helper.h"
|
||||
#include "qbuffer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
QString Base64Helper::imageToBase64(const QImage &image)
|
||||
{
|
||||
return QString(imageToBase64x(image));
|
||||
}
|
||||
|
||||
QByteArray Base64Helper::imageToBase64x(const QImage &image)
|
||||
{
|
||||
//这个转换可能比较耗时建议在线程中执行
|
||||
QByteArray data;
|
||||
QBuffer buffer(&data);
|
||||
image.save(&buffer, "JPG");
|
||||
data = data.toBase64();
|
||||
return data;
|
||||
}
|
||||
|
||||
QImage Base64Helper::base64ToImage(const QString &data)
|
||||
{
|
||||
return base64ToImagex(data.toUtf8());
|
||||
}
|
||||
|
||||
QImage Base64Helper::base64ToImagex(const QByteArray &data)
|
||||
{
|
||||
//这个转换可能比较耗时建议在线程中执行
|
||||
QImage image;
|
||||
image.loadFromData(QByteArray::fromBase64(data));
|
||||
return image;
|
||||
}
|
||||
|
||||
QString Base64Helper::textToBase64(const QString &text)
|
||||
{
|
||||
return QString(text.toLocal8Bit().toBase64());
|
||||
}
|
||||
|
||||
QString Base64Helper::base64ToText(const QString &text)
|
||||
{
|
||||
return QString(QByteArray::fromBase64(text.toLocal8Bit()));
|
||||
}
|
||||
37
tool/base64helper/base64helper.h
Normal file
37
tool/base64helper/base64helper.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef BASE64HELPER_H
|
||||
#define BASE64HELPER_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 Base64Helper
|
||||
#else
|
||||
class Base64Helper
|
||||
#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 // BASE64HELPER_H
|
||||
19
tool/base64helper/base64helper.pro
Normal file
19
tool/base64helper/base64helper.pro
Normal file
@@ -0,0 +1,19 @@
|
||||
QT += core gui
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
|
||||
|
||||
TARGET = base64helper
|
||||
TEMPLATE = app
|
||||
DESTDIR = $$PWD/../bin
|
||||
CONFIG += warn_off
|
||||
|
||||
SOURCES += main.cpp
|
||||
|
||||
SOURCES += frmbase64helper.cpp
|
||||
SOURCES += base64helper.cpp
|
||||
|
||||
HEADERS += frmbase64helper.h
|
||||
HEADERS += base64helper.h
|
||||
|
||||
FORMS += frmbase64helper.ui
|
||||
|
||||
97
tool/base64helper/frmbase64helper.cpp
Normal file
97
tool/base64helper/frmbase64helper.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmbase64helper.h"
|
||||
#include "ui_frmbase64helper.h"
|
||||
#include "base64helper.h"
|
||||
#include "qfiledialog.h"
|
||||
#include "qelapsedtimer.h"
|
||||
#include "qdebug.h"
|
||||
|
||||
frmBase64Helper::frmBase64Helper(QWidget *parent) : QWidget(parent), ui(new Ui::frmBase64Helper)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
frmBase64Helper::~frmBase64Helper()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmBase64Helper::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 frmBase64Helper::on_btnClear_clicked()
|
||||
{
|
||||
ui->txtFile->clear();
|
||||
ui->txtText->clear();
|
||||
ui->txtBase64->clear();
|
||||
ui->labImage->clear();
|
||||
}
|
||||
|
||||
void frmBase64Helper::on_btnImageToBase64_clicked()
|
||||
{
|
||||
//计时
|
||||
QElapsedTimer time;
|
||||
time.start();
|
||||
|
||||
QString fileName = ui->txtFile->text().trimmed();
|
||||
if (!fileName.isEmpty()) {
|
||||
ui->txtBase64->setText(Base64Helper::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 frmBase64Helper::on_btnBase64ToImage_clicked()
|
||||
{
|
||||
//计时
|
||||
QElapsedTimer time;
|
||||
time.start();
|
||||
|
||||
QString text = ui->txtBase64->toPlainText().trimmed();
|
||||
if (!text.isEmpty()) {
|
||||
QPixmap pix = QPixmap::fromImage(Base64Helper::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 frmBase64Helper::on_btnTextToBase64_clicked()
|
||||
{
|
||||
QString text = ui->txtText->text().trimmed();
|
||||
if (!text.isEmpty()) {
|
||||
ui->txtBase64->setText(Base64Helper::textToBase64(text));
|
||||
}
|
||||
}
|
||||
|
||||
void frmBase64Helper::on_btnBase64ToText_clicked()
|
||||
{
|
||||
QString text = ui->txtBase64->toPlainText().trimmed();
|
||||
if (!text.isEmpty()) {
|
||||
ui->txtText->setText(Base64Helper::base64ToText(text));
|
||||
}
|
||||
}
|
||||
30
tool/base64helper/frmbase64helper.h
Normal file
30
tool/base64helper/frmbase64helper.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef FRMBASE64HELPER_H
|
||||
#define FRMBASE64HELPER_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class frmBase64Helper;
|
||||
}
|
||||
|
||||
class frmBase64Helper : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmBase64Helper(QWidget *parent = 0);
|
||||
~frmBase64Helper();
|
||||
|
||||
private:
|
||||
Ui::frmBase64Helper *ui;
|
||||
|
||||
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 // FRMBASE64HELPER_H
|
||||
139
tool/base64helper/frmbase64helper.ui
Normal file
139
tool/base64helper/frmbase64helper.ui
Normal file
@@ -0,0 +1,139 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmBase64Helper</class>
|
||||
<widget class="QWidget" name="frmBase64Helper">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Widget</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<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>120</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>打开文件</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="2">
|
||||
<widget class="QPushButton" name="btnImageToBase64">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>120</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>图片转base64</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="3">
|
||||
<widget class="QPushButton" name="btnBase64ToImage">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>120</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>base64转图片</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QLineEdit" name="txtText">
|
||||
<property name="text">
|
||||
<string>游龙 feiyangqingyun QQ: 517216493</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="1">
|
||||
<widget class="QPushButton" name="btnClear">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>120</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>清空数据</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="2">
|
||||
<widget class="QPushButton" name="btnTextToBase64">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>120</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>文字转base64</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="3">
|
||||
<widget class="QPushButton" name="btnBase64ToText">
|
||||
<property name="minimumSize">
|
||||
<size>
|
||||
<width>120</width>
|
||||
<height>0</height>
|
||||
</size>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>base64转文字</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0" colspan="4">
|
||||
<widget class="QLabel" name="labImage">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="frameShape">
|
||||
<enum>QFrame::Box</enum>
|
||||
</property>
|
||||
<property name="frameShadow">
|
||||
<enum>QFrame::Sunken</enum>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0" colspan="4">
|
||||
<widget class="QTextEdit" name="txtBase64"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
31
tool/base64helper/main.cpp
Normal file
31
tool/base64helper/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmbase64helper.h"
|
||||
#include <QApplication>
|
||||
#include <QTextCodec>
|
||||
|
||||
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
|
||||
|
||||
frmBase64Helper w;
|
||||
w.setWindowTitle("图片文字base64编码互换");
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user