-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocv .cpp
61 lines (48 loc) · 1.49 KB
/
ocv .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
#include "framework.h"
#include "ocv.h"
using namespace cv;
#include "wnd.h"
BITMAPINFOHEADER OpnCV::createBitmapHeader(int width, int height)
{
// Create bitmap
bi.biSize = sizeof(bi);
bi.biWidth = width;
bi.biHeight = -height;
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;
return bi;
}
Mat OpnCV::captureScreenMat(HWND hwnd)
{
Mat src;
// Get handle to a device context (DC)
HDC hwindowDC = GetDC(hwnd);
HDC hwindowCompatibleDC = CreateCompatibleDC(hwindowDC);
// Define scale, height and width
int screenx = GetSystemMetrics(SM_XVIRTUALSCREEN);
int screeny = GetSystemMetrics(SM_YVIRTUALSCREEN);
int width = window->SCREEN_WIDTH;
int height = window->SCREEN_HEIGHT;
// Create mat object
src.create(height, width, CV_8UC4);
// Create Bitmap
HBITMAP hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
BITMAPINFOHEADER bi = ocv->createBitmapHeader(width, height);
// Use DC with the bitmap
SelectObject(hwindowCompatibleDC, hbwindow);
// Copy from the window DC to the bitmap DC
StretchBlt(hwindowCompatibleDC, 0, 0, width, height, hwindowDC, screenx, screeny, width, height, SRCCOPY);
GetDIBits(hwindowCompatibleDC, hbwindow, 0, height, src.data, (BITMAPINFO*)&bi, DIB_RGB_COLORS);
// Avoid memory leak
DeleteObject(hbwindow);
DeleteDC(hwindowCompatibleDC);
ReleaseDC(hwnd, hwindowCompatibleDC);
return src;
}
OpnCV* ocv = new OpnCV();