admin
2022-06-27 eda1a611bc4afcf1c36a6c728f432aec1f688e1b
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "THSActionUtil.h"
#include "ImgDivider.h"
#include "Win32Util.h"
 
 
//»ñÈ¡¸±ÆÁ
HWND getSecondWindow() {
    list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
    list<HWND>::iterator ele;
    for (ele = wlist.begin();ele != wlist.end();ele++) {
        HWND hwnd = *ele;
        string str = Win32Util::getWindowName(hwnd);
        if (str.find("ͬ»¨Ë³") != string::npos && str.find("¸±ÆÁ") != string::npos)
        {
            return hwnd;
        }
    }
    return 0;
}
 
void THSActionUtil::openSecondScreen() {
 
    list<HWND> wlist = Win32Util::searchWindow("ͬ»¨Ë³(");
 
    bool open = false;
    HWND mainPage = 0;
 
    list<HWND>::iterator ele;
    for (ele = wlist.begin();ele != wlist.end();ele++) {
        HWND hwnd = *ele;
        string str = Win32Util::getWindowName(hwnd);
        if (str.find("ͬ»¨Ë³") != string::npos && str.find("¸±ÆÁ") != string::npos)
        {
            cout << str << endl;
            SetForegroundWindow(hwnd);
            SetFocus(hwnd);
            open = true;
            break;
        }
 
        if (str.find("ͬ»¨Ë³") != string::npos && str.find("ÈÈÃŹÉ") != string::npos) {
            cout << hwnd << endl;
            //»ñÈ¡³ß´ç
            RECT rc;
            GetWindowRect(hwnd, &rc);
            if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) {
                mainPage = hwnd;
            }
        }
 
    }
 
    if (!open) {
        Win32Util::focus(mainPage);
        //²éÕÒÖ÷´°¿ÚµÄ¹¤¾ßÀ¸
        HWND tool = FindWindowExA(mainPage, NULL, "AfxControlBar100s", NULL);
 
        //»ñÈ¡µÚÒ»¸ö×Ó´°¿Ú
        tool = FindWindowExA(tool, NULL, NULL, NULL);
        cout << "²Ù×÷À¸£º" << tool << endl;
        //´ò¿ª¸±ÆÁ£¬¶à´°°´Å¥IDΪ:0x00007AF9
        HWND btn = GetDlgItem(tool, 0x00007AF9);
        RECT rc;
        GetWindowRect(btn, &rc);
        //»ñÈ¡ÐèÒªµã»÷µÄλÖÃ
        int x = rc.left + (rc.right - rc.left) / 3;
        int y = rc.top + (rc.bottom - rc.top) / 3;
        //µã»÷¶à´°
        Win32Util::click(x, y);
        //µã»÷¸±ÆÁ1 
        Win32Util::click(x + 10, y + 21 * 7 + 5, 500);
    }
}
//Ìí¼Ó¹ÉƱ
void THSActionUtil::setGP(std::string quickCode, list<std::string> codeList) {
    //´ò¿ª¸±ÆÁ
    HWND sw = getSecondWindow();
    if (sw <= 0)
    {
        openSecondScreen();
        Sleep(2000);
        sw = getSecondWindow();
    }
    if (sw <= 0) {
        throw("δ´ò¿ª¸±ÆÁ");
    }
 
    //´ò¿ª°å¿é
    Win32Util::keyboardNum(quickCode);
    Win32Util::keyboard(VK_RETURN, 200);
    Sleep(1000);
    //ÉèÖðå¿éÖÐµÄ¹ÉÆ±
 
    //»ñÈ¡°å¿éÄÚÈݾä±ú
 
    HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL);
    cv::Mat img = CaptureUtil::capture(content);
    if (img.cols <= 0 || img.rows <= 0) {
        throw("°å¿é½ØÆÁÄÚÈÝΪ¿Õ");
    }
 
    std::list<GPCodeArea>  areaList = recognitionGPArea(img);
 
    std::list<string> addList;
    std::list<GPCodeArea> delList;
    std::list<GPCodeArea> resultList = recognitionNum(img, areaList);
 
    for (std::list<GPCodeArea>::reverse_iterator ele= resultList.rbegin();ele != resultList.rend();++ele) {
 
        bool contains = false;
        std::list<string>::iterator e;
        for (e = codeList.begin();e != codeList.end();e++) {
            if (*e == (*ele).code) {
                contains = true;
                break;
            }
        }
 
        if (!contains) {
            delList.push_back(*ele);
        }
        else
        {
            codeList.remove((*ele).code);
        }
    
    }
    //-----ÏÈɾ³ýÐèҪɾ³ýµÄ
    //»ñÈ¡ÄÚÈݰå¿é×ø±ê
    RECT rect;
    GetWindowRect(content, &rect);
    
 
    for (std::list<GPCodeArea>::iterator ele = delList.begin();ele != delList.end();++ele) {     
        int x = rect.left;
        int y = rect.top;
        x += (*ele).startx+5;
        y += (*ele).starty+5;
        //Ñ¡ÖÐɾ³ý
        Win32Util::click(x,y,50);
        Win32Util::keyboard(VK_DELETE, 50);
    }
 
    //----Ôö¼Ó
    //½ØÍ¼£¬Ê¶±ð³öÔö¼Ó°´Å¥Î»Ö㬵ã»÷Ôö¼Ó£¬ÊäÈëÄÚÈÝ
 
}
 
 
void add(string code) {
    //´ò¿ª¸±ÆÁ
    HWND sw = getSecondWindow();
    if (sw <= 0)
    {
        openSecondScreen();
        Sleep(2000);
        sw = getSecondWindow();
    }
    if (sw <= 0) {
        throw("δ´ò¿ª¸±ÆÁ");
    }
 
    HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL);
    cv::Mat img = CaptureUtil::capture(content);
    if (img.cols <= 0 || img.rows <= 0) {
        throw("°å¿é½ØÆÁÄÚÈÝΪ¿Õ");
    }
    std::list<GPCodeArea>  areaList = recognitionGPArea(img);
    for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();++ele) {
        GPCodeArea codeArea = *ele;
        if (codeArea.type == IMG_TYPE_ADD) {
            RECT rc;
            GetWindowRect(HWND(0x00161728), &rc);
            int y = rc.top + codeArea.starty + (codeArea.endy - codeArea.starty) / 2;
            int x = rc.left + codeArea.startx + (codeArea.endx - codeArea.startx) / 2;
            Win32Util::click(x, y, 50);
            //ÊäÈë¹ÉƱ´úÂë
            Win32Util::keyboardNum(code,1000);
            Win32Util::keyboard(VK_RETURN, 1500);
            //¹Ø±Õ°´Å¥
            Sleep(100);
            HWND close = FindWindowA(0, "Ìí¼Ó¹ÉƱ");
            SendMessage(close, WM_CLOSE, 0, 0);
            break;
        }
    }
}
 
//ʶ±ð¹ÉƱ´úÂë
std::list<GPCodeArea>  THSActionUtil::recognitionGPArea(cv::Mat img) {
    //»ñÈ¡title·Ö¸ôÏß
    int rows = img.rows;
    int cols = img.cols;
    int r;
    int contentStartRow = -1;
    for (r = 5;r < img.rows;r++) {
        if (ImgDivider::isRowFull(img, r, cols - 100, cols - 5, 2)) {
            contentStartRow = r;
        }
 
        if (contentStartRow > -1) {
            break;
        }
    }
 
    if (contentStartRow < 0) {
        throw("ÆðʼÐзָô³ö´í");
    }
 
    //·Ö¸ôÁÐ
    int c = 0;
    int startC = -1;
    int endC = -1;
    for (c = 50;c < cols;c++) {
        if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) {
            if (startC < 0) {
                startC = c;
            }
            else {
                if (c - startC < 20) {
                    startC = c;
                }
                else {
                    endC = c;
                    break;
                }
            }
        }
    }
 
    if (startC < 0 || endC < 0) {
        throw("ÄÚÈÝ¿ò·Ö¸ô³ö´í");
    }
    //cv::imshow("ÄÚÈÝ", cv::Mat(img, cv::Rect(startC, contentStartRow, endC - startC, rows - contentStartRow)));
    std::list<GPCodeArea> resultList;
 
    startC += 1;
    endC -= 1;
 
    //·Ö¸ôÐÐÄÚÈÝ
    int emptyStartRow = -1;
    int emptyEndRow = -1;
    int startf = -1;
    int endf = -1;
    std::list<int*> dataItemList;
 
    for (int i = contentStartRow + 30;i < rows;i++) {
        bool empty = ImgDivider::isRowEmpty(img, i, startC, startC + 50, 1, 64) && ImgDivider::isRowEmpty(img, i, startC + (endC - startC) / 2 - 40, startC + (endC - startC) / 2 + 40, 1, 64);
        if (empty) {
            if (emptyStartRow < 0) {
                emptyStartRow = i;
                emptyEndRow = i;
            }
            else {
                emptyEndRow = i;
            }
 
            if (emptyEndRow - emptyStartRow > 50 && dataItemList.size() > 0) {
                //ûÓÐÊý¾ÝÁË
                break;
            }
 
            if (startf > -1 && endf > -1 && emptyEndRow - emptyStartRow > 4) {
                //ÄÚÈÝ×ø±ê
                   //LogUtil::debug("ÄÚÈݵĸ߶ÈΪ£º%d \n", endf - startf);
                int* dd = (int*)malloc(sizeof(int) * 4);
                *dd = startC;
                *(dd + 1) = startf;
                *(dd + 2) = endC;
                *(dd + 3) = endf;
                //ÐÐÊý¾Ý¸ß´óÓÚ6²ÅΪÓÐЧµÄÐиß
                if (endf - startf > 6)
                {
                    dataItemList.push_back(dd);
                }
                startf = -1;
                endf = -1;
                emptyStartRow = i;
                emptyEndRow = i;
            }
 
        }
        else
        {
            //Êý¾Ý¿ªÊ¼
            if (startf < 0) {
                startf = i;
                endf = i;
            }
            else {
                endf = i;
            }
            emptyStartRow = -1;
            //Êý¾Ý½áÊø
        }
    }
 
    //·Ö¸ôÁÐ
    //·Ö¸ôÿһÁеÄÊý¾Ý
    std::list<int*>::iterator ele;
    int index = 0;
    for (ele = dataItemList.begin(); ele != dataItemList.end();ele++) {
        index++;
        int startRow = *(*ele + 1);
        int startCol = *(*ele + 0);
        int endRow = *(*ele + 3);
        int endCol = *(*ele + 2);
        LogUtil::debug("%d %d %d %d\n", startRow, startCol, endRow, endCol);
 
        //±£´æÐÐÊý¾Ý
        if (false) {
            std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\split\\";
            path.append(std::to_string(index)).append(".jpg");
            imwrite(path, cv::Mat(img, cv::Rect(startCol, startRow, endCol - startCol + 1, endRow - startRow + 1)));
        }
 
        int emptyStart = -1;
        int emptyEnd = -1;
        int dataStart = -1;
        int dataEnd = -1;
 
        std::list<int*> rowDataList;
 
        for (int i = startCol;i <= endCol;i++) {
            bool empty = ImgDivider::isColEmpty(img, i, startRow, endRow, 64);
            if (empty) {
                if (emptyStart < 0) {
                    emptyStart = i;
                    emptyEnd = i;
                }
                else {
                    emptyEnd = i;
                }
 
                //3¸ö¼°ÒÔÉϵĿհ×Êý¾Ý²Å·ÖÁÐ
                if (emptyEnd - emptyStart >= 5 && dataEnd - dataStart > 0) {
                    int* dd = (int*)malloc(sizeof(int) * 4);
                    *dd = dataStart;
                    *(dd + 1) = startRow;
                    *(dd + 2) = dataEnd;
                    *(dd + 3) = endRow;
                    rowDataList.push_back(dd);
                    dataEnd = -1;
                    dataStart = -1;
                    if (rowDataList.size() >= 2) {
                        break;
                    }
                }
            }
            else {
                if (dataStart < 0) {
                    dataStart = i;
                    dataEnd = i;
                }
                else {
                    dataEnd = i;
                }
 
                emptyStart = -1;
                emptyEnd = -1;
            }
        }
        //ºóÃæµÄÊý¾ÝûÓÐ×ã¹»µÄ¿Õ°×·Ö¸ô
        if (dataEnd - dataStart > 0) {
            int* dd = (int*)malloc(sizeof(int) * 4);
            *dd = dataStart;
            *(dd + 1) = startRow;
            *(dd + 2) = dataEnd;
            *(dd + 3) = endRow;
            rowDataList.push_back(dd);
        }
 
        int rowDataSize = rowDataList.size();
        if (rowDataSize == 2) {
 
            std::list<int*>::iterator ele = rowDataList.begin();
            std::advance(ele, 1);
 
            GPCodeArea area = GPCodeArea();
            area.type = IMG_TYPE_GP;
            area.startx = **ele;
            area.starty = *(*ele + 1);
            area.endx = *(*ele + 2);
            area.endy = *(*ele + 3);
            resultList.push_back(area);
        }
        else {
            std::list<int*>::iterator ele = rowDataList.begin();
            GPCodeArea area = GPCodeArea();
            area.type = IMG_TYPE_ADD;
            area.startx = **ele;
            area.starty = *(*ele + 1);
            area.endx = *(*ele + 2);
            area.endy = *(*ele + 3);
            resultList.push_back(area);
        }
 
 
 
        //±£´æÊý¾Ý
        if (false)
        {
            std::list<int*>::iterator ele1;
            int cc = 0;
            for (ele1 = rowDataList.begin(); ele1 != rowDataList.end();ele1++) {
                if (*ele1)
                {
                    int startCRow = *(*ele1 + 1);
                    int startCCol = *(*ele1 + 0);
                    int endCRow = *(*ele1 + 3);
                    int endCCol = *(*ele1 + 2);
                    cv::Mat    temp = cv::Mat(img, cv::Rect(startCCol, startCRow, endCCol - startCCol + 1, endCRow - startCRow + 1));
                    std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\split\\";
                    path = path.append(std::to_string(index));
                    path = path.append("_");
                    path = path.append(std::to_string(cc));
                    path = path.append(".jpg");
                    imwrite(path.c_str(), temp);
                }
                cc++;
            }
        }
 
    }
 
    return resultList;
 
}
 
std::list<GPCodeArea>   THSActionUtil::recognitionNum(cv::Mat img, std::list<GPCodeArea> areaList) {
 
    if (!recognitionManager) {
        recognitionManager = new RecognitionManager();
    }
    //ʶ±ðÊý×Ö
    std::list<GPCodeArea> codeList;
 
    std::list<GPCodeArea>::iterator ele;
 
    for (ele = areaList.begin();ele != areaList.end();ele++) {
        GPCodeArea codeArea = *ele;
        if (codeArea.type == IMG_TYPE_GP) {
            cv::Mat nums = cv::Mat(img, cv::Rect(codeArea.startx, codeArea.starty, codeArea.endx - codeArea.startx + 1, codeArea.endy - codeArea.starty + 1));
            std::list<cv::Mat> list2 = ImgUtil::splitNum(nums);
            std::list<uchar> resultList = recognitionManager->recognitionGPCode(list2);
            std::list<uchar>::iterator ele1;
            string code = "";
            for (ele1 = resultList.begin();ele1 != resultList.end();++ele1) {
                code.append(to_string(*ele1));
            }
            codeArea.code = code;
            codeList.push_back(codeArea);
        }
    }
 
    return codeList;
}