重写base64编码转换demo
This commit is contained in:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user