#include "THSActionUtil.h" #include "ImgDivider.h" #include "Win32Util.h" #include bool isL2Screen(string str) { if (str.find("ͬ»¨Ë³(") != string::npos) { cout << str << endl; if (str.find("¸±ÆÁ") == string::npos) { if (str.find("ÈÈÃŹÉ") != string::npos || str.find("Îҵİå¿é") != string::npos) { return true; } } } return false; } bool isMainScreen(string str) { if (str.find("ͬ»¨Ë³(") != string::npos) { cout << str << endl; if (str.find("¸±ÆÁ") == string::npos) { return true; } } return false; } bool isSecondScreen(string str) { return str.find("ͬ»¨Ë³(") != string::npos && str.find("¸±ÆÁ1") != string::npos; } bool isThirdScreen(string str) { return str.find("ͬ»¨Ë³(") != string::npos && str.find("¸±ÆÁ2") != string::npos; } std::list splitGPCodeNum(cv::Mat img) { std::list nums = ImgUtil::splitNum(img, 96); if (nums.size() != 6) { int retryCount = 0; while (retryCount < 4) { retryCount++; nums = ImgUtil::splitNum(img, 96 + retryCount * 10); if (nums.size() == 6) { break; } } } if (nums.size() != 6) { std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append(std::to_string(rand())).append("_listen.jpg"); imwrite(path, img); throw string("´úÂëÊý×Ö·Ö¸ô³ö´í(32)"); } return nums; } //»ñÈ¡Õû¸öL2µÄ´úÂëÑ¡ÔñÇøÓò list splitL2WholeCateArea(cv::Mat img) { int contentStartRow = -1; int contentEndRow = -1; for (int r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, 10, 200, 2, 10, 30)) { if (contentStartRow < 0) { contentStartRow = r; } else { if (r - contentStartRow > 10) { contentEndRow = r; break; } } } } if (contentStartRow < 0 || contentEndRow < 0) { throw string("ÆðʼÐлò½áÊøÐзָô³ö´í(23)"); } //·Ö¸ôÁÐ list dataColIndexs; int startf = -1; int endf = -1; int startIndex = -1; for (int i = 10; i < img.cols; i++) { if (startIndex == -1) { startIndex = i; } bool full = ImgDivider::isColFull(img, i, contentStartRow, contentEndRow, 2); if (full) { if (startf < 0) { startf = i; endf = i; } else { endf = i; } } else { if (startf > -1 && endf > -1) { int width = endf - startf + 1; int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = startf - 1; dataColIndexs.push_back(dd); startIndex = i; } startf = -1; endf = -1; } } if (startf > -1 && endf > -1) { int width = endf - startf + 1; int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = startf - 1; LogUtil::debug("ÁÐÊý¾Ý£º%d-%d\n", *dd, *(dd + 1)); dataColIndexs.push_back(dd); startf = -1; endf = -1; } if (img.cols - startIndex > 50) { int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = img.cols - 1; dataColIndexs.push_back(dd); } std::list areaList; list::iterator ele; for (ele = dataColIndexs.begin(); ele != dataColIndexs.end(); ele++) { int* p = *ele; int startx = *p; int endx = *(p + 1); int endRow = 0; //ÅжϽáÊøÐÐ for (int r = contentStartRow + 10; r < img.rows; r++) { bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30); if (full) { endRow = r; break; } } GPCodeArea area = GPCodeArea(); area.startx = startx; area.endx = endx; area.starty = contentStartRow; area.endy = contentEndRow; areaList.push_back(area); free(*ele); } return areaList; } //»ñÈ¡¸±ÆÁ2 HWND THSActionUtil::getThirdWindow() { list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isThirdScreen(str)) { return hwnd; } } return 0; } HWND THSActionUtil::getIndustryWindow() { 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) { return hwnd; } } return 0; } HWND THSActionUtil::getTradeDelegateWindow() { list wlist = Win32Util::searchWindow("רҵ°æÏµ¥"); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; return hwnd; } return 0; } HWND THSActionUtil::getTradeSimpleDelegateWindow() { list wlist = Win32Util::searchWindow("ͬ»¨Ë³Ô¶º½°æ"); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; hwnd = FindWindowExA(hwnd, NULL, NULL, NULL); string name = Win32Util::getWindowName(hwnd); if (name.find("ÍøÉÏ¹ÉÆ±½»Ò×ϵͳ") != string::npos) { return hwnd; } } return 0; } HWND THSActionUtil::getDXJLWindow() { HWND hwnd = getDXJLMainWindow(); hwnd = FindWindowExA(hwnd, NULL, "#32770", NULL); if (Win32Util::isWindowShow(hwnd)) { return hwnd; } return 0; } HWND THSActionUtil::getDXJLMainWindow() { list wlist = Win32Util::searchWindow("¶ÌÏß¾«Áé"); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; if (Win32Util::isWindowShow(hwnd)) { return hwnd; } } return 0; } HWND THSActionUtil::getTradeSuccessWindow() { list wlist = Win32Util::searchWindow("ÍøÉÏ¹ÉÆ±½»Ò×ϵͳ"); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; return hwnd; } return 0; } //»ñÈ¡¸±ÆÁ1 HWND THSActionUtil::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 (isSecondScreen(str)) { return hwnd; } } return 0; } HWND THSActionUtil::getMainWin() { list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isMainScreen(str)) { return hwnd; } } return 0; } HWND THSActionUtil::getL2Win() { list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isL2Screen(str)) { //ÅжÏÄÚÈÝÊÇ·ñΪL2µÄÄÚÈÝ HWND mainWin = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL); HWND temp = NULL; int content_count = 0; while (TRUE) { temp = FindWindowExA(mainWin, temp, "AfxWnd100s", NULL); if (temp == NULL || temp <= 0) { break; } if (Win32Util::isWindowShow(temp)) { content_count++; } } if (content_count >= L2_CHENNL_COUNT*2) { return hwnd; } } } return 0; } bool THSActionUtil::checkEnv() { list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); list::iterator ele; bool fp = false; bool fp2 = false; bool zp = false; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isSecondScreen(str)) { fp = true; } if (isThirdScreen(str)) { fp2 = true; } if (isL2Screen(str)) { zp = true; } } if (!zp) { throw string("ͬ»¨Ë³Ö÷ÆÁL2´°¿Úδ´ò¿ª(01)"); } if (!fp) { throw string("ͬ»¨Ë³¸±ÆÁ1ûÓдò¿ª(02)"); } if (!fp2) { throw string("ͬ»¨Ë³¸±ÆÁ2ûÓдò¿ª(03)"); } return true; } void THSActionUtil::openL2Screen() { HWND l2 = getL2Win(); if (l2 <= 0) { //³¢ÊÔ´ò¿ªÖ÷ÆÁ HWND main = THSActionUtil::getMainWin(); if (main > 0) { Win32Util::focus(main); Win32Util::keyboardNum(".00", 200); Win32Util::keyboard(13, 10); Sleep(1000); } } } bool THSActionUtil::correctL2ScreenPlate(MatOcrFun matOcrFun) { HWND mainPage = getL2Win(); if (mainPage <= 0) { throw string("L2ÆÁδ´ò¿ª"); } //»ñÈ¡°å¿éµÄMat HWND content = FindWindowExA(mainPage, NULL, "AfxFrameOrView100s", NULL); //½ØÍ¼ cv::Mat oimg = CaptureUtil::capture(content); cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); //·Ö¸ôͼƬ list areaList = splitL2WholeCateArea(img); int index = 0; for (list::iterator e = areaList.begin(); e != areaList.end(); ++e) { GPCodeArea area = *e; RECT plateRect; Win32Util::getWindowRect(content, &plateRect); //»ñÈ¡´°ÌåλÖà try { int rows = area.endy - area.starty + 1; int cols = area.endx - area.startx + 1; cv::Mat mat = cv::Mat::zeros(rows, cols, CV_8UC1); cv::Mat mat1 = cv::Mat(img, cv::Rect(area.startx, area.starty, cols, rows)); uchar* datas = (uchar*)malloc(sizeof(uchar) * mat.rows * mat.cols); for (int r = 0; r < rows; r++) { for (int c = 0; c < cols; c++) { datas[r * cols + c] = mat1.ptr(r)[c]; } } mat.data = datas; list results = matOcrFun("Êװ幨ע", mat); free(datas); //»ñÈ¡µã»÷λÖà for (list::iterator ele = results.begin(); ele != results.end(); ++ele) { OCRResult result = *ele; int x = area.startx + result.rect.left; int y = area.starty + result.rect.top; cv::Rect ocrRect = cv::Rect(x, y, result.rect.right - result.rect.left + 1, result.rect.bottom - result.rect.top + 1); //ÅжÏÊÇ·ñÑ¡ÖУ¬Ñ¡Öк󱳾°ÑÕÉ«µÄɫֵΪ15£¬·ñÔò¾ÍδѡÖÐ int selectColorCount = 0; for (int r = ocrRect.y; r < ocrRect.y + ocrRect.height; r++) { for (int c = ocrRect.x; c < ocrRect.x + ocrRect.width; c++) { if (img.ptr(r)[c] == 15) { selectColorCount++; } } } //ImgUtil::markMat(img,ocrRect,255,1); Win32Util::showWindow(content); Win32Util::focus(content); // δ±»Ñ¡ÖÐ if (selectColorCount < 20) { for (int i = 0; i < 2; i++) { Win32Util::click(plateRect.left + x + 10, plateRect.top + y + (result.rect.bottom - result.rect.top) / 2, 100); } } } } catch (string st) { printf(st.c_str()); return FALSE; } } if (FALSE) { string path = string("D:/test_mark.png"); cv::imwrite(path, img); } return TRUE; } void THSActionUtil::openSecondScreen() { HWND second = getSecondWindow(); if (second > 0) { Win32Util::showWindow(second); return; } list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); bool open = false; HWND mainPage = getMainWin(); list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isSecondScreen(str)) { cout << str << endl; SetForegroundWindow(hwnd); SetFocus(hwnd); open = true; break; } if (isL2Screen(str)) { cout << hwnd << endl; //»ñÈ¡³ß´ç RECT rc; GetWindowRect(hwnd, &rc); if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) { mainPage = hwnd; } } } if (!open) { if (mainPage <= 0) { throw string("δÕÒµ½Ê×Ò³(04)"); } 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::openThirdScreen() { 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 (isThirdScreen(str)) { cout << str << endl; SetForegroundWindow(hwnd); SetFocus(hwnd); open = true; break; } if (isL2Screen(str)) { cout << hwnd << endl; //»ñÈ¡³ß´ç RECT rc; GetWindowRect(hwnd, &rc); if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) { mainPage = hwnd; } } } if (!open) { if (mainPage <= 0) { throw string("δÕÒµ½Ê×Ò³(05)"); } 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); Sleep(50); //µã»÷¸±ÆÁ2 Win32Util::click(x + 10, y + 21 * 8 + 5, 500); } } void THSActionUtil::openTradeSuccessScreen() { HWND hwnd = getTradeSuccessWindow(); if (hwnd <= 0) { throw string("½»Ò×´°¿Úδ´ò¿ª(06)"); } hwnd = FindWindowExA(hwnd, NULL, "AfxMDIFrame140s", NULL); hwnd = FindWindowExA(hwnd, NULL, "AfxWnd140s", NULL); hwnd = FindWindowExA(hwnd, NULL, NULL, "HexinScrollWnd"); hwnd = FindWindowExA(hwnd, NULL, NULL, "HexinScrollWnd2"); hwnd = FindWindowExA(hwnd, NULL, "SysTreeView32", NULL); if (hwnd <= 0) { throw string("δÕÒµ½½»Ò×´°¿Ú²Ëµ¥(07)"); } //µã»÷µ±ÈÕίÍÐ SendMessage(hwnd, WM_LBUTTONDOWN, 0x00000001, 0x00E70047); SendMessage(hwnd, WM_LBUTTONUP, 0x00000000, 0x00E70047); } void THSActionUtil::openDXJLScreen() { HWND hw = getL2Win(); if (hw <= 0) { throw string("L2δ´ò¿ª"); } Win32Util::focus(hw); Win32Util::keyboardNum("DXJL", 300); Win32Util::keyboard(13, 10); Sleep(1000); DEVMODE device = Win32Util::getL2ScreenInfo(); int device_width = device.dmPelsWidth; int device_height = device.dmPelsHeight; POINTL device_pos = device.dmPosition; Win32Util::moveWin(THSActionUtil::getDXJLMainWindow(), device_pos.x + device_width / 2 + 30, device_pos.y + device_height / 2, 1120, 900); } //Ìí¼Ó¹ÉƱ bool THSActionUtil::setGP(std::string quickCode, list codeList, RecognitionManager* recognitionManager, MatOcrFun ocrFun) { list tempCodeList; for (std::list::iterator e = codeList.begin(); e != codeList.end(); e++) { tempCodeList.push_back(*e); } //´ò¿ª¸±ÆÁ HWND sw = getSecondWindow(); if (sw <= 0) { openSecondScreen(); Sleep(2000); sw = getSecondWindow(); } if (sw <= 0) { throw string("δ´ò¿ª¸±ÆÁ(11)"); } //´ò¿ª°å¿é RECT rect; GetWindowRect(sw, &rect); Win32Util::mouseMove(rect.left + 10, rect.top + 5, 1); //Win32Util::click(10); Win32Util::focus(sw); Win32Util::keyboardNum(quickCode, 200); Win32Util::keyboard(VK_RETURN, 1000); Sleep(2000); //ÉèÖðå¿éÖÐµÄ¹ÉÆ± //»ñÈ¡°å¿éÄÚÈݾä±ú HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL); cv::Mat oimg = CaptureUtil::capture(content); cout << "¸±ÆÁ½ØÍ¼:" << oimg.rows << "-" << oimg.cols << endl; if (oimg.cols <= 0 || oimg.rows <= 0) { throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(12)"); } cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); std::list areaList = recognitionGPArea(img); std::list addList; std::list delList; std::list resultList = recognitionGPCodeNum(img, areaList, recognitionManager); for (std::list::reverse_iterator ele = resultList.rbegin(); ele != resultList.rend(); ++ele) { bool contains = false; std::list::iterator e; for (e = tempCodeList.begin(); e != tempCodeList.end(); e++) { if (*e == (*ele).code) { contains = true; break; } } if (!contains) { delList.push_back(*ele); } else { tempCodeList.remove((*ele).code); } } //-----ÏÈɾ³ýÐèҪɾ³ýµÄ //»ñÈ¡ÄÚÈݰå¿é×ø±ê GetWindowRect(sw, &rect); Win32Util::mouseMove(rect.left + 10, rect.top + 5, 1); Win32Util::click(10); for (std::list::iterator ele = delList.begin(); ele != delList.end(); ++ele) { RECT rect; GetWindowRect(content, &rect); Win32Util::focus(sw); int x = rect.left; int y = rect.top; x += (*ele).startx + 5; y += (*ele).starty + 5; //Ñ¡ÖÐɾ³ý Win32Util::mouseMove(x, y, 50); Win32Util::click(10); Win32Util::keyboard(VK_DELETE, 50); } img.release(); //----Ôö¼Ó //½ØÍ¼£¬Ê¶±ð³öÔö¼Ó°´Å¥Î»Ö㬵ã»÷Ôö¼Ó£¬ÊäÈëÄÚÈÝ for (std::list::iterator ele = tempCodeList.begin(); ele != tempCodeList.end(); ++ele) { if ((*ele).length() > 0) { addGP(*ele, ocrFun); Sleep(100); } } oimg = CaptureUtil::capture(content); if (oimg.cols <= 0 || oimg.rows <= 0) { throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(13)"); } img = ImgUtil::grayImage(oimg); areaList = recognitionGPArea(img); resultList = recognitionGPCodeNum(img, areaList, recognitionManager); for (std::list::iterator ele = resultList.begin(); ele != resultList.end(); ++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) { codeList.remove((*ele).code); } } if (codeList.size() == 0) { return true; } return false; } bool THSActionUtil::addGP(string code, MatOcrFun ocrFun) { clock_t start_time = clock(); //´ò¿ª¸±ÆÁ HWND sw = getSecondWindow(); if (sw <= 0) { openSecondScreen(); Sleep(2000); sw = getSecondWindow(); } if (sw <= 0) { throw string("δ´ò¿ª¸±ÆÁ(14)"); } HWND content = FindWindowExA(sw, NULL, "AfxFrameOrView100s", NULL); cv::Mat oimg = CaptureUtil::capture(content); cv::Mat img = ImgUtil::grayImage(oimg); if (img.cols <= 0 || img.rows <= 0) { throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(15)"); } 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) { Win32Util::focus(sw); RECT rc; GetWindowRect(content, &rc); int y = rc.top + codeArea.starty + (codeArea.endy - codeArea.starty) / 2; int x = rc.left + codeArea.startx + (codeArea.endx - codeArea.startx) / 2; //½ØÍ¼ HWND addGPWin = 0; for (int i = 0; i < 6; i++) { Win32Util::click(x, y, 50); for (int j = 0; j < 20; j++) { list winList = Win32Util::searchWindow("Ìí¼Ó¹ÉƱ"); if (winList.size() > 0) { for (list::iterator ele = winList.begin(); ele != winList.end(); ele++) { if (Win32Util::isWindowShow(*ele)) { addGPWin = *ele; break; } else { cout << "´úÂë¿òûÏÔʾ" << endl; } } } else { cout << "ûÓлñÈ¡µ½´úÂë¿ò" << endl; Sleep(50); } } if (addGPWin > 0) { break; } else { Sleep(50); } } if (addGPWin <= 0) { throw string("δ´ò¿ªÌí¼Ó´úÂëWindow(16)"); } //ÊäÈë¹ÉƱ´úÂë Win32Util::keyboardNum(code, 50); // ʶ±ðµÚÒ»¸öλÖõÄÄÚÈÝ bool set_success = FALSE; for (int i = 0; i < 10; i++) { cv::Mat img_ = CaptureUtil::capture(addGPWin); cv::Mat grayImg_ = ImgUtil::grayImage(img_); cv::Mat rectImg = cv::Mat(grayImg_, cv::Rect(40, 145, 50, 26)); cv::Mat targetImg = rectImg.clone(); rectImg.copyTo(targetImg); try { list resultList = ocrFun(code, targetImg); if (resultList.size() > 0) { set_success = TRUE; break; } else { Sleep(100); } } catch (...) { Sleep(100); } } if (set_success) { Win32Util::keyboard(VK_RETURN, 50); } //¹Ø±Õ°´Å¥ Sleep(50); HWND close = FindWindowA(0, "Ìí¼Ó¹ÉƱ"); PostMessage(close, WM_CLOSE, 0, 0); return set_success; } } return FALSE; } std::list THSActionUtil::splitPlateRowArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) { //·Ö¸ôÐÐÄÚÈÝ int emptyStartRow = -1; int emptyEndRow = -1; int startf = -1; int endf = -1; std::list dataItemList; for (int i = start_row; i < end_row; i++) { //TODO 100 bool empty = ImgDivider::isRowEmpty(img, i, start_col, start_col + 50, 1, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + 50, start_col + 150, 2, _IMG_BINARY_THRESHOLD) && ImgDivider::isRowEmpty(img, i, start_col + (end_col - start_col) / 2 - 40, start_col + (end_col - start_col) / 2 + 40, 1, _IMG_BINARY_THRESHOLD); 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 && (dataItemList.size() > 0 && emptyEndRow - emptyStartRow > 3 || emptyEndRow - emptyStartRow > 0)) { //ÄÚÈÝ×ø±ê //LogUtil::debug("ÄÚÈݵĸ߶ÈΪ£º%d \n", endf - startf); //ÐÐÊý¾Ý¸ß´óÓÚ6²ÅΪÓÐЧµÄÐÐ¸ß if (endf - startf > 6) { int* dd = (int*)malloc(sizeof(int) * 4); *dd = start_col; *(dd + 1) = startf; *(dd + 2) = end_col; *(dd + 3) = endf; 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; //Êý¾Ý½áÊø } } return dataItemList; } GPCodeArea THSActionUtil::splitPlateMenuArea(cv::Mat img) { int rows = img.rows; int cols = img.cols; int r; int contentStartRow = -1; int contentEndRow = -1; for (r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, 0, cols - 5, 3, 12, 30)) { if (contentStartRow < 0) contentStartRow = r; else if (r - contentStartRow > 100) { contentEndRow = r; } } if (contentStartRow > -1 && contentEndRow > -1) { break; } } if (contentEndRow < 0) { contentEndRow = img.rows - 1; } if (contentStartRow < 0 || contentEndRow < 0) { throw string("ÆðʼÐзָô³ö´í(16)"); } //·Ö¸ôÁÐ int c = 0; int startC = -1; for (c = 0; c < cols; c++) { if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) { if (startC < 0 && c>50) { startC = c; break; } } } if (startC < 0) { throw string("ÄÚÈÝ¿ò·Ö¸ô³ö´í(17)"); } GPCodeArea area = GPCodeArea(); area.startx = 0; area.starty = contentStartRow; area.endx = startC; area.endy = contentEndRow; return area; } //·Ö¸ô°å¿éÐÐÊý¾Ý std::list THSActionUtil::splitPlateRowArea(cv::Mat img) { 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 || contentStartRow>rows - 100) { throw string("ÆðʼÐзָô³ö´í(18)"); } //·Ö¸ôÁÐ 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 string("ÄÚÈÝ¿ò·Ö¸ô³ö´í(19)"); } //ÔٴηָôÄÚÈÝ¿ò,Åųý¸ÉÈÅ int cut_index = -1; for (int r = contentStartRow + 5; r < contentStartRow + 5 + 100; r++) { if (ImgDivider::isRowFull(img, r, startC, endC, 2, 25, 30)) { cut_index = r; } else { if (cut_index > 0 && r - cut_index > 30) { contentStartRow = cut_index; break; } } } startC += 1; endC -= 1; int start_row = contentStartRow + 25; int start_col = startC; int end_row = rows; int end_col = endC; return splitPlateRowArea(img, start_row, start_col, end_row, end_col); } list splitGPCodeArea(cv::Mat img, int start_row, int start_col, int end_row, int end_col) { list resultList; //·Ö¸ôÐÐÄÚÈÝ std::list dataItemList = THSActionUtil::splitPlateRowArea(img, start_row, start_col, end_row, end_col); //·Ö¸ôÁÐ //·Ö¸ôÿһÁеÄÊý¾Ý 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); //¹ýÂË if (area.endx - area.startx > 20) { resultList.push_back(area); } } else if (rowDataList.size() > 0) { 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 (rowDataList.size() > 0) { for (std::list::iterator e = rowDataList.begin(); e != rowDataList.end(); e++) { free(*e); } } free(*ele); //±£´æÊý¾Ý 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::recognitionGPArea(cv::Mat img) { //imwrite("C:\\Users\\Administrator\\Desktop\\ocr\\test.jpg", 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 string("ÆðʼÐзָô³ö´í(20)"); } //·Ö¸ôÁÐ 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 string("ÄÚÈÝ¿ò·Ö¸ô³ö´í(21)"); } //cv::imshow("ÄÚÈÝ", cv::Mat(img, cv::Rect(startC, contentStartRow, endC - startC, rows - contentStartRow))); startC += 1; endC -= 1; std::list resultList; try { resultList = splitGPCodeArea(img, contentStartRow + 25, startC, rows, endC); } catch (...) { throw string("·Ö¸ô´úÂëÇøÓò³ö´í(22)"); } return resultList; } std::list THSActionUtil::recognitionGPCodeNum(cv::Mat img, std::list areaList, RecognitionManager* recognitionManager) { //ʶ±ðÊý×Ö std::list codeList; std::list::iterator ele; int index = 0; for (ele = areaList.begin(); ele != areaList.end(); ele++) { index++; 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)); //±£´æ·Ö¸ôµÄÊý¾Ý if (false) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append(to_string(index)); path.append(".jpg"); cv::imwrite(path, nums); } std::list list2 = splitGPCodeNum(nums); if (false) { std::list::iterator e; int ci = 0; for (e = list2.begin(); e != list2.end(); e++) { ci++; string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append(to_string(index)).append("_").append(to_string(ci)); path.append(".jpg"); cv::imwrite(path, *e); } } std::list resultList; try { 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; //cout << code << endl; codeList.push_back(codeArea); } catch (...) { } } } return codeList; } std::list THSActionUtil::recognitionGPCode(cv::Mat img, RecognitionManager* recognitionManager) { cv::Mat grayImg = ImgUtil::grayImage(img); std::list areaList = recognitionGPArea(grayImg); std::list list = recognitionGPCodeNum(grayImg, areaList, recognitionManager); std::list resultList; for (std::list::iterator ele = list.begin(); ele != list.end(); ele++) { resultList.push_back((*ele).code); } return resultList; } //·Ö¸ôL2Êý¾ÝµÄĿ¼ std::list splitL2Cate(cv::Mat img) { int cols = img.cols; int contentStartRow = -1; int contentEndRow = -1; for (int r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, 10, 200, 2, 10, 30) && ImgDivider::isRowFull(img, r, img.cols - 200, img.cols - 1, 2, 10, 30)) { if (contentStartRow < 0) { contentStartRow = r; } else { if (r - contentStartRow > 10) { //contentEndRow = r; } } } //contentStartRow > -1 && contentEndRow > -1 if (contentStartRow > -1) { break; } } //contentStartRow < 0 || contentEndRow < 0 if (contentStartRow < 0) { throw string("ÆðʼÐлò½áÊøÐзָô³ö´í(23)"); } contentEndRow = contentStartRow + 50; //·Ö¸ôÁÐ list dataColIndexs; int startf = -1; int endf = -1; int startIndex = -1; for (int i = 10; i < cols; i++) { if (startIndex == -1) { startIndex = i; } bool full = ImgDivider::isColFull(img, i, contentStartRow, contentEndRow, 2); if (full) { if (startf < 0) { startf = i; endf = i; } else { endf = i; } } else { if (startf > -1 && endf > -1) { int width = endf - startf + 1; int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = startf - 1; dataColIndexs.push_back(dd); startIndex = i; } startf = -1; endf = -1; } } if (startf > -1 && endf > -1) { int width = endf - startf + 1; int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = startf - 1; LogUtil::debug("ÁÐÊý¾Ý£º%d-%d\n", *dd, *(dd + 1)); dataColIndexs.push_back(dd); startf = -1; endf = -1; } if (cols - startIndex > 50) { int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = cols - 1; dataColIndexs.push_back(dd); } std::list areaList; list::iterator ele; for (ele = dataColIndexs.begin(); ele != dataColIndexs.end(); ele++) { int* p = *ele; int startx = *p; int endx = *(p + 1); int endRow = 0; //ÅжϽáÊøÐÐ for (int r = contentStartRow + 10; r < img.rows; r++) { bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30); if (full) { endRow = r; break; } } GPCodeArea area = GPCodeArea(); area.startx = startx; area.endx = endx; area.starty = contentStartRow; area.endy = endRow; areaList.push_back(area); free(*ele); } return areaList; } //·Ö¸ôL2½»Ò×¶ÓÁÐ std::list splitL2TradeQueue(cv::Mat img) { int cols = img.cols; int contentStartRow = -1; int contentEndRow = -1; for (int r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, 0, img.cols / 3, 2, 10, 30) && ImgDivider::isRowFull(img, r, img.cols * 2 / 3, img.cols, 2, 10, 30)) { if (contentStartRow < 0) { contentStartRow = r; break; } } } //contentStartRow < 0 || contentEndRow < 0 if (contentStartRow < 0) { throw string("ÆðʼÐлò½áÊøÐзָô³ö´í(24)"); } contentEndRow = contentStartRow + 50; //·Ö¸ôÁÐ list dataColIndexs; int startf = -1; int endf = -1; int startIndex = -1; for (int i = 10; i < cols; i++) { if (startIndex == -1) { startIndex = i; } bool full = ImgDivider::isColFull(img, i, contentStartRow, contentEndRow, 2); if (full) { if (startf < 0) { startf = i; endf = i; } else { endf = i; } } else { if (startf > -1 && endf > -1) { int width = endf - startf + 1; int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = startf - 1; dataColIndexs.push_back(dd); startIndex = i; } startf = -1; endf = -1; } } if (startf > -1 && endf > -1) { int width = endf - startf + 1; int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = startf - 1; LogUtil::debug("ÁÐÊý¾Ý£º%d-%d\n", *dd, *(dd + 1)); dataColIndexs.push_back(dd); startf = -1; endf = -1; } if (cols - startIndex > 50) { int* dd = (int*)malloc(sizeof(int) * 2); *dd = startIndex; *(dd + 1) = cols - 1; dataColIndexs.push_back(dd); } std::list areaList; list::iterator ele; for (ele = dataColIndexs.begin(); ele != dataColIndexs.end(); ele++) { int* p = *ele; int startx = *p; int endx = *(p + 1); int endRow = 0; //ÅжϽáÊøÐÐ for (int r = contentStartRow + 10; r < img.rows; r++) { bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30); if (full) { endRow = r; break; } } GPCodeArea area = GPCodeArea(); area.startx = startx; area.endx = endx; area.starty = contentStartRow; area.endy = endRow; areaList.push_back(area); free(*ele); } return areaList; } std::map THSActionUtil::getListenL2GPCodes(RecognitionManager* recognitionManager) { std::list areaList = getListenL2GPAreaAndCode(recognitionManager); std::map map; for (std::list::iterator ele = areaList.begin(); ele != areaList.end(); ele++) { GPCodeArea data = (*ele); if (map.count(data.cateIndex) == 0 && data.type == IMG_TYPE_GP) { map[data.cateIndex] = data.code; } } return map; } void THSActionUtil::betchAddGP(HWND addDialog, list codeList) { //½«´úÂëдÈëtxtÎļþ string path = "D:\\gpcode\\gp.txt"; ofstream ofs; ofs.open(path, ios::trunc); int index = 0; for (list::iterator ele = codeList.begin(); ele != codeList.end(); ++ele) { index++; ofs << *ele; if (index < codeList.size()) ofs << "\n"; } Sleep(200); addDialog = FindWindowExA(addDialog, NULL, "AfxWnd100s", "CEFBrowser"); addDialog = FindWindowExA(addDialog, NULL, "CefBrowserWindow", NULL); addDialog = FindWindowExA(addDialog, NULL, "Chrome_WidgetWin_0", NULL); addDialog = FindWindowExA(addDialog, NULL, "Chrome_RenderWidgetHostHWND", NULL); cout << "Ìí¼Ó¹ÉƱµ¯¿ò£º" << addDialog << endl; Win32Util::focus(addDialog); //ͼƬѡÔñ°´Å¥ SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x00370141); SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x00370141); Sleep(100); //ÉÏ´«Îļþ°´Å¥ SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x016000B1); SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x016000B1); HWND hwnd = HWND(0); for (int i = 0; i < 10; i++) { HWND temp = FindWindowA("#32770", "´ò¿ª"); if (temp > 0) { hwnd = temp; break; } Sleep(1000); } Win32Util::selectTexFileWin10(hwnd, "D:\\gpcode\\"); Sleep(500); Win32Util::focus(addDialog); //µã»÷Ìí¼ÓÈ«²¿°´Å¥ SendMessage(addDialog, WM_LBUTTONDOWN, 0x00000001, 0x00570139); SendMessage(addDialog, WM_LBUTTONUP, 0x00000000, 0x00570139); //¹Ø±Õµ¯¿ò HWND close = FindWindowA(0, "Ìí¼Ó¹ÉƱ"); SendMessage(close, WM_CLOSE, 0, 0); } std::list THSActionUtil::getListenL2GPAreaAndCode(RecognitionManager* recognitionManager) { //½ØÍ¼µ±Ç°ÓÐÄÄЩ¹ÉƱ´úÂë list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); HWND mainPage = 0; list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isL2Screen(str)) { cout << hwnd << endl; //»ñÈ¡³ß´ç RECT rc; GetWindowRect(hwnd, &rc); if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) { mainPage = hwnd; break; } } } if (mainPage <= 0) { throw string("L2¼àÌýδ´ò¿ª(25)"); } HWND content = FindWindowExA(mainPage, NULL, "AfxFrameOrView100s", NULL); //½ØÍ¼ cv::Mat oimg = CaptureUtil::capture(content); cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); //·Ö¸ôͼƬ std::list areaList = splitL2Cate(img); std::list fresultList; int index = 0; for (std::list::iterator ele = areaList.begin(); ele != areaList.end(); ele++) { GPCodeArea area = *ele; if (false) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append("cate_").append(to_string(index)).append(".jpg"); cv::imwrite(path, cv::Mat(img, cv::Rect(area.startx, area.starty, area.endx - area.startx + 1, area.endy - area.starty + 1))); } index++; std::list resultList = splitGPCodeArea(img, area.starty + 20, area.startx, area.endy, area.endx); for (std::list::iterator ele1 = resultList.begin(); ele1 != resultList.end(); ele1++) { GPCodeArea areaC = *ele1; if (areaC.type == IMG_TYPE_GP) { cv::Mat src = cv::Mat(img, cv::Rect(areaC.startx, areaC.starty, areaC.endx - areaC.startx + 1, areaC.endy - areaC.starty + 1)); if (false) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append(to_string(index)).append(".jpg"); cv::imwrite(path, src); } list nums = splitGPCodeNum(src); if (!recognitionManager) { recognitionManager = new RecognitionManager(); } try { list rresult = recognitionManager->recognitionGPCode(nums); string num; for (std::list::iterator e = rresult.begin(); e != rresult.end(); e++) { num.append(to_string(*e)); } (*ele1).code = num; (*ele1).cateIndex = index - 1; fresultList.push_back(*ele1); } catch (...) { } } } } return fresultList; } // »ñÈ¡²Ù×÷ÇøÓò std::list> THSActionUtil::getListenL2GPAreaActionBar() { //½ØÍ¼µ±Ç°ÓÐÄÄЩ¹ÉƱ´úÂë list wlist = Win32Util::searchWindow("ͬ»¨Ë³("); HWND mainPage = 0; list::iterator ele; for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isL2Screen(str)) { cout << hwnd << endl; //»ñÈ¡³ß´ç RECT rc; GetWindowRect(hwnd, &rc); if (rc.right - rc.left > 200 && rc.bottom - rc.top > 200) { mainPage = hwnd; break; } } } if (mainPage <= 0) { throw string("L2¼àÌýδ´ò¿ª(25)"); } HWND content = FindWindowExA(mainPage, NULL, "AfxFrameOrView100s", NULL); //½ØÍ¼ cv::Mat oimg = CaptureUtil::capture(content); cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); //·Ö¸ôͼƬ std::list areaList = splitL2Cate(img); std::list fresultList; int index = 0; for (std::list::iterator ele = areaList.begin(); ele != areaList.end(); ele++) { GPCodeArea area = *ele; int startRow = -1; int endRow = -1; for (int r = area.endy; r >= area.starty; r--) { if (!ImgDivider::isRowEmpty(img, r, area.startx, (area.endx + area.startx) / 2)) { if (startRow < 0) { startRow = r; endRow = r; } endRow = r; } else { if (startRow > 0 && endRow > 0) { if (startRow - endRow > 10) { GPCodeArea farea; farea.startx = area.startx; farea.endx = area.endx; farea.starty = endRow; farea.endy = startRow; fresultList.push_back(farea); // TODO ÔÝʱ´ò¿ª if (false) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append("cate_").append(to_string(index)).append(".jpg"); cv::imwrite(path, cv::Mat(img, cv::Rect(farea.startx, farea.starty, farea.endx - farea.startx + 1, farea.endy - farea.starty + 1))); } break; } else { startRow = endRow; } } } } index++; } index = 0; std::list> ffresultList; //Çиîÿһ¿éµÄ²Ëµ¥ for (std::list::iterator ele = fresultList.begin(); ele != fresultList.end(); ele++) { GPCodeArea area = *ele; int start_col = area.startx; list tempList; for (int c = area.startx; c <= area.endx; c++) { //ͨ¹ýµÚÒ»ÐеĴ¿ºÚµã×ö·Ö¸ô if (img.ptr(area.starty)[c] <= 5) { if (c - start_col > 10) { //»ñÈ¡µ½·Ö¸ôµã GPCodeArea child; child.startx = start_col; child.endx = c - 1; child.starty = area.starty; child.endy = area.endy; tempList.push_back(child); start_col = c; if (false) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; path.append("cate_").append(to_string(index)).append("_").append(to_string(c)).append(".jpg"); cv::imwrite(path, cv::Mat(img, cv::Rect(child.startx, child.starty, child.endx - child.startx + 1, child.endy - child.starty + 1))); } } } } index++; ffresultList.push_back(tempList); } return ffresultList; } cv::Mat getTHSTimeCapture() { HWND hwnd = THSActionUtil::getMainWin(); if (hwnd <= 0) { throw string("δÕÒµ½Í¬»¨Ë³Ö÷Ò³"); } HWND bar = FindWindowEx(hwnd, NULL, TEXT("msctls_statusbar32"), NULL); int count = 0; int lastTop = 0; HWND lastBar = 0; do { count++; RECT rect; GetWindowRect(bar, &rect); if (rect.top > lastTop) { lastTop = rect.top; lastBar = bar; } bar = FindWindowEx(hwnd, bar, TEXT("msctls_statusbar32"), NULL); if (count > 10 || bar <= 0) { bar = 0; break; } } while (TRUE); if (lastBar <= 0) { throw string("δÕÒµ½Í¬»¨Ë³Ê±¼ä´°¿Ú"); } if (lastBar > 0) { cv::Mat mat = CaptureUtil::capture(lastBar); if (mat.cols > 100) { mat = cv::Mat(mat, cv::Rect(mat.cols - 80, 0, 80, mat.rows)); mat = ImgUtil::grayImage(mat); return mat; } } throw string("δÕÒµ½Í¬»¨Ë³Ê±¼ä½ØÍ¼»ñȡʧ°Ü"); } bool THSActionUtil::thsIsDead() { cv::Mat oldMat = getTHSTimeCapture(); Sleep(1200); cv::Mat newMat = getTHSTimeCapture(); if (ImgUtil::isImgSame(oldMat, newMat)) { return TRUE; } return FALSE; } void THSActionUtil::updateMainSite() { //»ñÈ¡Ö÷Ò³ HWND main = getMainWin(); if (main <= 0) { throw string("ͬ»¨Ë³Ö÷ҳδ´ò¿ª"); } Win32Util::showWindow(main); //»ñÈ¡toolbar HWND win = NULL; do { win = FindWindowExA(main, win, "AfxControlBar100s", ""); if (win <= 0) { break; } RECT rect; GetWindowRect(win, &rect); if (rect.bottom - rect.top >= 40 && rect.bottom - rect.top <= 60) { break; } } while (true); if (win <= 0) { throw string("²âËÙ°´Å¥Î´ÕÒµ½"); } win = FindWindowExA(win, NULL, "#32770", ""); if (win <= 0) { throw string("²âËÙ°´Å¥Î´ÕÒµ½"); } HWND btn = GetDlgItem(win, 0x00007A17); if (btn <= 0) { throw string("²âËÙ°´Å¥Î´ÕÒµ½"); } //µã»÷°´Å¥ Win32Util::visualClick(btn, MAKELONG(3, 3)); for (int i = 0; i < 10; i++) { list _list = Win32Util::searchWindow("Ñ¡Ôñ×îÓÅÐÐÇéÖ÷Õ¾"); if (_list.size() > 0) { HWND win = *(_list.begin()); HWND loginBtn = 0; for (int j = 0; j < 10; j++) { loginBtn = FindWindowExA(win, NULL, "Button", "µÇ ¼"); if (loginBtn > 0) { // ÓÅÏÈÑ¡ÔñµçÐÅ HWND dianXin = FindWindowExA(win, NULL, "Button", "ÖйúµçÐÅ"); if (dianXin > 0) { Win32Util::visualClick(dianXin, MAKELONG(2, 2)); Sleep(100); } //µã»÷µÇ¼°´Å¥ Win32Util::visualClick(loginBtn, MAKELONG(2, 2)); break; } Sleep(200); } } Sleep(400); } } bool THSActionUtil::screenL2Money(HWND l2_hwnd, string money, MatOcrFun ocrFun) { if (!Win32Util::isWindowShow(l2_hwnd)) { throw string("L2´°¿Ú¾ä±úδչʾ"); } HWND win = FindWindowExA(l2_hwnd, NULL, "#32770", NULL); win = FindWindowExA(win, NULL, "stock_trade_info_dlg", "stock_trade_info"); if (win <= 0) { throw string("δÕÒµ½L2´°¿ÚµÄÄÚÈÝ"); } RECT rect; Win32Util::getWindowRect(win, &rect); //ÖØÊÔ5´Î for (int i = 0; i < 5; i++) { Win32Util::postMessage(win, WM_LBUTTONDOWN, MAKELONG(1, 0), MAKELONG(0x007E, 0x000A)); Win32Util::sendMessage(win, WM_PARENTNOTIFY, MAKELONG(1, 0), MAKELONG(0x2FBC, 0x000B)); Win32Util::postMessage(win, WM_LBUTTONUP, MAKELONG(1, 0), MAKELONG(0x007E, 0x000A)); Sleep(10); HWND child = FindWindowExA(win, NULL, "EditWnd", NULL); if (child > 0) { RECT editRect; Win32Util::getWindowRect(child, &editRect); //Win32Util::focus(child); Win32Util::input_num(child, money); Win32Util::focus(child); Sleep(50); cv::Mat mat = CaptureUtil::capture(win); cv::Mat grayImg = ImgUtil::grayImage(mat); cv::Mat rectMat = cv::Mat(grayImg, cv::Rect(editRect.left - rect.left, editRect.top - rect.top, editRect.right - editRect.left, editRect.bottom - editRect.top)); //ImgUtil::markMat(grayImg, cv::Rect(editRect.left - rect.left, editRect.top - rect.top, editRect.right - editRect.left, editRect.bottom - editRect.top),255,1); cv::Mat targetMat = rectMat.clone(); rectMat.copyTo(targetMat); list results = ocrFun(".", targetMat); string newmoney = ""; for (list::iterator ele = results.begin(); ele != results.end(); ++ele) { newmoney.append((*ele).text); } if (newmoney == money) { return TRUE; } } } return FALSE; } void THSActionUtil::clearPlateCodes(HWND hwnd) { HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL); for (int i = 0; i < 15; i++) { //½ØÍ¼Ê¶±ðÊÇ·ñ»¹ÓдúÂë cv::Mat oimg = CaptureUtil::capture(content); if (oimg.cols <= 0 || oimg.rows <= 0) { throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(26)"); } cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); std::list areaList = recognitionGPArea(img); cout << "Êý¾ÝÊýÁ¿:" << areaList.size() << endl; if (areaList.size() <= 2) { //Ö´ÐÐ×îºóÒ»´Îɾ³ý Win32Util::focus(hwnd); Win32Util::keyboard(VK_DELETE, 30); break; } //Ö´ÐÐɾ³ýʼþ for (int j = 0; j < 20; j++) { Win32Util::focus(hwnd); Win32Util::keyboard(VK_DELETE, 30); } } } RECT THSActionUtil::getPlateAddCodeBtnPostion(HWND win) { //»ñÈ¡Ìí¼Ó°´Å¥Î»Öà HWND content = FindWindowExA(win, NULL, "AfxFrameOrView100s", NULL); RECT contentRect; GetWindowRect(content, &contentRect); cv::Mat oimg = CaptureUtil::capture(content); if (oimg.cols <= 0 || oimg.rows <= 0) { throw string("°å¿é½ØÆÁÄÚÈÝΪ¿Õ(27)"); } cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); std::list areaList = recognitionGPArea(img); for (std::list::iterator ele = areaList.begin(); ele != areaList.end(); ele++) { cout << "Ìí¼Ó°´Å¥Î»Öãº" << (*ele).startx << "," << (*ele).starty << "," << (*ele).endx << "," << (*ele).endy << endl; if ((*ele).type == IMG_TYPE_ADD) { RECT rect; rect.left = contentRect.left + (*ele).startx; rect.right = contentRect.left + (*ele).endx; rect.top = contentRect.top + (*ele).starty; rect.bottom = contentRect.top + (*ele).endy; return rect; } } throw string("ÉÐδÕÒµ½Ìí¼Ó°´Å¥(28)"); } bool THSActionUtil::setListenL2GP(int p, string code, RecognitionManager* recognitionManager) { std::list resultList = getListenL2GPAreaAndCode(recognitionManager); HWND content = FindWindowExA(getL2Win(), NULL, "AfxFrameOrView100s", NULL); int index = 0; for (std::list::iterator ele = resultList.begin(); ele != resultList.end(); ele++) { GPCodeArea result = *ele; if (result.code == code) { //µã»÷ RECT rc; GetWindowRect(content, &rc); int x = rc.left + result.startx + 5; int y = rc.top + result.starty + 5; Win32Util::focus(content); //¼ä¸ô1200msÔÙµã»÷ Win32Util::click(x, y, 1200); return true; } } return false; } GPCodeArea THSActionUtil::recognitionPlateContentArea(cv::Mat grayImg) { int rows = grayImg.rows; int cols = grayImg.cols; int r; int contentStartRow = -1; for (r = 5; r < grayImg.rows; r++) { if (ImgDivider::isRowFull(grayImg, r, cols - 100, cols - 5, 2)) { contentStartRow = r; } if (contentStartRow > -1) { break; } } if (contentStartRow < 0) { throw string("ÆðʼÐзָô³ö´í(29)"); } //·Ö¸ôÁÐ int c = 0; int startC = -1; int endC = -1; for (c = 50; c < cols; c++) { if (ImgDivider::isColFull(grayImg, 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 string("ÄÚÈÝ¿ò·Ö¸ô³ö´í(30)"); } GPCodeArea area = GPCodeArea(); area.startx = startC; area.starty = contentStartRow; area.endx = endC; area.endy = rows - 1; return area; } void THSActionUtil::addTargetCodes(list codeList, RecognitionManager* recognitionManager) { //»ñȡͬ»¨Ë³Ö÷Ò³ HWND win = getSecondWindow(); Win32Util::focus(win); //¿ì½Ý¼ü´ò¿ª°å¿é Win32Util::keyboardNum("51", 200); Win32Util::keyboard(13, 10); Sleep(1000); //ɾ³ý°å¿éÄÚµÄËùÓÐÄÚÈÝ clearPlateCodes(win); //»ñÈ¡Ìí¼Ó°´Å¥Î»Öà RECT codeArea = getPlateAddCodeBtnPostion(win); //µã»÷Ìí¼Ó°´Å¥ Win32Util::click(codeArea.left + 5, codeArea.top + 3); //ÅúÁ¿Ìí¼Ó´úÂë HWND addDLG; for (int i = 0; i < 10; i++) { addDLG = FindWindowA("#32770", "Ìí¼Ó¹ÉƱ"); if (addDLG > 0) { break; } Sleep(1000); } if (addDLG <= 0) { throw string("Ìí¼Ó¹ÉƱ´°¿Ú´ò¿ªÊ§°Ü(31)"); } if (codeList.size() >= 100) { betchAddGP(addDLG, codeList); } else { //Öð¸öÌí¼Ó for (list::iterator ele = codeList.begin(); ele != codeList.end(); ++ele) { string code = *ele; Win32Util::focus(addDLG); for (int i = 0; i < 6; i++) { Win32Util::keyboard(VK_BACK, 10); } Win32Util::focus(addDLG); Win32Util::keyboardNum(code, 1000); Win32Util::focus(addDLG); Win32Util::keyboard(VK_RETURN, 1500); } Sleep(100); SendMessage(addDLG, WM_CLOSE, 0, 0); } //TODO УÑé´úÂëµÄÕýÈ·ÐÔ }