2017-01-22 18:42:03 +02:00
|
|
|
#include "ImageHandler.h"
|
2012-05-14 17:01:57 +03:00
|
|
|
#include <QImage>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
2016-09-15 13:22:08 +03:00
|
|
|
#include <QThread>
|
|
|
|
#include <QTime>
|
2012-05-14 17:01:57 +03:00
|
|
|
|
2017-01-15 20:36:41 +02:00
|
|
|
#if QT_VERSION < 0x050000
|
|
|
|
#include <QGraphicsObject>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#endif // QT_VERSION < Qt 5.0
|
|
|
|
|
2017-03-20 17:23:52 +02:00
|
|
|
#if defined(QZXING_QML)
|
|
|
|
#include <QQuickItem>
|
|
|
|
#include <QQuickItemGrabResult>
|
|
|
|
#include <QQuickWindow>
|
|
|
|
#endif //QZXING_QML
|
|
|
|
|
2012-05-14 17:01:57 +03:00
|
|
|
ImageHandler::ImageHandler(QObject *parent) :
|
|
|
|
QObject(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-09-15 13:22:08 +03:00
|
|
|
QImage ImageHandler::extractQImage(QObject *imageObj, int offsetX, int offsetY, int width, int height)
|
2012-05-14 17:01:57 +03:00
|
|
|
{
|
2017-03-20 17:23:52 +02:00
|
|
|
QImage img;
|
|
|
|
#if defined(QZXING_QML)
|
2016-09-15 13:22:08 +03:00
|
|
|
#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();
|
2016-09-27 15:38:33 +03:00
|
|
|
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();
|
2016-09-15 13:22:08 +03:00
|
|
|
qApp->processEvents();
|
|
|
|
QThread::yieldCurrentThread();
|
|
|
|
}
|
2017-03-20 17:23:52 +02:00
|
|
|
img = result->image();
|
2016-09-15 13:22:08 +03:00
|
|
|
#else
|
2012-05-14 17:01:57 +03:00
|
|
|
QGraphicsObject *item = qobject_cast<QGraphicsObject*>(imageObj);
|
|
|
|
|
2015-06-11 10:36:08 +03:00
|
|
|
if (!item) {
|
2012-05-14 17:01:57 +03:00
|
|
|
qDebug() << "Item is NULL";
|
|
|
|
return QImage();
|
|
|
|
}
|
|
|
|
|
2017-03-20 17:23:52 +02:00
|
|
|
img = QImage(item->boundingRect().size().toSize(), QImage::Format_RGB32);
|
2012-05-14 17:01:57 +03:00
|
|
|
img.fill(QColor(255, 255, 255).rgb());
|
|
|
|
QPainter painter(&img);
|
|
|
|
QStyleOptionGraphicsItem styleOption;
|
|
|
|
item->paint(&painter, &styleOption);
|
2016-09-15 13:22:08 +03:00
|
|
|
#endif
|
2017-03-20 17:23:52 +02:00
|
|
|
#endif //defined(QZXING_QML)
|
2012-05-14 17:01:57 +03:00
|
|
|
|
2016-09-15 13:22:08 +03:00
|
|
|
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)
|
2016-09-27 15:38:33 +03:00
|
|
|
return img.copy(offsetX, offsetY, width, height);
|
2012-05-14 17:01:57 +03:00
|
|
|
else
|
2016-09-27 15:38:33 +03:00
|
|
|
return img;
|
2012-05-14 17:01:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImageHandler::save(QObject *imageObj, const QString &path,
|
2016-09-15 13:22:08 +03:00
|
|
|
const int offsetX, const int offsetY,
|
|
|
|
const int width, const int height)
|
2012-05-14 17:01:57 +03:00
|
|
|
{
|
|
|
|
QImage img = extractQImage(imageObj, offsetX, offsetY, width, height);
|
|
|
|
img.save(path);
|
|
|
|
}
|
2015-10-31 19:14:59 +02:00
|
|
|
|
2016-09-27 15:38:33 +03:00
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
void ImageHandler::imageGrabberReady()
|
|
|
|
{
|
|
|
|
pendingGrabbersLocker.lockForWrite();
|
|
|
|
pendingGrabbers.remove(sender());
|
|
|
|
pendingGrabbersLocker.unlock();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|