/* * Copyright (c) 2023 Alex Spataru * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include #include "QrCodeGenerator.h" /** * @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); } /** * @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"()" << R"()" << R"()"; 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, 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; }