Update code from changes in private projects

This commit is contained in:
Alex Spataru
2023-10-12 22:42:07 -05:00
parent a834f5db60
commit 65f4173c27
2 changed files with 136 additions and 79 deletions
+82 -38
View File
@@ -20,60 +20,104 @@
* THE SOFTWARE.
*/
#include "QrCodeGenerator.h"
#include <sstream>
#include <string>
#include <QPainter>
#include <QTextStream>
#include <QSvgRenderer>
QrCodeGenerator::QrCodeGenerator(QObject *parent) : QObject(parent) {}
#include "QrCodeGenerator.h"
QImage QrCodeGenerator::generateQr(const QString &data, const quint16 size,
const quint16 borderSize,
qrcodegen::QrCode::Ecc errorCorrection) {
QByteArray byteArray = data.toUtf8();
const qrcodegen::QrCode qrCode = qrcodegen::QrCode::encodeText(byteArray.constData(),
errorCorrection);
/**
* @brief Default constructor for the QrCodeGenerator class.
* @param parent QObject parent
*/
QrCodeGenerator::QrCodeGenerator(QObject *parent)
: QObject(parent)
{
// No implementation needed for default constructor.
}
/**
* @brief Generates a QR code image from a given text string.
* @param data The data to encode in the QR code.
* @param size The image size for the generated QR code.
* @param borderSize The size of the border around the QR code.
* @param errorCorrection The level of error correction to apply.
* @return QImage representing the generated QR code.
*/
QImage QrCodeGenerator::generateQr(const QString &data, quint16 size, quint16 borderSize,
qrcodegen::QrCode::Ecc errorCorrection)
{
auto b = data.toUtf8();
const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
return qrCodeToImage(qrCode, borderSize, size);
}
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border) const {
if (border > INT_MAX / 2 || border * 2 > INT_MAX - qr.getSize())
throw std::overflow_error("Border too large");
std::ostringstream sb;
sb << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
sb << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" "
"\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
sb << "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" viewBox=\"0 0 ";
sb << (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2)
<< "\" stroke=\"none\">\n";
sb << "\t<rect width=\"100%\" height=\"100%\" fill=\"#FFFFFF\"/>\n";
sb << "\t<path d=\"";
for (int y = 0; y < qr.getSize(); y++) {
for (int x = 0; x < qr.getSize(); x++) {
if (qr.getModule(x, y)) {
if (x != 0 || y != 0)
sb << " ";
sb << "M" << (x + border) << "," << (y + border) << "h1v1h-1z";
}
}
}
sb << "\" fill=\"#000000\"/>\n";
sb << "</svg>\n";
return QString::fromStdString(sb.str());
/**
* @brief Generates an SVG string representing a QR code.
* @param data The data to encode in the QR code.
* @param borderSize The size of the border around the QR code.
* @param errorCorrection The level of error correction to apply.
* @return QString containing the SVG representation of the QR code.
*/
QString QrCodeGenerator::generateSvgQr(const QString &data, quint16 borderSize,
qrcodegen::QrCode::Ecc errorCorrection)
{
auto b = data.toUtf8();
const auto qrCode = qrcodegen::QrCode::encodeText(b.constData(), errorCorrection);
return toSvgString(qrCode, borderSize);
}
/**
* @brief Converts a QR code to its SVG representation as a string.
* @param qr The QR code to convert.
* @param border The border size to use.
* @return QString containing the SVG representation of the QR code.
*/
QString QrCodeGenerator::toSvgString(const qrcodegen::QrCode &qr, quint16 border) const
{
QString str;
QTextStream sb(&str);
sb << R"(<?xml version="1.0" encoding="UTF-8"?>)"
<< R"(<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">)"
<< R"(<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 )"
<< (qr.getSize() + border * 2) << " " << (qr.getSize() + border * 2)
<< R"(" stroke="none"><rect width="100%" height="100%" fill="#FFFFFF"/><path d=")";
for (int y = 0; y < qr.getSize(); y++)
{
for (int x = 0; x < qr.getSize(); x++)
{
if (qr.getModule(x, y))
{
sb << (x == 0 && y == 0 ? "" : " ") << "M" << (x + border) << "," << (y + border)
<< "h1v1h-1z";
}
}
}
sb << R"(" fill="#000000"/></svg>)";
return str;
}
/**
* @brief Converts a QR code to a QImage.
* @param qrCode The QR code to convert.
* @param border The border size to use.
* @param size The image size to generate.
* @return QImage representing the QR code.
*/
QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border,
const quint16 size) const {
auto svg = toSvgString(qrCode, border);
quint16 size) const
{
QString svg = toSvgString(qrCode, border);
QSvgRenderer render(svg.toUtf8());
QImage image(size, size, QImage::Format_Mono);
image.fill(Qt::white);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
render.render(&painter);
return image;
}
+19 -6
View File
@@ -30,10 +30,11 @@
/**
* @class QrCodeGenerator
* @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library to
* generate QR codes from QStrings in Qt applications.
* @brief The QrCodeGenerator class is a simple C++ class that uses the qrcodegen library
* to generate QR codes from QStrings in Qt applications.
*/
class QrCodeGenerator : public QObject {
class QrCodeGenerator : public QObject
{
public:
/**
* @brief Constructs a QrCodeGenerator object.
@@ -51,16 +52,28 @@ public:
*
* @return QImage containing the generated QR code.
*/
QImage generateQr(const QString &data, const quint16 size = 500, const quint16 borderSize = 1,
QImage generateQr(const QString &data, const quint16 size = 1000, const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM);
/**
* @brief Generates a QR code from the given data and error correction level.
* @param data The QString containing the data to encode in the QR code.
* @param borderSize The desired border width of the generated image (default: 1).
* @param errorCorrection The desired error correction level (default:
* qrcodegen::QrCode::Ecc::MEDIUM).
*
* @return QString string containing the generated QR code in SVG format.
*/
QString generateSvgQr(const QString &data, const quint16 borderSize = 1,
qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM);
private:
/**
* @brief Converts a qrcodegen::QrCode object to a SVG image.
* @param qrCode The qrcodegen::QrCode object to convert.
* @param borderSize The desired border width of the generated image (default: 1).
* @param border The desired border width of the generated image (default: 1).
*
* @return QImage containing the QR code.
* @return SVG containing the QR code.
*/
QString toSvgString(const qrcodegen::QrCode &qr, quint16 border) const;