-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaptureworker.cpp
More file actions
88 lines (68 loc) · 1.95 KB
/
captureworker.cpp
File metadata and controls
88 lines (68 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "captureworker.h"
#include <QtWidgets>
#include <QCryptographicHash>
namespace
{
QString calcHash(const QByteArray& array)
{
QCryptographicHash hf(QCryptographicHash::Md4);
hf.addData(array);
QString hash = hf.result().toBase64();
return hash;
}
float imagesSimilarity(const QImage& image1, const QImage& image2)
{
int cx = std::min(image1.width(), image2.width());
int cy = std::min(image1.height(), image2.height());
int equalPixelsCount = 0;
for( int x = 0; x < cx; ++x)
{
for(int y = 0; y < cy; ++y)
{
if(image1.pixel(x, y)==image2.pixel(x, y))
++equalPixelsCount;
}
}
if(cx*cy == 0)
return 0.;
return 100. * equalPixelsCount / (cx * cy);
}
}
CaptureWorker::CaptureWorker(QByteArray&& prevPngImage)
: QObject(nullptr)
, m_timer(this)
{
QBuffer buffer(&prevPngImage);
buffer.open(QIODevice::ReadOnly);
m_prevImage.load(&buffer, "PNG");
connect(&m_timer, &QTimer::timeout, this, &CaptureWorker::onTimer);
}
CaptureWorker::~CaptureWorker()
{
}
void CaptureWorker::Start()
{
m_timer.moveToThread(QThread::currentThread());
// one minute timer
m_timer.start(60*1000);
// make first shot immediately
onTimer();
}
void CaptureWorker::onTimer()
{
QScreen *screen = QGuiApplication::primaryScreen();
if(!screen)
{
emit Error("Failed to make screenshot");
return;
}
ScreenShot shot;
auto image = screen->grabWindow(0).toImage();
QBuffer buffer(&shot.pngImage);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
shot.hash = calcHash(shot.pngImage);
shot.similarity = imagesSimilarity(image, m_prevImage);
m_prevImage = image;
emit NewScreenshotCaptured(shot);
}