From 58633d8cd0e2c348ba68431fd4be7d354e28e2a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berk=20Demirk=C4=B1r?= Date: Thu, 5 Jun 2025 18:38:45 +0300 Subject: [PATCH] Allow QR code generation from binary data --- src/QrCodeGenerator.cpp | 18 ++++++++++++++++++ src/QrCodeGenerator.h | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/QrCodeGenerator.cpp b/src/QrCodeGenerator.cpp index b6ffb8a..66a466c 100644 --- a/src/QrCodeGenerator.cpp +++ b/src/QrCodeGenerator.cpp @@ -56,6 +56,24 @@ QImage QrCodeGenerator::generateQr(const QString &data, quint16 size, return qrCodeToImage(qrCode, borderSize, size); } +/** + * @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 QByteArray &data, quint16 size, + quint16 borderSize, + qrcodegen::QrCode::Ecc errorCorrection) +{ + std::vector c(data.constData(), data.constData() + data.size()); + const auto qrCode + = qrcodegen::QrCode::encodeBinary(c, 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. diff --git a/src/QrCodeGenerator.h b/src/QrCodeGenerator.h index bd4d840..9d67956 100644 --- a/src/QrCodeGenerator.h +++ b/src/QrCodeGenerator.h @@ -58,6 +58,22 @@ public: qrcodegen::QrCode::Ecc errorCorrection = qrcodegen::QrCode::Ecc::MEDIUM); + /** + * @brief Generates a QR code from the given data and error correction level. + * @param data The QByteArray containing the data to encode in the QR code. + * @param size The desired width/height of the generated image (default: 500). + * @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 QImage containing the generated QR code. + */ + QImage generateQr(const QByteArray &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.