-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshotcapture.cpp
More file actions
144 lines (117 loc) · 3.99 KB
/
Copy pathsnapshotcapture.cpp
File metadata and controls
144 lines (117 loc) · 3.99 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "snapshotcapture.h"
#include "win32utils.h"
#include <psapi.h>
#include <opencv/cv.h>
#include <QDebug>
SnapshotCapture::SnapshotCapture()
{
initialized = false;
width = height = 0;
}
int SnapshotCapture::getWidth()
{
return width;
}
int SnapshotCapture::getHeight()
{
return height;
}
bool SnapshotCapture::startCapture(HWND hwnd, bool is_screenshot)
{
if (!IsWindow(hwnd)) {
qDebug() << "SnapshotCapture::startCapture(): Not a window";
return false;
}
if ( initialized )
endCapture();
window = hwnd;
screenshot = is_screenshot;
try
{
windowDc = GetWindowDC(screenshot ? 0 : window);
memDc = CreateCompatibleDC(windowDc);
if (!windowDc || !memDc) {
qDebug() << "SnapshotCapture::startCapture(): Could not create WindowDC" << windowDc << "or memDc" << memDc;
return false;
}
GetWindowRect(window, &wr);
GetClientRect(window, &cr);
POINT clientOffset = { 0, 0 };
if (!ClientToScreen(window, &clientOffset)) {
qDebug() << "SnapshotCapture::startCapture(): Could not convert client to screen coordinates" << clientOffset.x << "," << clientOffset.y;
return false;
}
moveRect(cr, clientOffset.x, clientOffset.y);
width = cr.right - cr.left;
height = cr.bottom - cr.top;
if (width == 0 || height == 0) {
qDebug() << "SnapshotCapture::startCapture(): Width" << width << "or height" << height << "are zero";
return false;
}
hbmp = CreateCompatibleBitmap(windowDc, width, height);
if (!hbmp) {
qDebug() << "SnapshotCapture::startCapture(): Could not create compatible bitmap";
return false;
}
SelectObject(memDc, hbmp);
bi =
{
sizeof(BITMAPINFOHEADER), // biSize
width, // biWidth
-height, // biHeight
1, // biPlanes
32, // biBitCount
BI_RGB, // biCompression
0, // biSizeImage
0, // biXPelsPerMeter
0, // biYPelsPerMeter
0, // biClrUser
0 // biClrImportant
};
initialized = true;
return true;
}
catch (std::exception& e)
{
qDebug() << "SnapshotCapture::startCapture(): Exception thrown " << e.what();
endCapture();
throw e;
}
}
void SnapshotCapture::endCapture()
{
if ( initialized ) {
DeleteObject(hbmp);
DeleteObject(memDc);
ReleaseDC(screenshot ? 0 : window, windowDc);
initialized = false;
}
}
bool SnapshotCapture::captureFrame(cv::Mat& dst)
{
if ( !initialized ) {
//qDebug() << "SnapshotCapture::captureFrame() : not initialized";
return false;
}
int offsetX = cr.left;
int offsetY = cr.top;
if (!screenshot) {
offsetX -= wr.left;
offsetY -= wr.top;
}
if (!BitBlt(memDc, 0, 0, width, height, windowDc, offsetX, offsetY, SRCCOPY)) {
qDebug() << "SnapshotCapture::captureFrame() : BitBlt failed";
return false;
}
dst.create(height, width, CV_8UC4);
if (!dst.data) {
qDebug() << "SnapshotCapture::captureFrame() : could not allocate destination matrix";
throw std::invalid_argument("Could not allocate destination matrix.");
}
int success = GetDIBits(memDc, hbmp, 0, (UINT)height, dst.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
if (!success || success == ERROR_INVALID_PARAMETER) {
qDebug() << "SnapshotCapture::captureFrame() : GetDIBits failed";
return false;
}
return true;
}