#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('M', 'J', 'P', 'G');
double fps = 10.0;
string filename = "example_1000.avi";
if (!writer.open(filename, codec, fps, capDesktop.image.size(), true)) {
cerr << "Could not open the output video file for write. Error code: " << writer.getBackendName() << endl;
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;
}
}