From 8b06b1cbf112d55307ea8a6efe711db4e7506d89 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 07 三月 2023 10:13:47 +0800 Subject: [PATCH] 'GPU内核方法优化' --- ConsoleApplication/THSActionUtil.cpp | 337 +++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 248 insertions(+), 89 deletions(-) diff --git a/ConsoleApplication/THSActionUtil.cpp b/ConsoleApplication/THSActionUtil.cpp index e6060ae..6a256aa 100644 --- a/ConsoleApplication/THSActionUtil.cpp +++ b/ConsoleApplication/THSActionUtil.cpp @@ -179,7 +179,7 @@ HWND THSActionUtil::getThirdWindow() { list<HWND> wlist = Win32Util::searchWindow("同花顺("); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isThirdScreen(str)) @@ -194,7 +194,7 @@ HWND THSActionUtil::getIndustryWindow() { list<HWND> wlist = Win32Util::searchWindow("同花顺("); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (str.find("行业") != string::npos) @@ -208,7 +208,7 @@ HWND THSActionUtil::getTradeDelegateWindow() { list<HWND> wlist = Win32Util::searchWindow("专业版下单"); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; return hwnd; } @@ -230,11 +230,38 @@ 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<HWND> wlist = Win32Util::searchWindow("短线精灵"); + list<HWND>::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<HWND> wlist = Win32Util::searchWindow("网上股票交易系统"); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; return hwnd; } @@ -248,7 +275,7 @@ HWND THSActionUtil::getSecondWindow() { list<HWND> wlist = Win32Util::searchWindow("同花顺("); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isSecondScreen(str)) @@ -262,7 +289,7 @@ HWND THSActionUtil::getMainWin() { list<HWND> wlist = Win32Util::searchWindow("同花顺("); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); @@ -278,7 +305,7 @@ HWND THSActionUtil::getL2Win() { list<HWND> wlist = Win32Util::searchWindow("同花顺("); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); @@ -289,7 +316,7 @@ HWND temp = NULL; int content_count = 0; while (TRUE) { - temp = FindWindowExA(mainWin, temp, "AfxWnd100s", NULL); + temp = FindWindowExA(mainWin, temp, "AfxWnd100s", NULL); if (temp == NULL || temp <= 0) { break; } @@ -297,7 +324,7 @@ content_count++; } } - if (content_count >= 16) + if (content_count >= L2_CHENNL_COUNT*2) { return hwnd; } @@ -314,7 +341,7 @@ bool fp = false; bool fp2 = false; bool zp = false; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isSecondScreen(str)) @@ -391,8 +418,8 @@ 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); + 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<uchar>(r)[c]; @@ -405,7 +432,7 @@ for (list<OCRResult>::iterator ele = results.begin(); ele != results.end(); ++ele) { OCRResult result = *ele; - int x = area.startx + result.rect.left; + 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,否则就未选中 @@ -421,7 +448,7 @@ //ImgUtil::markMat(img,ocrRect,255,1); Win32Util::showWindow(content); Win32Util::focus(content); - + // 未被选中 if (selectColorCount < 20) { @@ -431,7 +458,7 @@ } } } - + } catch (string st) { @@ -460,7 +487,7 @@ HWND mainPage = getMainWin(); list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isSecondScreen(str)) @@ -520,7 +547,7 @@ HWND mainPage = 0; list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isThirdScreen(str)) @@ -591,12 +618,29 @@ 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<std::string> codeList, RecognitionManager* recognitionManager) { +bool THSActionUtil::setGP(std::string quickCode, list<std::string> codeList, RecognitionManager* recognitionManager, MatOcrFun ocrFun) { list<std::string> tempCodeList; - for (std::list<string>::iterator e = codeList.begin();e != codeList.end();e++) { + for (std::list<string>::iterator e = codeList.begin(); e != codeList.end(); e++) { tempCodeList.push_back(*e); } @@ -641,11 +685,11 @@ std::list<GPCodeArea> delList; std::list<GPCodeArea> resultList = recognitionGPCodeNum(img, areaList, recognitionManager); - for (std::list<GPCodeArea>::reverse_iterator ele = resultList.rbegin();ele != resultList.rend();++ele) { + for (std::list<GPCodeArea>::reverse_iterator ele = resultList.rbegin(); ele != resultList.rend(); ++ele) { bool contains = false; std::list<string>::iterator e; - for (e = tempCodeList.begin();e != tempCodeList.end();e++) { + for (e = tempCodeList.begin(); e != tempCodeList.end(); e++) { if (*e == (*ele).code) { contains = true; break; @@ -669,7 +713,7 @@ Win32Util::mouseMove(rect.left + 10, rect.top + 5, 1); Win32Util::click(10); - for (std::list<GPCodeArea>::iterator ele = delList.begin();ele != delList.end();++ele) { + for (std::list<GPCodeArea>::iterator ele = delList.begin(); ele != delList.end(); ++ele) { RECT rect; GetWindowRect(content, &rect); Win32Util::focus(sw); @@ -685,10 +729,10 @@ //----增加 //截图,识别出增加按钮位置,点击增加,输入内容 - for (std::list<string>::iterator ele = tempCodeList.begin();ele != tempCodeList.end();++ele) { + for (std::list<string>::iterator ele = tempCodeList.begin(); ele != tempCodeList.end(); ++ele) { if ((*ele).length() > 0) { - addGP(*ele); + addGP(*ele, ocrFun); Sleep(100); } } @@ -700,10 +744,10 @@ img = ImgUtil::grayImage(oimg); areaList = recognitionGPArea(img); resultList = recognitionGPCodeNum(img, areaList, recognitionManager); - for (std::list<GPCodeArea>::iterator ele = resultList.begin();ele != resultList.end();++ele) { + for (std::list<GPCodeArea>::iterator ele = resultList.begin(); ele != resultList.end(); ++ele) { bool contains = false; std::list<string>::iterator e; - for (e = codeList.begin();e != codeList.end();e++) { + for (e = codeList.begin(); e != codeList.end(); e++) { if (*e == (*ele).code) { contains = true; break; @@ -722,7 +766,8 @@ } -void THSActionUtil::addGP(string code) { +bool THSActionUtil::addGP(string code, MatOcrFun ocrFun) { + clock_t start_time = clock(); //打开副屏 HWND sw = getSecondWindow(); if (sw <= 0) @@ -742,7 +787,9 @@ throw string("板块截屏内容为空(15)"); } std::list<GPCodeArea> areaList = recognitionGPArea(img); - for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();++ele) { + + + for (std::list<GPCodeArea>::iterator ele = areaList.begin(); ele != areaList.end(); ++ele) { GPCodeArea codeArea = *ele; if (codeArea.type == IMG_TYPE_ADD) { Win32Util::focus(sw); @@ -750,21 +797,79 @@ 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; - Win32Util::click(x, y, 50); - Sleep(10); - Win32Util::click(x, y, 50); - Sleep(10); - Win32Util::click(x, y, 50); + //截图 + HWND addGPWin = 0; + for (int i = 0; i < 6; i++) { + Win32Util::click(x, y, 50); + for (int j = 0; j < 20; j++) { + list<HWND> winList = Win32Util::searchWindow("添加股票"); + if (winList.size() > 0) { + for (list<HWND>::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, 1000); - Win32Util::keyboard(VK_RETURN, 1500); + 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<OCRResult> 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(100); + Sleep(50); HWND close = FindWindowA(0, "添加股票"); PostMessage(close, WM_CLOSE, 0, 0); - break; + return set_success; } } + + return FALSE; } @@ -777,7 +882,7 @@ int endf = -1; std::list<int*> dataItemList; - for (int i = start_row;i < end_row;i++) { + 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) { @@ -840,7 +945,7 @@ int r; int contentStartRow = -1; int contentEndRow = -1; - for (r = 5;r < img.rows;r++) { + for (r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, 0, cols - 5, 3, 12, 30)) { if (contentStartRow < 0) @@ -868,7 +973,7 @@ //分隔列 int c = 0; int startC = -1; - for (c = 0;c < cols;c++) { + for (c = 0; c < cols; c++) { if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) { if (startC < 0 && c>50) { startC = c; @@ -895,7 +1000,7 @@ int cols = img.cols; int r; int contentStartRow = -1; - for (r = 5;r < img.rows;r++) { + for (r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, cols - 100, cols - 5, 2)) { contentStartRow = r; } @@ -913,7 +1018,7 @@ int c = 0; int startC = -1; int endC = -1; - for (c = 50;c < cols;c++) { + for (c = 50; c < cols; c++) { if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) { if (startC < 0) { startC = c; @@ -936,7 +1041,7 @@ //再次分隔内容框,排除干扰 int cut_index = -1; - for (int r = contentStartRow + 5;r < contentStartRow + 5 + 100;r++) { + for (int r = contentStartRow + 5; r < contentStartRow + 5 + 100; r++) { if (ImgDivider::isRowFull(img, r, startC, endC, 2, 25, 30)) { cut_index = r; @@ -983,7 +1088,7 @@ //分隔每一列的数据 std::list<int*>::iterator ele; int index = 0; - for (ele = dataItemList.begin(); ele != dataItemList.end();ele++) { + for (ele = dataItemList.begin(); ele != dataItemList.end(); ele++) { index++; int startRow = *(*ele + 1); int startCol = *(*ele + 0); @@ -1005,7 +1110,7 @@ std::list<int*> rowDataList; - for (int i = startCol;i <= endCol;i++) { + for (int i = startCol; i <= endCol; i++) { bool empty = ImgDivider::isColEmpty(img, i, startRow, endRow, 64); if (empty) { if (emptyStart < 0) { @@ -1072,7 +1177,7 @@ resultList.push_back(area); } } - else if(rowDataList.size()>0) { + else if (rowDataList.size() > 0) { std::list<int*>::iterator ele = rowDataList.begin(); GPCodeArea area = GPCodeArea(); area.type = IMG_TYPE_ADD; @@ -1087,7 +1192,7 @@ //内存释放 if (rowDataList.size() > 0) { - for (std::list<int*>::iterator e = rowDataList.begin();e != rowDataList.end();e++) { + for (std::list<int*>::iterator e = rowDataList.begin(); e != rowDataList.end(); e++) { free(*e); } } @@ -1099,7 +1204,7 @@ { std::list<int*>::iterator ele1; int cc = 0; - for (ele1 = rowDataList.begin(); ele1 != rowDataList.end();ele1++) { + for (ele1 = rowDataList.begin(); ele1 != rowDataList.end(); ele1++) { if (*ele1) { int startCRow = *(*ele1 + 1); @@ -1133,7 +1238,7 @@ int cols = img.cols; int r; int contentStartRow = -1; - for (r = 5;r < img.rows;r++) { + for (r = 5; r < img.rows; r++) { if (ImgDivider::isRowFull(img, r, cols - 100, cols - 5, 2)) { contentStartRow = r; } @@ -1151,7 +1256,7 @@ int c = 0; int startC = -1; int endC = -1; - for (c = 50;c < cols;c++) { + for (c = 50; c < cols; c++) { if (ImgDivider::isColFull(img, c, contentStartRow + 5, contentStartRow + 100, 2)) { if (startC < 0) { startC = c; @@ -1193,7 +1298,7 @@ std::list<GPCodeArea>::iterator ele; int index = 0; - for (ele = areaList.begin();ele != areaList.end();ele++) { + for (ele = areaList.begin(); ele != areaList.end(); ele++) { index++; GPCodeArea codeArea = *ele; if (codeArea.type == IMG_TYPE_GP) { @@ -1212,7 +1317,7 @@ if (false) { std::list<cv::Mat>::iterator e; int ci = 0; - for (e = list2.begin();e != list2.end();e++) { + 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)); @@ -1235,10 +1340,10 @@ codeList.push_back(codeArea); } catch (...) { - - + + } - + } } @@ -1251,7 +1356,7 @@ std::list<GPCodeArea> areaList = recognitionGPArea(grayImg); std::list<GPCodeArea> list = recognitionGPCodeNum(grayImg, areaList, recognitionManager); std::list<string> resultList; - for (std::list<GPCodeArea>::iterator ele = list.begin();ele != list.end();ele++) { + for (std::list<GPCodeArea>::iterator ele = list.begin(); ele != list.end(); ele++) { resultList.push_back((*ele).code); } return resultList; @@ -1265,7 +1370,7 @@ int cols = img.cols; int contentStartRow = -1; int contentEndRow = -1; - for (int r = 5;r < img.rows;r++) { + 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) { @@ -1296,7 +1401,7 @@ int startf = -1; int endf = -1; int startIndex = -1; - for (int i = 10;i < cols;i++) { + for (int i = 10; i < cols; i++) { if (startIndex == -1) { startIndex = i; } @@ -1358,14 +1463,14 @@ std::list<GPCodeArea> areaList; list<int*>::iterator ele; - for (ele = dataColIndexs.begin();ele != dataColIndexs.end();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++) { + for (int r = contentStartRow + 10; r < img.rows; r++) { bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30); if (full) { endRow = r; @@ -1393,7 +1498,7 @@ int cols = img.cols; int contentStartRow = -1; int contentEndRow = -1; - for (int r = 5;r < img.rows;r++) { + 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) { @@ -1414,7 +1519,7 @@ int startf = -1; int endf = -1; int startIndex = -1; - for (int i = 10;i < cols;i++) { + for (int i = 10; i < cols; i++) { if (startIndex == -1) { startIndex = i; } @@ -1476,14 +1581,14 @@ std::list<GPCodeArea> areaList; list<int*>::iterator ele; - for (ele = dataColIndexs.begin();ele != dataColIndexs.end();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++) { + for (int r = contentStartRow + 10; r < img.rows; r++) { bool full = ImgDivider::isRowFull(img, r, startx, endx, 2, 10, 30); if (full) { endRow = r; @@ -1509,7 +1614,7 @@ std::map<int, string> THSActionUtil::getListenL2GPCodes(RecognitionManager* recognitionManager) { std::list<GPCodeArea> areaList = getListenL2GPAreaAndCode(recognitionManager); std::map<int, string> map; - for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) { + for (std::list<GPCodeArea>::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; @@ -1527,7 +1632,7 @@ ofstream ofs; ofs.open(path, ios::trunc); int index = 0; - for (list<string>::iterator ele = codeList.begin();ele != codeList.end();++ele) + for (list<string>::iterator ele = codeList.begin(); ele != codeList.end(); ++ele) { index++; ofs << *ele; @@ -1554,7 +1659,7 @@ HWND hwnd = HWND(0); - for (int i = 0;i < 10;i++) + for (int i = 0; i < 10; i++) { HWND temp = FindWindowA("#32770", "打开"); if (temp > 0) { @@ -1581,7 +1686,7 @@ list<HWND> wlist = Win32Util::searchWindow("同花顺("); HWND mainPage = 0; list<HWND>::iterator ele; - for (ele = wlist.begin();ele != wlist.end();ele++) { + for (ele = wlist.begin(); ele != wlist.end(); ele++) { HWND hwnd = *ele; string str = Win32Util::getWindowName(hwnd); if (isL2Screen(str)) { @@ -1611,7 +1716,7 @@ std::list<GPCodeArea> fresultList; int index = 0; - for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) { + for (std::list<GPCodeArea>::iterator ele = areaList.begin(); ele != areaList.end(); ele++) { GPCodeArea area = *ele; if (false) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\"; @@ -1624,7 +1729,7 @@ std::list<GPCodeArea> resultList = splitGPCodeArea(img, area.starty + 20, area.startx, area.endy, area.endx); - for (std::list<GPCodeArea>::iterator ele1 = resultList.begin();ele1 != resultList.end();ele1++) { + for (std::list<GPCodeArea>::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)); @@ -1650,7 +1755,7 @@ fresultList.push_back(*ele1); } catch (...) { - + } } } @@ -1699,12 +1804,12 @@ int index = 0; for (std::list<GPCodeArea>::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 (!ImgDivider::isRowEmpty(img, r, area.startx, (area.endx + area.startx) / 2)) { if (startRow < 0) { startRow = r; @@ -1754,7 +1859,7 @@ //获取到分隔点 GPCodeArea child; child.startx = start_col; - child.endx = c -1; + child.endx = c - 1; child.starty = area.starty; child.endy = area.endy; tempList.push_back(child); @@ -1772,7 +1877,7 @@ index++; ffresultList.push_back(tempList); } - + return ffresultList; @@ -1869,16 +1974,22 @@ } //点击按钮 - Win32Util::visualClick(btn, MAKELONG(3,3)); - for (int i = 0;i < 10;i++) { + Win32Util::visualClick(btn, MAKELONG(3, 3)); + for (int i = 0; i < 10; i++) { list<HWND> _list = Win32Util::searchWindow("选择最优行情主站"); if (_list.size() > 0) { HWND win = *(_list.begin()); HWND loginBtn = 0; - for (int j = 0;j < 10;j++) + for (int j = 0; j < 10; j++) { - loginBtn = FindWindowExA(win, NULL, "Button", "登 录"); + 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; @@ -1892,10 +2003,58 @@ } +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<OCRResult> results = ocrFun(".", targetMat); + string newmoney = ""; + for (list<OCRResult>::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++) + for (int i = 0; i < 15; i++) { //截图识别是否还有代码 cv::Mat oimg = CaptureUtil::capture(content); @@ -1914,7 +2073,7 @@ break; } //执行删除事件 - for (int j = 0;j < 20;j++) { + for (int j = 0; j < 20; j++) { Win32Util::focus(hwnd); Win32Util::keyboard(VK_DELETE, 30); } @@ -1937,7 +2096,7 @@ cv::Mat img = ImgUtil::grayImage(oimg); oimg.release(); std::list<GPCodeArea> areaList = recognitionGPArea(img); - for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) { + for (std::list<GPCodeArea>::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; @@ -1960,7 +2119,7 @@ HWND content = FindWindowExA(getL2Win(), NULL, "AfxFrameOrView100s", NULL); int index = 0; - for (std::list<GPCodeArea>::iterator ele = resultList.begin();ele != resultList.end();ele++) { + for (std::list<GPCodeArea>::iterator ele = resultList.begin(); ele != resultList.end(); ele++) { GPCodeArea result = *ele; if (result.code == code) { //点击 @@ -1984,7 +2143,7 @@ int cols = grayImg.cols; int r; int contentStartRow = -1; - for (r = 5;r < grayImg.rows;r++) { + for (r = 5; r < grayImg.rows; r++) { if (ImgDivider::isRowFull(grayImg, r, cols - 100, cols - 5, 2)) { contentStartRow = r; } @@ -2002,7 +2161,7 @@ int c = 0; int startC = -1; int endC = -1; - for (c = 50;c < cols;c++) { + for (c = 50; c < cols; c++) { if (ImgDivider::isColFull(grayImg, c, contentStartRow + 5, contentStartRow + 100, 2)) { if (startC < 0) { startC = c; @@ -2050,7 +2209,7 @@ //批量添加代码 HWND addDLG; - for (int i = 0;i < 10;i++) { + for (int i = 0; i < 10; i++) { addDLG = FindWindowA("#32770", "添加股票"); if (addDLG > 0) { break; @@ -2066,10 +2225,10 @@ } else { //逐个添加 - for (list<std::string>::iterator ele = codeList.begin();ele != codeList.end();++ele) { + for (list<std::string>::iterator ele = codeList.begin(); ele != codeList.end(); ++ele) { string code = *ele; Win32Util::focus(addDLG); - for (int i = 0;i < 6;i++) + for (int i = 0; i < 6; i++) { Win32Util::keyboard(VK_BACK, 10); } -- Gitblit v1.8.0