From 12ae5334b4025a526405d408e9c6ae75c46b28e4 Mon Sep 17 00:00:00 2001 From: UpdogCoder Date: Sun, 18 Feb 2024 12:34:14 +0000 Subject: [PATCH] Created example for displaying QR code in QML --- example/CMakeLists.txt | 5 ++++ example/ZXingQtQmlWriter.cpp | 32 ++++++++++++++++++++ example/ZXingQtQmlWriter.h | 34 ++++++++++++++++++++++ example/ZXingQtQmlWriter.qml | 50 ++++++++++++++++++++++++++++++++ example/ZXingQtQmlWriter.qrc | 5 ++++ example/ZXingQtQmlWriterMain.cpp | 31 ++++++++++++++++++++ 6 files changed, 157 insertions(+) mode change 100644 => 100755 example/CMakeLists.txt create mode 100755 example/ZXingQtQmlWriter.cpp create mode 100755 example/ZXingQtQmlWriter.h create mode 100755 example/ZXingQtQmlWriter.qml create mode 100755 example/ZXingQtQmlWriter.qrc create mode 100755 example/ZXingQtQmlWriterMain.cpp diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt old mode 100644 new mode 100755 index 9a492ca5eb..399016347f --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -56,4 +56,9 @@ if (BUILD_WRITERS) add_executable (ZXingQtWriter ZXingQtWriter.cpp) target_link_libraries(ZXingQtWriter ZXing::ZXing Qt::Gui) endif() + + if (TARGET Qt::Multimedia AND TARGET Qt::Quick) + add_executable(ZXingQtQmlWriter ZXingQtQmlWriterMain.cpp ZXingQtQmlWriter.qrc ZXingQtQmlWriter.h ZXingQtQmlWriter.cpp) + target_link_libraries(ZXingQtQmlWriter ZXing::ZXing Qt::Gui Qt::Multimedia Qt::Quick) + endif() endif() diff --git a/example/ZXingQtQmlWriter.cpp b/example/ZXingQtQmlWriter.cpp new file mode 100755 index 0000000000..32640c7ca7 --- /dev/null +++ b/example/ZXingQtQmlWriter.cpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: Apache-2.0 +#include "ZXingQtQmlWriter.h" +#include "BarcodeFormat.h" +#include "BitMatrix.h" +#include "MultiFormatWriter.h" +#include +#include + +ZXingQtQmlWriter::ZXingQtQmlWriter(QQuickPaintedItem* parent) : QQuickPaintedItem{parent} +{} + + +void ZXingQtQmlWriter::generateBarcode(const QString &text) +{ + auto writer = ZXing::MultiFormatWriter(ZXing::BarcodeFormat::QRCode); + auto matrix = writer.encode(text.toStdString(), 0, 0); + auto bitmap = ZXing::ToMatrix(matrix); + + m_qrCode = QImage(bitmap.data(), bitmap.width(), bitmap.height(), bitmap.width(), QImage::Format::Format_Grayscale8).copy(); + this->update(); +} + +void ZXingQtQmlWriter::paint(QPainter *painter) +{ + //Ensuring that QR code is drawn as a rectangle + qreal minSize = std::min(boundingRect().width(), boundingRect().height()); + QRectF scaledRect{0,0,minSize,minSize}; + // Center the scaled image within the container + scaledRect.moveCenter(boundingRect().center()); + + painter->drawImage(scaledRect, m_qrCode); +} diff --git a/example/ZXingQtQmlWriter.h b/example/ZXingQtQmlWriter.h new file mode 100755 index 0000000000..62fb43f2ce --- /dev/null +++ b/example/ZXingQtQmlWriter.h @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: Apache-2.0 +#ifndef ZXINGQTQMLWRITER_H +#define ZXINGQTQMLWRITER_H + +#include +#include +#include +#include + +//Example of class which can draw QR code in QML based on +//QImage generated by ZXing +class ZXingQtQmlWriter : public QQuickPaintedItem +{ + Q_OBJECT + QML_ELEMENT + +public: + explicit ZXingQtQmlWriter(QQuickPaintedItem* parent = nullptr); + + //Method based on code from ZXingQtWriter.cpp created by Axel Waggershauser. + //Method uses ZXing::BarcodeFormat::QRCode format - different formats can be + //supported by passing the format as argument or changing format inside method + //implementation + Q_INVOKABLE void generateBarcode(const QString &text); + + // QQuickPaintedItem interface + void paint(QPainter *painter); + +private: + QImage m_qrCode; +}; + + +#endif // ZXINGQTQMLWRITER_H diff --git a/example/ZXingQtQmlWriter.qml b/example/ZXingQtQmlWriter.qml new file mode 100755 index 0000000000..f34d7afb5a --- /dev/null +++ b/example/ZXingQtQmlWriter.qml @@ -0,0 +1,50 @@ +import QtQuick 2.15 +import QtQuick.Controls 2.15 +import QtQuick.Layouts 2.15 +import ZXing 1.0 + +ApplicationWindow { + id: app + visible: true + width: 480 + height: 640 + title: Qt.application.name + + background: Rectangle { + color: "#2e2f30" + } + + ColumnLayout { + id: appLayout + anchors.fill: parent + //Creating instance of ZXingQtQmlWriter that displays QR code + ZXingQtQmlWriter { + id: qrCode + + Layout.fillHeight: true + Layout.fillWidth: true + Layout.margins: 25 + Layout.alignment: Qt.AlignTop | Qt.AlignHCenter + } + + RowLayout { + Layout.margins: 50 + Layout.alignment: Qt.AlignTop + + TextField { + id: codeContentsInput + Layout.fillWidth: true + Layout.alignment: Qt.AlignHCenter + + placeholderText: "Enter contents of QR code" + onAccepted: qrCode.generateBarcode(codeContentsInput.text) + } + + Button { + Layout.alignment: Qt.AlignHCenter + text: "Generate" + onClicked: qrCode.generateBarcode(codeContentsInput.text) + } + } + } +} diff --git a/example/ZXingQtQmlWriter.qrc b/example/ZXingQtQmlWriter.qrc new file mode 100755 index 0000000000..757e36895b --- /dev/null +++ b/example/ZXingQtQmlWriter.qrc @@ -0,0 +1,5 @@ + + + ZXingQtQmlWriter.qml + + diff --git a/example/ZXingQtQmlWriterMain.cpp b/example/ZXingQtQmlWriterMain.cpp new file mode 100755 index 0000000000..3c8fbcfb82 --- /dev/null +++ b/example/ZXingQtQmlWriterMain.cpp @@ -0,0 +1,31 @@ +/* + * Copyright 2020 Axel Waggershauser + */ +// SPDX-License-Identifier: Apache-2.0 + +#include +#include + +#include "ZXingQtQmlWriter.h" + +int main(int argc, char *argv[]) +{ +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); +#endif + QGuiApplication app(argc, argv); + app.setApplicationName("ZXingQtQmlWriter"); + + ZXingQtQmlWriter writer; + //Registering ZXingQtQmlWriter instance so it is created on QML level. + //Can be done directly inside CMake using qt_add_qml_module macro + qmlRegisterType("ZXing",1,0, "ZXingQtQmlWriter"); + + QQmlApplicationEngine engine; + engine.load(QUrl(QStringLiteral("qrc:/ZXingQtQmlWriter.qml"))); + if (engine.rootObjects().isEmpty()) { + return -1; + } + + return app.exec(); +}