|
| 1 | +// Copyright (c) 2025 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/qrimageprovider.h> |
| 6 | + |
| 7 | +#if defined(HAVE_CONFIG_H) |
| 8 | +#include <config/bitcoin-config.h> /* for USE_QRCODE */ |
| 9 | +#endif |
| 10 | + |
| 11 | +#ifdef USE_QRCODE |
| 12 | +#include <qrencode.h> |
| 13 | +#endif |
| 14 | + |
| 15 | +#include <QImage> |
| 16 | +#include <QQuickImageProvider> |
| 17 | +#include <QSize> |
| 18 | +#include <QString> |
| 19 | +#include <QUrl> |
| 20 | +#include <QUrlQuery> |
| 21 | + |
| 22 | +QRImageProvider::QRImageProvider() |
| 23 | + : QQuickImageProvider{QQuickImageProvider::Image} |
| 24 | +{ |
| 25 | +} |
| 26 | + |
| 27 | +QImage QRImageProvider::requestImage(const QString& id, QSize* size, const QSize& requested_size) |
| 28 | +{ |
| 29 | +#ifdef USE_QRCODE |
| 30 | + const QUrl url{"image:///" + id}; |
| 31 | + const QUrlQuery query{url}; |
| 32 | + const QString data{url.path().mid(1)}; |
| 33 | + const QColor fg{query.queryItemValue("fg")}; |
| 34 | + const QColor bg{query.queryItemValue("bg")}; |
| 35 | + |
| 36 | + QRcode* code = QRcode_encodeString(data.toUtf8().constData(), 0, QR_ECLEVEL_L, QR_MODE_8, 1); |
| 37 | + |
| 38 | + if (code) { |
| 39 | + QImage image{code->width, code->width, QImage::Format_ARGB32}; |
| 40 | + unsigned char* p = code->data; |
| 41 | + for (int y = 0; y < code->width; ++y) { |
| 42 | + for (int x = 0; x < code->width; ++x) { |
| 43 | + image.setPixelColor(x, y, (*p & 1) ? fg : bg); |
| 44 | + ++p; |
| 45 | + } |
| 46 | + } |
| 47 | + *size = QSize(code->width, code->width); |
| 48 | + QRcode_free(code); |
| 49 | + return image; |
| 50 | + } |
| 51 | +#endif // USE_QRCODE |
| 52 | + QImage pixel{1, 1, QImage::Format_ARGB32}; |
| 53 | + pixel.setPixelColor(0, 0, QColorConstants::Transparent); |
| 54 | + *size = QSize(1, 1); |
| 55 | + return pixel; |
| 56 | +} |
0 commit comments