2017-02-13 15:03:58 +03:00
|
|
|
#ifndef SCREENSHOT_H
|
|
|
|
#define SCREENSHOT_H
|
|
|
|
#include <QObject>
|
2017-02-13 15:36:48 +03:00
|
|
|
#include <QtWidgets>
|
2017-02-14 15:12:24 +03:00
|
|
|
#include <QVariant>
|
2018-01-22 14:26:17 +03:00
|
|
|
|
2017-02-13 15:03:58 +03:00
|
|
|
class ScreenShot: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit ScreenShot () : QObject() {}
|
2017-02-14 15:12:24 +03:00
|
|
|
// Take a screenshot, convert it to a bitarray and return it with some metadata
|
2017-02-27 17:23:34 +03:00
|
|
|
Q_INVOKABLE QVariantMap capture() {
|
2018-01-16 16:35:16 +03:00
|
|
|
// Get all pixels as 1 (black) or 0 (white)
|
|
|
|
QByteArray imageArray;
|
2018-01-16 17:41:47 +03:00
|
|
|
|
|
|
|
// Dimensions of output image
|
|
|
|
int outputHeight = 0;
|
|
|
|
int outputWidth = 0;
|
|
|
|
|
2018-01-22 14:26:17 +03:00
|
|
|
const QList<QScreen*> screens = QGuiApplication::screens();
|
2018-01-22 16:08:08 +03:00
|
|
|
std::vector<QImage> screenshots(screens.length());
|
|
|
|
std::transform(screens.begin(), screens.end(), screenshots.begin(), &ScreenShot::takeScreenshot);
|
2018-01-22 14:26:17 +03:00
|
|
|
|
|
|
|
for (QImage image : screenshots) {
|
|
|
|
outputWidth = std::max(outputWidth, image.width());
|
|
|
|
}
|
2018-01-16 17:41:47 +03:00
|
|
|
|
2018-01-22 14:26:17 +03:00
|
|
|
for (QImage image : screenshots) {
|
2018-01-16 17:41:47 +03:00
|
|
|
// Monochrome, no dither
|
|
|
|
image = image.convertToFormat(QImage::Format_Mono, Qt::ThresholdDither);
|
|
|
|
|
|
|
|
// Stack screens vertically in output image
|
|
|
|
outputHeight += image.height();
|
|
|
|
for (int row = 0; row < image.height(); ++row) {
|
|
|
|
for (int col = 0; col < image.width(); ++col) {
|
|
|
|
QRgb px = image.pixel(col, row);
|
|
|
|
if (px == 0xFF000000) {
|
|
|
|
imageArray.append((char) 1);
|
|
|
|
} else {
|
|
|
|
imageArray.append((char) 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pad smaller screens horizontally
|
|
|
|
for (int col = image.width(); col < outputWidth; ++col) {
|
2018-01-16 16:35:16 +03:00
|
|
|
imageArray.append((char) 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 17:41:47 +03:00
|
|
|
|
2018-01-16 16:35:16 +03:00
|
|
|
QVariantMap map;
|
2018-01-16 17:41:47 +03:00
|
|
|
map.insert("width", outputWidth);
|
|
|
|
map.insert("height", outputHeight);
|
2018-01-16 16:35:16 +03:00
|
|
|
map.insert("data", QString(imageArray.toBase64()));
|
|
|
|
return map;
|
2017-02-13 15:03:58 +03:00
|
|
|
}
|
2018-01-22 14:26:17 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-02-13 15:03:58 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SCREENSHOT_H
|