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
| #ifdef _MSC_VER #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" ) #endif #define _AFXDLL #include<afxwin.h> #include <opencv2/opencv.hpp> #include <opencv2/highgui.hpp>
LPVOID screenCaptureData = NULL; HBITMAP hBitmap; HDC hDDC; HDC hCDC; int nWidth; int nHeight;
void initGDI() { nWidth = GetSystemMetrics(SM_CXSCREEN); nHeight = GetSystemMetrics(SM_CYSCREEN); screenCaptureData = new char[nWidth*nHeight * 4]; memset(screenCaptureData, 0, nWidth); hDDC = GetDC(GetDesktopWindow()); hCDC = CreateCompatibleDC(hDDC); hBitmap = CreateCompatibleBitmap(hDDC, nWidth, nHeight); SelectObject(hCDC, hBitmap); }
void gdiScreenCapture(LPVOID screenCaptureData) { BitBlt(hCDC, 0, 0, nWidth, nHeight, hDDC, 0, 0, SRCCOPY); GetBitmapBits(hBitmap, nWidth*nHeight * 4, screenCaptureData); IplImage *img_tmp = cvCreateImage(cvSize(nWidth, nHeight), 8, 4); memcpy(img_tmp->imageData, screenCaptureData, nWidth*nHeight * 4); IplImage *img2_tmp = cvCreateImage(cvSize(nWidth, nHeight), 8, 3);
cv::Mat img = cv::cvarrToMat(img_tmp); cv::Mat img2 = cv::cvarrToMat(img2_tmp);
cvtColor(img, img2, CV_BGRA2BGR); imwrite("rgba.jpg", img); cv::waitKey(); }
int main() { initGDI(); gdiScreenCapture(screenCaptureData); return 0; }
|