admin
2022-10-28 086ec74e94654e92b3a1c6f42612d12ef33ff4b2
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "TradeQueueCapture.h"
#include "CaptureUtil.h"
#include "Win32Util.h"
#include <codecvt>
 
 
 
TradeQueueCapture::TradeQueueCapture() {
    openCLExcuter = new OpenCLExcuter();
    openCLExcuter->init();
}
 
 
 
list<int*> TradeQueueCapture::split(cv::Mat img) {
    list<int*> dataItemList;
    // ´ÓͼƬÖмä·Ö¸ô
    int start_row = img.rows / 2 + 1;
    //´Ó·Ö¸îÏß¿ªÊ¼»ñÈ¡µÚÒ»ÐеÄÊý¾Ý
    int start_index = -1;
    int end_index = -1;
 
    for (int r = start_row;r < img.rows;r++) {
        if (!ImgDivider::isRowEmpty(img, r, 0, img.cols / 2, 1)) {
            if (start_index < 0) {
                start_index = r;
            }
            end_index = r;
        }
        else {
            if (start_index > -1 && end_index > -1) {
                break;
            }
        }
    }
    int start_col = 0;
    //ÒѾ­Ïµ¥µÄ
    if (end_index - start_index + 1 > _NUMBER_TRADE_QUEUE_HEIGHT + 4) {
        //Çиʼµã
        for (int c = img.cols - 5; c >= 0; c--) {
            if (ImgDivider::isColFull(img,c,start_index,end_index,1)) {
                start_col = c + 1;
                break;
            }
        }
    }
     start_row = -1;
    int end_row = -1;
    if (start_col > 0) {
    //È¥³ýÉÏϵĺڱß
        for (int r = start_index; r <= end_index; r++) {
            if (!ImgDivider::isRowEmpty(img, r, start_col, img.cols - 1)) {
                if (start_row < 0) {
                    start_row = r;
                    end_row = r;
                }
                end_row = r;
            }
            else {
                if (start_row > 0 && end_row > 0) {
                    break;
                }
            }
        }
    }else {
        start_col = 0;
        start_row = start_index;
        end_row = end_index;
    }
 
    std::list<int*> posList;
    if (start_row < 0 || end_row < 0) {
        return posList;
    }
     
  
    int start_col_ = -1;
    int end_col_ = -1;
    for (int c = img.cols - 1;c >= start_col;c--) {
        if (!ImgDivider::isColEmpty(img, c, start_row, end_row)) {
            if (start_col_ < 0) {
                start_col_ = c;
            }
            end_col_ = c;
        }
        else {
            if (end_col_ > -1 && end_col_ - c > 5) {
                int* pos = (int*)malloc(sizeof(int) * 4);
                pos[0] = end_col_;
                pos[1] = start_row;
                pos[2] = start_col_;
                pos[3] = end_row;
                posList.push_back(pos);
                start_col_ = -1;
                end_col_ = -1;
                if (posList.size() >= 2) {
                    break;
                }
            }
        }
    }
 
    if (start_col_ > -1 && end_col_ > -1 && posList.size()<2) {
        int* pos = (int*)malloc(sizeof(int) * 4);
        pos[0] = end_col_;
        pos[1] = start_row;
        pos[2] = start_col_;
        pos[3] = end_row;
        posList.push_back(pos);
    }    
 
    return posList;
 
}
 
 
TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(cv::Mat img, list<int*> posList) {
    int c = 0;
    unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_TRADE_QUEUE_WIDTH * _NUMBER_TRADE_QUEUE_HEIGHT);
    for (int r = 0;r < _NUMBER_TRADE_QUEUE_HEIGHT;r++) {
        for (int c = 0;c < _NUMBER_TRADE_QUEUE_WIDTH;c++)
        {
            zeroData[r * _NUMBER_TRADE_QUEUE_WIDTH + c] = ImgUtil::NUMS_TRADE_QUEUE[0].data.ptr<uchar>(r)[c];
        }
    }
 
    int length_per_num = 8;
    int ele_count = 2;
    
    int* splitResult = (int*)malloc(sizeof(int) * 1 * 4 * ele_count);
    
    list<int*>::iterator ele = posList.begin();
    int index = 1;
    for (list<int*>::iterator ele = posList.begin(); ele != posList.end(); ++ele)
    {
        splitResult[index * 4 + 0] = (*ele)[0];
        splitResult[index * 4 + 1] = (*ele)[1];
        splitResult[index * 4 + 2] = (*ele)[2];
        splitResult[index * 4 + 3] = (*ele)[3];
        index--;
    }
 
    int line_number_count = ele_count * length_per_num;
    unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count);
    unsigned char types[] = { NUM_TYPE_PRICE, NUM_TYPE_NUM};
    UcharDataInfo typesData = UcharDataInfo();
    typesData.length = ele_count;
    typesData.data = types;
 
    openCLExcuter->splitTradeQueueNum(img, IntDataInfo({ splitResult,(int)(ele_count * 1) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, ele_count, length_per_num);
 
 
    uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_TRADE_QUEUE_HEIGHT * 1) * _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count);
 
 
 
    openCLExcuter->createNumberTemplates(1, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, line_number_count, ImgUtil::numsOneLevel_trade_queue, templateNums);
 
 
 
    uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, 1 * _NUMBER_TRADE_QUEUE_HEIGHT, _NUMBER_TRADE_QUEUE_WIDTH * 10 * line_number_count, _NUMBER_TRADE_QUEUE_WIDTH, _NUMBER_TRADE_QUEUE_HEIGHT, line_number_count);
 
 
 
 
    TradeQueueResult result;
    
    for (int i = 0; i < ele_count; i++)
    {
        string st = "";
        for (int j = 0; j < length_per_num; j++)
        {
            if (j == length_per_num - 2 && i==0) {
                st.append(".");
            }
            st.append(to_string(numberResult[0][length_per_num* i + j]));
        
        }
 
        if (i == 0) {
            result.buy_1_price = st;
        }
        else if (i == 1) {
            result.buy_1_volumn = stoi(st);
        }
    }
 
    free(numberResult[0]);
    int volumn = 0;//stoi(st);
    //printf("½á¹û:%d\n", volumn);
 
    free(totalNumberData);
    free(numberResult);
    free(zeroData);
    free(templateNums);
    img.release();
    
 
    return result;
}
TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(HWND hwnd) {
    //Åжϴ°¿ÚÊÇ·ñÏÔʾ
    if (!Win32Util::isWindowShow(hwnd)) {
        throw string("½»Ò×´°¿Ú¾ä±ú²»¿É¼û");
    }
    //---»ñÈ¡´úÂëÃû³Æ
    //»ñÈ¡½»Ò××Ó´°¿Ú
    HWND child = NULL;
    HWND trade_win = NULL;
    int count = 0;
    do {
        count++;
        child = FindWindowExA(hwnd, child, "#32770", NULL);
        if (child <= 0) {
            break;
        }
        HWND temp = FindWindowExA(child, NULL, "Edit", NULL);
        if (temp > 0) {
            trade_win = child;
            break;
        }
    } while (count < 5);
 
    if (!trade_win) {
        throw string("×Ó½»Ò×´°¿ÚδÕÒµ½");
    }
    HWND code_name_win = GetDlgItem(trade_win, 0x000005C2);
    if (code_name_win <= 0) {
        throw string("´úÂëÃû³Æ¿Ø¼þδÕÒµ½");
    }
    wstring code_name = Win32Util::getText(code_name_win);
    if (code_name.length() < 2) {
        throw string("½»Ò×´°¿ÚδÌî³ä´úÂë");
    }
 
    //²éÕÒ½»Ò×¶ÓÁоä±ú
    HWND queue_win = NULL;
    child = NULL;
    count = 0;
    do {
        count++;
        child = FindWindowExA(hwnd, child, "#32770", NULL);
        if (child <= 0) {
            break;
        }
        if (child != trade_win) {
 
            if (Win32Util::isWindowShow(child)) {
                RECT rect;
                GetWindowRect(child, &rect);
                if (rect.bottom - rect.top > 100) {
                    queue_win = child;
                    break;
                }
            }
        }
    } while (count < 5);
    if (!queue_win) {
        throw string("½»Ò×¶ÓÁÐδÕÒµ½");
    }
 
    string now_time = Win32Util::getNowTime();
 
    cv::Mat img = CaptureUtil::capture(queue_win);
    uchar* data = (uchar*)malloc(sizeof(uchar) * img.cols * img.rows);
    if (img.channels() == 3)
    {
        openCLExcuter->rgb2ThresholdInvert(img.data, img.cols, img.rows, data, 150);
    }
    else if (img.channels() == 4) {
        openCLExcuter->rgba2ThresholdInvert(img.data, img.cols, img.rows, data, 150);
    }
    cv::Mat grayImg = cv::Mat::zeros(img.rows, img.cols, CV_8UC1);
    grayImg.data = data;
    img.release();
    //string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gray.jpg";
    //cv::imwrite(path, grayImg);
 
    list<int*> posList = split(grayImg);
    if (posList.size() < 2) {
        free(data);
        throw string("Êý¾Ý·Ö¸ô³ö´í");
    }
 
    TradeQueueResult result  = recognition_buy_1_volumn(grayImg, posList);
    for (list<int*>::iterator ele = posList.begin(); ele != posList.end(); ele++) {
        free(*ele);
    }
    free(data);
 
    //std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
    // wstring ×ªstring£¬Ö§³ÖÖÐÎÄ
    const char* GBK_LOCALE_NAME = ".936";
    wstring_convert<codecvt_byname<wchar_t, char, mbstate_t>> converter(new codecvt_byname<wchar_t, char, mbstate_t>(GBK_LOCALE_NAME));
    result.code_name = converter.to_bytes(code_name);
    result.time = now_time;
    return result;
}
 
 
TradeQueueResult TradeQueueCapture::recognition_buy_1_volumn(cv::Mat grayImg) {
    list<int*> rowDataList = split(grayImg);
    return recognition_buy_1_volumn(grayImg, rowDataList);
}