#include "THSActionUtil.h" #include "ImgDivider.h" #include "Win32Util.h" //»ñÈ¡¸±ÆÁ HWND getSecondWindow() { list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); list::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 wlist = Win32Util::searchWindow("ͬ»¨Ë³("); bool open = false; HWND mainPage = 0; list::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 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 areaList = recognitionGPArea(img); std::list addList; std::list delList; std::list resultList = recognitionNum(img, areaList); for (std::list::reverse_iterator ele= resultList.rbegin();ele != resultList.rend();++ele) { bool contains = false; std::list::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::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 areaList = recognitionGPArea(img); for (std::list::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 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 resultList; startC += 1; endC -= 1; //·Ö¸ôÐÐÄÚÈÝ int emptyStartRow = -1; int emptyEndRow = -1; int startf = -1; int endf = -1; std::list 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::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 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::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::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::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 THSActionUtil::recognitionNum(cv::Mat img, std::list areaList) { if (!recognitionManager) { recognitionManager = new RecognitionManager(); } //ʶ±ðÊý×Ö std::list codeList; std::list::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 list2 = ImgUtil::splitNum(nums); std::list resultList = recognitionManager->recognitionGPCode(list2); std::list::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; }