admin
2022-07-08 ae66a5e07a2ebb455e83c65ded12697fd0fece98
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
108
109
110
111
112
113
114
115
116
#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;
 
            if (rect.left < 0)
                rect.left = 0;
 
            if (rect.top < 0)
                rect.top = 0;
 
            width = rect.right - rect.left;
            height = 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()));
    }
}