qt_demoe/ui/core_base/base64helper.cpp

42 lines
973 B
C++
Raw Normal View History

2021-09-16 06:48:38 +00:00
#include "base64helper.h"
2021-07-31 07:02:53 +00:00
#include "qbuffer.h"
#include "qdebug.h"
2021-09-16 06:48:38 +00:00
QString Base64Helper::imageToBase64(const QImage &image)
2021-07-31 07:02:53 +00:00
{
return QString(imageToBase64x(image));
}
2021-09-16 06:48:38 +00:00
QByteArray Base64Helper::imageToBase64x(const QImage &image)
2021-07-31 07:02:53 +00:00
{
//这个转换可能比较耗时建议在线程中执行
QByteArray data;
QBuffer buffer(&data);
image.save(&buffer, "JPG");
data = data.toBase64();
return data;
}
2021-09-16 06:48:38 +00:00
QImage Base64Helper::base64ToImage(const QString &data)
2021-07-31 07:02:53 +00:00
{
return base64ToImagex(data.toUtf8());
}
2021-09-16 06:48:38 +00:00
QImage Base64Helper::base64ToImagex(const QByteArray &data)
2021-07-31 07:02:53 +00:00
{
//这个转换可能比较耗时建议在线程中执行
QImage image;
image.loadFromData(QByteArray::fromBase64(data));
return image;
}
2021-09-16 06:48:38 +00:00
QString Base64Helper::textToBase64(const QString &text)
2021-07-31 07:02:53 +00:00
{
2022-11-01 06:28:21 +00:00
return QString(text.toUtf8().toBase64());
2021-07-31 07:02:53 +00:00
}
2021-09-16 06:48:38 +00:00
QString Base64Helper::base64ToText(const QString &text)
2021-07-31 07:02:53 +00:00
{
2022-11-01 06:28:21 +00:00
return QString(QByteArray::fromBase64(text.toUtf8()));
2021-07-31 07:02:53 +00:00
}