From f7b9e8307856751e4252b7f4595c6980dea65728 Mon Sep 17 00:00:00 2001 From: Denis Kormalev Date: Thu, 15 Sep 2016 13:22:08 +0300 Subject: [PATCH 1/2] Better Qt5 support --- src/QZXing.h | 8 ++++---- src/imagehandler.cpp | 49 +++++++++++++++++++++++++++++++++++--------- src/imagehandler.h | 8 ++++---- src/qzxing.cpp | 39 ++++++++++++++++++++++++----------- 4 files changed, 74 insertions(+), 30 deletions(-) diff --git a/src/QZXing.h b/src/QZXing.h index eaee633..da95a77 100644 --- a/src/QZXing.h +++ b/src/QZXing.h @@ -120,8 +120,8 @@ public slots: * of a portion of the image. (Suggested for Qt 4.x) */ QString decodeSubImageQML(QObject *item, - const double offsetX = 0 , const double offsetY = 0, - const double width = 0, const double height = 0); + const int offsetX = 0, const int offsetY = 0, + const int width = 0, const int height = 0); /** * The decoding function accessible from QML. (Suggested for Qt 5.x) @@ -138,8 +138,8 @@ public slots: * (Suggested for Qt 5.x) */ QString decodeSubImageQML(const QUrl &imageUrl, - const double offsetX = 0, const double offsetY = 0, - const double width = 0, const double height = 0); + const int offsetX = 0, const int offsetY = 0, + const int width = 0, const int height = 0); /** * The main encoding function. Currently supports only Qr code encoding diff --git a/src/imagehandler.cpp b/src/imagehandler.cpp index 74826b0..970b859 100644 --- a/src/imagehandler.cpp +++ b/src/imagehandler.cpp @@ -4,16 +4,37 @@ #include #include #include +#include +#include +#include +#include +#include ImageHandler::ImageHandler(QObject *parent) : QObject(parent) { } -QImage ImageHandler::extractQImage(QObject *imageObj, - const double offsetX, const double offsetY, - const double width, const double height) +QImage ImageHandler::extractQImage(QObject *imageObj, int offsetX, int offsetY, int width, int height) { +#if QT_VERSION >= 0x050000 + QQuickItem *item = qobject_cast(imageObj); + + if (!item || !item->window()->isVisible()) { + qDebug() << "Item is NULL"; + return QImage(); + } + + bool imgReady = false; + QTime timer; + timer.start(); + auto img = item->grabToImage(); + connect(img.data(), &QQuickItemGrabResult::ready, this, [&imgReady](){imgReady = true;}); + while (!imgReady && timer.elapsed() < 1000) { + qApp->processEvents(); + QThread::yieldCurrentThread(); + } +#else QGraphicsObject *item = qobject_cast(imageObj); if (!item) { @@ -26,18 +47,26 @@ QImage ImageHandler::extractQImage(QObject *imageObj, QPainter painter(&img); QStyleOptionGraphicsItem styleOption; item->paint(&painter, &styleOption); +#endif - if(offsetX == 0 && offsetY == 0 && width == 0 && height == 0) - return img; + if (offsetX < 0) + offsetX = 0; + if (offsetY < 0) + offsetY = 0; + if (width < 0) + width = 0; + if (height < 0) + height = 0; + + if (offsetX || offsetY || width || height) + return img->image().copy(offsetX, offsetY, width, height); else - { - return img.copy(offsetX, offsetY, width, height); - } + return img->image(); } void ImageHandler::save(QObject *imageObj, const QString &path, - const double offsetX, const double offsetY, - const double width, const double height) + const int offsetX, const int offsetY, + const int width, const int height) { QImage img = extractQImage(imageObj, offsetX, offsetY, width, height); img.save(path); diff --git a/src/imagehandler.h b/src/imagehandler.h index ce1022e..39aac70 100644 --- a/src/imagehandler.h +++ b/src/imagehandler.h @@ -11,13 +11,13 @@ public: explicit ImageHandler(QObject *parent = 0); QImage extractQImage(QObject *imageObj, - const double offsetX = 0 , const double offsetY = 0, - const double width = 0, const double height = 0); + int offsetX = 0, int offsetY = 0, + int width = 0, int height = 0); public slots: void save(QObject *item, const QString &path, - const double offsetX = 0, const double offsetY = 0, - const double width = 0, const double height = 0); + const int offsetX = 0, const int offsetY = 0, + const int width = 0, const int height = 0); }; #endif // IMAGEHANDLER_H diff --git a/src/qzxing.cpp b/src/qzxing.cpp index 98f23b1..f12b2b4 100644 --- a/src/qzxing.cpp +++ b/src/qzxing.cpp @@ -9,10 +9,15 @@ #include "imagehandler.h" #include #include +#include #include #include #include +#include +#include +#include + using namespace zxing; QZXing::QZXing(QObject *parent) : QObject(parent), tryHarder_(false) @@ -296,8 +301,8 @@ QString QZXing::decodeImageQML(QObject *item) } QString QZXing::decodeSubImageQML(QObject *item, - const double offsetX, const double offsetY, - const double width, const double height) + const int offsetX, const int offsetY, + const int width, const int height) { if(item == NULL) { @@ -317,21 +322,31 @@ QString QZXing::decodeImageQML(const QUrl &imageUrl) } QString QZXing::decodeSubImageQML(const QUrl &imageUrl, - const double offsetX, const double offsetY, - const double width, const double height) + const int offsetX, const int offsetY, + const int width, const int height) { QString imagePath = imageUrl.path(); imagePath = imagePath.trimmed(); - QFile file(imagePath); - if (!file.exists()) { - qDebug() << "[decodeSubImageQML()] The file" << file.fileName() << "does not exist."; - emit decodingFinished(false); - return ""; + QImage img; + if (imageUrl.scheme() == "image") { + if (imagePath.startsWith("/")) + imagePath = imagePath.right(imagePath.length() - 1); + QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine(); + QQuickImageProvider *imageProvider = static_cast(engine->imageProvider(imageUrl.host())); + QSize imgSize; + img = imageProvider->requestImage(imagePath, &imgSize, QSize()); + } else { + QFileInfo fileInfo(imagePath); + if (!fileInfo.exists()) { + qDebug() << "[decodeSubImageQML()] The file" << imagePath << "does not exist."; + emit decodingFinished(false); + return ""; + } + img = QImage(imagePath); } - QImage img(imageUrl.path()); - if(!(offsetX == 0 && offsetY == 0 && width == 0 && height == 0)) { + + if (offsetX || offsetY || width || height) img = img.copy(offsetX, offsetY, width, height); - } return decodeImage(img); } From 60c46080015b112c4b741c82e140059b5df5e36c Mon Sep 17 00:00:00 2001 From: Denis Kormalev Date: Tue, 27 Sep 2016 15:38:33 +0300 Subject: [PATCH 2/2] C++03 support --- src/QZXing.h | 2 +- src/imagehandler.cpp | 31 +++++++++++++++++++++++++------ src/imagehandler.h | 10 ++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/QZXing.h b/src/QZXing.h index da95a77..d9a4b10 100644 --- a/src/QZXing.h +++ b/src/QZXing.h @@ -13,7 +13,7 @@ #include #endif -#include +#include "QZXingImageProvider.h" // forward declaration namespace zxing { diff --git a/src/imagehandler.cpp b/src/imagehandler.cpp index 970b859..c3d8c49 100644 --- a/src/imagehandler.cpp +++ b/src/imagehandler.cpp @@ -25,15 +25,25 @@ QImage ImageHandler::extractQImage(QObject *imageObj, int offsetX, int offsetY, return QImage(); } - bool imgReady = false; QTime timer; timer.start(); - auto img = item->grabToImage(); - connect(img.data(), &QQuickItemGrabResult::ready, this, [&imgReady](){imgReady = true;}); - while (!imgReady && timer.elapsed() < 1000) { + QSharedPointer result = item->grabToImage(); + pendingGrabbersLocker.lockForWrite(); + pendingGrabbers << result.data(); + pendingGrabbersLocker.unlock(); + + connect(result.data(), &QQuickItemGrabResult::ready, this, &ImageHandler::imageGrabberReady); + while (timer.elapsed() < 1000) { + pendingGrabbersLocker.lockForRead(); + if (!pendingGrabbers.contains(result.data())) { + pendingGrabbersLocker.unlock(); + break; + } + pendingGrabbersLocker.unlock(); qApp->processEvents(); QThread::yieldCurrentThread(); } + QImage img = result->image(); #else QGraphicsObject *item = qobject_cast(imageObj); @@ -59,9 +69,9 @@ QImage ImageHandler::extractQImage(QObject *imageObj, int offsetX, int offsetY, height = 0; if (offsetX || offsetY || width || height) - return img->image().copy(offsetX, offsetY, width, height); + return img.copy(offsetX, offsetY, width, height); else - return img->image(); + return img; } void ImageHandler::save(QObject *imageObj, const QString &path, @@ -72,3 +82,12 @@ void ImageHandler::save(QObject *imageObj, const QString &path, img.save(path); } +#if QT_VERSION >= 0x050000 + void ImageHandler::imageGrabberReady() + { + pendingGrabbersLocker.lockForWrite(); + pendingGrabbers.remove(sender()); + pendingGrabbersLocker.unlock(); + } +#endif + diff --git a/src/imagehandler.h b/src/imagehandler.h index 39aac70..6624eda 100644 --- a/src/imagehandler.h +++ b/src/imagehandler.h @@ -3,6 +3,10 @@ #include #include +#if QT_VERSION >= 0x050000 +#include +#include +#endif class ImageHandler : public QObject { @@ -18,6 +22,12 @@ public slots: void save(QObject *item, const QString &path, const int offsetX = 0, const int offsetY = 0, const int width = 0, const int height = 0); +private: +#if QT_VERSION >= 0x050000 + void imageGrabberReady(); + QSet pendingGrabbers; + QReadWriteLock pendingGrabbersLocker; +#endif }; #endif // IMAGEHANDLER_H