/* (c) Copyright 2012 - 2026 Anton Sviridenko https://picapica.im This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #include "qrcode.h" #include "../PICA_id.h" #include #include #include // Which of ZXing-C++'s two writer APIs this libZXing has is found out by // configure, see the comments there. 3.x builds the new one by default, 2.x // the old. #ifdef HAVE_ZXING_CREATE_BARCODE #include #else #include #include #endif #include #include QImage picaQrCodeImage(const QString &text, int quietZone) { const std::string utf8 = text.toStdString(); if (quietZone < 0) quietZone = 0; // ZXing reports what it cannot encode by throwing, in both APIs - text too // long for the largest QR code being the one way that could happen here. try { #ifdef HAVE_ZXING_CREATE_BARCODE const ZXing::Barcode barcode = ZXing::CreateBarcodeFromText(utf8, ZXing::CreatorOptions(ZXing::BarcodeFormat::QRCode)); // One byte per module, 0 for a dark one and 0xff for a light one. const ZXing::ImageView symbol = barcode.symbol(); const int width = symbol.width(); const int height = symbol.height(); auto dark = [&symbol](int x, int y) { return *symbol.data(x, y) < 0x80; }; #else const ZXing::BitMatrix matrix = ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode).setMargin(0).encode(utf8, 0, 0); const int width = matrix.width(); const int height = matrix.height(); auto dark = [&matrix](int x, int y) { return matrix.get(x, y); }; #endif if (width <= 0 || height <= 0) return QImage(); QImage image(width + 2 * quietZone, height + 2 * quietZone, QImage::Format_RGB32); if (image.isNull()) return QImage(); image.fill(Qt::white); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) if (dark(x, y)) image.setPixel(x + quietZone, y + quietZone, qRgb(0, 0, 0)); return image; } catch (const std::exception &e) { qWarning() << "Could not encode a QR code:" << e.what(); return QImage(); } } QByteArray picaIdFromQrText(const QString &text) { const QByteArray id = QByteArray::fromBase64(text.trimmed().toLatin1()).left(PICA_ID_SIZE); return id.size() == PICA_ID_SIZE ? id : QByteArray(); } QrCodeReader::QrCodeReader(QObject *parent) : QObject(parent) { } void QrCodeReader::read(QImage frame) { if (frame.format() != QImage::Format_RGB32 && frame.format() != QImage::Format_ARGB32) frame = frame.convertToFormat(QImage::Format_RGB32); if (frame.isNull()) { emit frameRead(QString()); return; } // Format_RGB32 is a 32 bit 0xffRRGGBB per pixel in the machine's own byte // order, so what ZXing sees in memory depends on it. #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN const ZXing::ImageFormat format = ZXing::ImageFormat::BGRX; #else const ZXing::ImageFormat format = ZXing::ImageFormat::XRGB; #endif const ZXing::ImageView image(frame.constBits(), frame.width(), frame.height(), format, (int)frame.bytesPerLine()); // QR codes only, which spares trying every other symbology on every frame. // tryHarder and tryRotate are what make a code held at an angle in front of // the camera, or a little out of focus, readable at all; tryInvert also // takes light-on-dark codes, as a phone in dark mode may draw them. ZXing::ReaderOptions options; options.setFormats(ZXing::BarcodeFormat::QRCode); options.setTryHarder(true); options.setTryRotate(true); options.setTryInvert(true); QString text; try { const auto result = ZXing::ReadBarcode(image, options); if (result.isValid()) text = QString::fromStdString(result.text()); } catch (const std::exception &e) { qWarning() << "Could not read a QR code:" << e.what(); } emit frameRead(text); }