-
Notifications
You must be signed in to change notification settings - Fork 25
/
Screen-Capturing.cpp
107 lines (92 loc) · 3.01 KB
/
Screen-Capturing.cpp
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
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/videoio.hpp>
#include <Windows.h>
#include <iostream>
using namespace std;
using namespace cv;
class hwnd2Mat
{
public:
hwnd2Mat(HWND hwindow, float scale = 1);
virtual ~hwnd2Mat();
virtual void read();
Mat image;
private:
HWND hwnd;
HDC hwindowDC, hwindowCompatibleDC;
int height, width, srcheight, srcwidth;
HBITMAP hbwindow;
BITMAPINFOHEADER bi;
};
hwnd2Mat::hwnd2Mat(HWND hwindow, float scale)
{
hwnd = hwindow;
hwindowDC = GetDC(hwnd);
hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
SetStretchBltMode(hwindowCompatibleDC, COLORONCOLOR);
RECT windowsize; // get the height and width of the screen
GetClientRect(hwnd, &windowsize);
srcheight = windowsize.bottom;
srcwidth = windowsize.right;
height = (int)(windowsize.bottom * scale);
width = (int)(windowsize.right * scale);
image.create(height, width, CV_8UC4);
// create a bitmap
hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
bi.biSize = sizeof(BITMAPINFOHEADER); //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx
bi.biWidth = width;
bi.biHeight = -height; //this is the line that makes it draw upside down or not
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// use the previously created device context with the bitmap
SelectObject(hwindowCompatibleDC, hbwindow);
read();
};
void hwnd2Mat::read()
{
// copy from the window device context to the bitmap device context
StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, 0, 0, srcwidth, srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors !
GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, image.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow
};
hwnd2Mat::~hwnd2Mat()
{
// avoid memory leak
DeleteObject(hbwindow);
DeleteDC(hwindowCompatibleDC);
ReleaseDC(hwnd, hwindowDC);
};
int main()
{
Mat ui(40, 400, CV_8UC3, Scalar(0, 130, 0));
putText(ui, "Press Esc to stop capturing", Point(30,30), FONT_HERSHEY_COMPLEX, 0.7,
Scalar(0, 0, 255), 1);
Mat bgrImg;
hwnd2Mat capDesktop(GetDesktopWindow());
VideoWriter writer;
int codec = VideoWriter::fourcc('X', 'V', 'I', 'D');
double fps = 10.0;
string filename = "./desktop_capture.avi";
writer.open(filename, codec, fps, capDesktop.image.size(), true);
// check if we succeeded
if (!writer.isOpened()) {
cerr << "Could not open the output video file for write\n";
return -1;
}
while ( true )
{
capDesktop.read();
cvtColor(capDesktop.image, bgrImg, COLOR_BGRA2BGR);
writer << bgrImg;
imshow("desktop capture", ui);
int key = waitKey(5);
if (key == 27)
break;
}
}