From 5175ffd8a540b31f4978557f3ab5d9b077ae5f77 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 8 Jul 2021 22:21:18 +0200 Subject: [PATCH] QrCodeUtils --- src/qrcode_scanner/QrCodeUtils.cpp | 53 ++++++++++++++++++++++++++++++ src/qrcode_scanner/QrCodeUtils.h | 17 ++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/qrcode_scanner/QrCodeUtils.cpp create mode 100644 src/qrcode_scanner/QrCodeUtils.h diff --git a/src/qrcode_scanner/QrCodeUtils.cpp b/src/qrcode_scanner/QrCodeUtils.cpp new file mode 100644 index 00000000..fd7b3dc0 --- /dev/null +++ b/src/qrcode_scanner/QrCodeUtils.cpp @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#include "QrCodeUtils.h" +#include + +bool QrCodeUtils::zimageFromQImage(const QImage &qImg, zbar::Image &dst) { + qDebug() << qImg.format(); + switch (qImg.format()) { + case QImage::Format_RGB32 : + case QImage::Format_ARGB32 : + case QImage::Format_ARGB32_Premultiplied : + break; + default : + return false; + } + + unsigned int bpl(qImg.bytesPerLine()); + unsigned int width(bpl / 4); + unsigned int height(qImg.height()); + + dst.set_size(width, height); + dst.set_format("BGR4"); + unsigned long datalen = qImg.sizeInBytes(); + dst.set_data(qImg.bits(), datalen); + if ((width * 4 != bpl) || (width * height * 4 > datalen)) { + return false; + } + return true; +} + +QString QrCodeUtils::scanImage(const QImage &img) { + zbar::ImageScanner scanner; + scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1); + + zbar::Image zImg; + int r = zimageFromQImage(img, zImg); + if (!r) { + qWarning() << "Unable to convert QImage into zbar::Image"; + return ""; + } + + zbar::Image scanImg = zImg.convert(*(long*)"Y800"); + scanner.scan(scanImg); + + QString result; + for (zbar::Image::SymbolIterator sym = scanImg.symbol_begin(); sym != scanImg.symbol_end(); ++sym) { + if (!sym->get_count()) { + result = QString::fromStdString(sym->get_data()); + } + } + return result; +} \ No newline at end of file diff --git a/src/qrcode_scanner/QrCodeUtils.h b/src/qrcode_scanner/QrCodeUtils.h new file mode 100644 index 00000000..10cf7fe7 --- /dev/null +++ b/src/qrcode_scanner/QrCodeUtils.h @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: BSD-3-Clause +// Copyright (c) 2020-2021, The Monero Project. + +#ifndef FEATHER_QRCODEUTILS_H +#define FEATHER_QRCODEUTILS_H + +#include +#include + +class QrCodeUtils { +public: + static bool zimageFromQImage(const QImage &qImg, zbar::Image &dst); + static QString scanImage(const QImage &img); +}; + + +#endif //FEATHER_QRCODEUTILS_H -- 2.52.0