Make QR scanning work in both Mac and Linux

This commit is contained in:
Emil Lundberg 2018-01-22 12:26:17 +01:00
parent 10f255e409
commit 91495df002
No known key found for this signature in database
GPG Key ID: 5B9688125FF0B636

View File

@ -3,6 +3,7 @@
#include <QObject> #include <QObject>
#include <QtWidgets> #include <QtWidgets>
#include <QVariant> #include <QVariant>
class ScreenShot: public QObject class ScreenShot: public QObject
{ {
Q_OBJECT Q_OBJECT
@ -16,16 +17,18 @@ public:
// Dimensions of output image // Dimensions of output image
int outputHeight = 0; int outputHeight = 0;
int outputWidth = 0; int outputWidth = 0;
for (QScreen *screen : QGuiApplication::screens()) {
outputWidth = std::max(outputWidth, screen->geometry().width()); const QList<QScreen*> screens = QGuiApplication::screens();
QImage screenshots[screens.length()];
std::transform(screens.begin(), screens.end(), screenshots, &ScreenShot::takeScreenshot);
for (QImage image : screenshots) {
outputWidth = std::max(outputWidth, image.width());
} }
for (QScreen *screen : QGuiApplication::screens()) { for (QImage image : screenshots) {
QRect g = screen->geometry();
QPixmap screenShot = screen->grabWindow(0, g.x(), g.y(), g.width(), g.height());
// Monochrome, no dither // Monochrome, no dither
QImage image = screenShot.toImage();
image = image.convertToFormat(QImage::Format_Mono, Qt::ThresholdDither); image = image.convertToFormat(QImage::Format_Mono, Qt::ThresholdDither);
// Stack screens vertically in output image // Stack screens vertically in output image
@ -53,6 +56,21 @@ public:
map.insert("data", QString(imageArray.toBase64())); map.insert("data", QString(imageArray.toBase64()));
return map; return map;
} }
private:
static QImage takeScreenshot(QScreen *screen) {
QRect g = screen->geometry();
return screen->grabWindow(
0,
#ifdef Q_OS_MACOS
g.x(), g.y(),
#else
0, 0,
#endif
g.width(), g.height()
).toImage();
}
}; };
#endif // SCREENSHOT_H #endif // SCREENSHOT_H