#include "win32_screen_shots.h"
|
|
namespace _wss
|
{
|
cv::Mat get_window_bits(HWND h)
|
{
|
|
//printf("´°Ì壺%d \n",h==NULL?0: 1);
|
cv::Mat result;
|
HBITMAP bit = nullptr;
|
RECT rect{ 0 };
|
int width = 0, height = 0;
|
HDC hdc = nullptr, hdc_compatible = nullptr;
|
BITMAP info{ 0 };
|
int channels = 0;
|
|
if (h == nullptr)
|
return result;
|
|
do
|
{
|
if (GetWindowRect(h, &rect) == FALSE)
|
break;
|
|
width = abs( rect.right - rect.left);
|
height =abs( rect.bottom - rect.top);
|
|
hdc = GetWindowDC(h);
|
if (hdc == nullptr)
|
break;
|
|
hdc_compatible = CreateCompatibleDC(hdc);
|
if (hdc_compatible == nullptr)
|
break;
|
|
bit = CreateCompatibleBitmap(hdc, width, height);
|
if (bit == nullptr)
|
break;
|
|
SelectObject(hdc_compatible, bit);
|
|
if (BitBlt(hdc_compatible, 0, 0, width, height, hdc, 0, 0, SRCCOPY) == FALSE)
|
break;
|
|
if (GetObjectA(bit, sizeof(BITMAP), &info) == 0)
|
break;
|
|
channels = info.bmBitsPixel == 1 ? 1 : info.bmBitsPixel / 8;
|
result.create(cv::Size(info.bmWidth, info.bmHeight), CV_MAKETYPE(CV_8U, channels));
|
if (GetBitmapBits(bit, info.bmHeight * info.bmWidth * channels, result.data) == 0)
|
break;
|
//µ¥Í¨µÀͼ
|
//result.create(cv::Size(info.bmWidth, info.bmHeight), CV_8UC1);
|
//if (GetBitmapBits(bit, info.bmHeight * info.bmWidth * channels, result.data) == 0)
|
// break;
|
} while (false);
|
|
if (hdc)
|
ReleaseDC(h, hdc);
|
|
if (hdc_compatible)
|
DeleteDC(hdc_compatible);
|
|
if (bit)
|
DeleteObject(bit);
|
|
return result;
|
}
|
|
cv::Mat screen_shot_by_window(HWND h)
|
{
|
return get_window_bits(h);
|
}
|
|
cv::Mat screen_shot_by_window(std::string name)
|
{
|
return get_window_bits(FindWindowA(0, name.c_str()));
|
}
|
|
cv::Mat screen_shot_by_full_screen(HWND h)
|
{
|
cv::Mat desktop;
|
|
if (h == nullptr)
|
return desktop;
|
|
desktop = get_window_bits(GetDesktopWindow());
|
if (desktop.empty())
|
return desktop;
|
|
RECT rect{ 0 };
|
if (GetWindowRect(h, &rect) == FALSE)
|
return desktop;
|
|
if (rect.left < 0)
|
rect.left = 0;
|
if (rect.top < 0)
|
rect.top = 0;
|
|
int width = rect.right - rect.left;
|
int height = rect.bottom - rect.top;
|
cv::Mat result = desktop(cv::Rect(rect.left, rect.top, width, height));
|
return result;
|
}
|
|
cv::Mat screen_shot_by_full_screen(std::string name)
|
{
|
return screen_shot_by_full_screen(FindWindowA(0, name.c_str()));
|
}
|
}
|