/* * 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 "QrCodeGenerator.h" #include #include #include #include QrCodeGenerator::QrCodeGenerator(QObject *parent) : QObject(parent) {} 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); 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 << "\n"; sb << "\n"; sb << "\n"; sb << "\t\n"; sb << "\t\n"; sb << "\n"; return QString::fromStdString(sb.str()); } QImage QrCodeGenerator::qrCodeToImage(const qrcodegen::QrCode &qrCode, quint16 border, const quint16 size) const { auto svg = toSvgString(qrCode, border); QSvgRenderer render(svg.toUtf8()); QImage image(size, size, QImage::Format_Mono); image.fill(Qt::white); QPainter painter(&image); render.render(&painter); return image; }