新增图片文字base64互换

master
feiyangqingyun 2021-02-20 09:26:02 +08:00
parent ac85752894
commit a744c61a43
6 changed files with 332 additions and 0 deletions

View File

@ -34,6 +34,7 @@ SUBDIRS += videowidget #通用视频控件
SUBDIRS += screenwidget #屏幕截图控件
SUBDIRS += imageswitch #图片开关控件
SUBDIRS += netserver #网络中转服务器
SUBDIRS += base64 #图片文字base64互换
win32 {
SUBDIRS += ffmpegdemo #视频流播放ffmpeg内核

20
base64/base64.pro Normal file
View File

@ -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

103
base64/frmbase64.cpp Normal file
View File

@ -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));
}
}

42
base64/frmbase64.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef FRMBASE64_H
#define FRMBASE64_H
#include <QWidget>
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

135
base64/frmbase64.ui Normal file
View File

@ -0,0 +1,135 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmBase64</class>
<widget class="QWidget" name="frmBase64">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>644</width>
<height>530</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"/>
</item>
<item row="0" column="1">
<widget class="QPushButton" name="btnOpen">
<property name="minimumSize">
<size>
<width>100</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>100</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>100</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>100</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>100</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>100</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
base64/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmbase64.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
frmBase64 w;
w.setWindowTitle("图片文字base64编码互换");
w.show();
return a.exec();
}