Merge branch 'better-qt5-support' of https://github.com/dkormalev/qzxing into dkormalev-better-qt5-support

This commit is contained in:
favoritas37 2016-10-02 13:13:59 +03:00
commit 725e67f66c
4 changed files with 104 additions and 31 deletions

View File

@ -13,7 +13,7 @@
#include <QQmlEngine> #include <QQmlEngine>
#endif #endif
#include <qzxingimageprovider.h> #include "QZXingImageProvider.h"
// forward declaration // forward declaration
namespace zxing { namespace zxing {
@ -120,8 +120,8 @@ public slots:
* of a portion of the image. (Suggested for Qt 4.x) * of a portion of the image. (Suggested for Qt 4.x)
*/ */
QString decodeSubImageQML(QObject *item, QString decodeSubImageQML(QObject *item,
const double offsetX = 0 , const double offsetY = 0, const int offsetX = 0, const int offsetY = 0,
const double width = 0, const double height = 0); const int width = 0, const int height = 0);
/** /**
* The decoding function accessible from QML. (Suggested for Qt 5.x) * The decoding function accessible from QML. (Suggested for Qt 5.x)
@ -138,8 +138,8 @@ public slots:
* (Suggested for Qt 5.x) * (Suggested for Qt 5.x)
*/ */
QString decodeSubImageQML(const QUrl &imageUrl, QString decodeSubImageQML(const QUrl &imageUrl,
const double offsetX = 0, const double offsetY = 0, const int offsetX = 0, const int offsetY = 0,
const double width = 0, const double height = 0); const int width = 0, const int height = 0);
/** /**
* The main encoding function. Currently supports only Qr code encoding * The main encoding function. Currently supports only Qr code encoding

View File

@ -4,16 +4,47 @@
#include <QPainter> #include <QPainter>
#include <QStyleOptionGraphicsItem> #include <QStyleOptionGraphicsItem>
#include <QDebug> #include <QDebug>
#include <QQuickItem>
#include <QQuickItemGrabResult>
#include <QQuickWindow>
#include <QThread>
#include <QTime>
ImageHandler::ImageHandler(QObject *parent) : ImageHandler::ImageHandler(QObject *parent) :
QObject(parent) QObject(parent)
{ {
} }
QImage ImageHandler::extractQImage(QObject *imageObj, QImage ImageHandler::extractQImage(QObject *imageObj, int offsetX, int offsetY, int width, int height)
const double offsetX, const double offsetY,
const double width, const double height)
{ {
#if QT_VERSION >= 0x050000
QQuickItem *item = qobject_cast<QQuickItem *>(imageObj);
if (!item || !item->window()->isVisible()) {
qDebug() << "Item is NULL";
return QImage();
}
QTime timer;
timer.start();
QSharedPointer<QQuickItemGrabResult> 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<QGraphicsObject*>(imageObj); QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
if (!item) { if (!item) {
@ -26,20 +57,37 @@ QImage ImageHandler::extractQImage(QObject *imageObj,
QPainter painter(&img); QPainter painter(&img);
QStyleOptionGraphicsItem styleOption; QStyleOptionGraphicsItem styleOption;
item->paint(&painter, &styleOption); item->paint(&painter, &styleOption);
#endif
if(offsetX == 0 && offsetY == 0 && width == 0 && height == 0) if (offsetX < 0)
return img; offsetX = 0;
else if (offsetY < 0)
{ offsetY = 0;
if (width < 0)
width = 0;
if (height < 0)
height = 0;
if (offsetX || offsetY || width || height)
return img.copy(offsetX, offsetY, width, height); return img.copy(offsetX, offsetY, width, height);
} else
return img;
} }
void ImageHandler::save(QObject *imageObj, const QString &path, void ImageHandler::save(QObject *imageObj, const QString &path,
const double offsetX, const double offsetY, const int offsetX, const int offsetY,
const double width, const double height) const int width, const int height)
{ {
QImage img = extractQImage(imageObj, offsetX, offsetY, width, height); QImage img = extractQImage(imageObj, offsetX, offsetY, width, height);
img.save(path); img.save(path);
} }
#if QT_VERSION >= 0x050000
void ImageHandler::imageGrabberReady()
{
pendingGrabbersLocker.lockForWrite();
pendingGrabbers.remove(sender());
pendingGrabbersLocker.unlock();
}
#endif

View File

@ -3,6 +3,10 @@
#include <QObject> #include <QObject>
#include <QImage> #include <QImage>
#if QT_VERSION >= 0x050000
#include <QSet>
#include <QReadWriteLock>
#endif
class ImageHandler : public QObject class ImageHandler : public QObject
{ {
@ -11,13 +15,19 @@ public:
explicit ImageHandler(QObject *parent = 0); explicit ImageHandler(QObject *parent = 0);
QImage extractQImage(QObject *imageObj, QImage extractQImage(QObject *imageObj,
const double offsetX = 0 , const double offsetY = 0, int offsetX = 0, int offsetY = 0,
const double width = 0, const double height = 0); int width = 0, int height = 0);
public slots: public slots:
void save(QObject *item, const QString &path, void save(QObject *item, const QString &path,
const double offsetX = 0, const double offsetY = 0, const int offsetX = 0, const int offsetY = 0,
const double width = 0, const double height = 0); const int width = 0, const int height = 0);
private:
#if QT_VERSION >= 0x050000
void imageGrabberReady();
QSet<QObject *> pendingGrabbers;
QReadWriteLock pendingGrabbersLocker;
#endif
}; };
#endif // IMAGEHANDLER_H #endif // IMAGEHANDLER_H

View File

@ -9,11 +9,16 @@
#include "imagehandler.h" #include "imagehandler.h"
#include <QTime> #include <QTime>
#include <QUrl> #include <QUrl>
#include <QFileInfo>
#include <zxing/qrcode/encoder/Encoder.h> #include <zxing/qrcode/encoder/Encoder.h>
#include <zxing/qrcode/ErrorCorrectionLevel.h> #include <zxing/qrcode/ErrorCorrectionLevel.h>
#include <zxing/common/detector/WhiteRectangleDetector.h> #include <zxing/common/detector/WhiteRectangleDetector.h>
#include <QColor> #include <QColor>
#include <QQmlEngine>
#include <QQmlContext>
#include <QQuickImageProvider>
using namespace zxing; using namespace zxing;
QZXing::QZXing(QObject *parent) : QObject(parent), tryHarder_(false) QZXing::QZXing(QObject *parent) : QObject(parent), tryHarder_(false)
@ -373,8 +378,8 @@ QString QZXing::decodeImageQML(QObject *item)
} }
QString QZXing::decodeSubImageQML(QObject *item, QString QZXing::decodeSubImageQML(QObject *item,
const double offsetX, const double offsetY, const int offsetX, const int offsetY,
const double width, const double height) const int width, const int height)
{ {
if(item == NULL) if(item == NULL)
{ {
@ -394,21 +399,31 @@ QString QZXing::decodeImageQML(const QUrl &imageUrl)
} }
QString QZXing::decodeSubImageQML(const QUrl &imageUrl, QString QZXing::decodeSubImageQML(const QUrl &imageUrl,
const double offsetX, const double offsetY, const int offsetX, const int offsetY,
const double width, const double height) const int width, const int height)
{ {
QString imagePath = imageUrl.path(); QString imagePath = imageUrl.path();
imagePath = imagePath.trimmed(); imagePath = imagePath.trimmed();
QFile file(imagePath); QImage img;
if (!file.exists()) { if (imageUrl.scheme() == "image") {
qDebug() << "[decodeSubImageQML()] The file" << file.fileName() << "does not exist."; if (imagePath.startsWith("/"))
imagePath = imagePath.right(imagePath.length() - 1);
QQmlEngine *engine = QQmlEngine::contextForObject(this)->engine();
QQuickImageProvider *imageProvider = static_cast<QQuickImageProvider *>(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); emit decodingFinished(false);
return ""; return "";
} }
QImage img(imageUrl.path()); img = QImage(imagePath);
if(!(offsetX == 0 && offsetY == 0 && width == 0 && height == 0)) {
img = img.copy(offsetX, offsetY, width, height);
} }
if (offsetX || offsetY || width || height)
img = img.copy(offsetX, offsetY, width, height);
return decodeImage(img); return decodeImage(img);
} }