-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreenanalyzer.cpp
More file actions
66 lines (50 loc) · 1.57 KB
/
screenanalyzer.cpp
File metadata and controls
66 lines (50 loc) · 1.57 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
#include "screenanalyzer.h"
#include <QApplication>
#include <QScreen>
#include <QPixmap>
#include <QDebug>
#include <QImage>
#include <QTime>
class ScreenAnalyzerPrivate {
public:
QTime timer;
};
ScreenAnalyzer::ScreenAnalyzer() : QThread(), d_ptr(new ScreenAnalyzerPrivate)
{
}
void ScreenAnalyzer::run()
{
setPriority(QThread::HighestPriority);
QScreen *screen = QApplication::primaryScreen();
if (screen) {
d_ptr->timer.start();
forever {
if( QThread::currentThread()->isInterruptionRequested() ) return;
d_ptr->timer.restart();
QImage image = screen->grabWindow(0).toImage();
int width = screen->geometry().width();
int height = screen->geometry().height();
int red = 0, green = 0, blue = 0, sample_count = 0;
// Sum all the colors in the given range
for ( int x = 0; x < width; ++x ) {
for ( int y = 0; y < height; ++y) {
QColor color( image.pixel(x, y) );
red += color.red();
green += color.green();
blue += color.blue();
++sample_count;
}
}
// Get he avarage by dividen all the sums by the sample count
red /= sample_count;
green /= sample_count;
blue /= sample_count;
emit color( QColor(red, green, blue, 255) );
msleep( qMax(20 - d_ptr->timer.elapsed(), 0) );
}
}
}
void ScreenAnalyzer::stop()
{
requestInterruption();
}