admin
2023-02-09 125db633619a0b4c7bd1d498ea2bf1cefa4f73d3
ConsoleApplication/THSActionUtil.cpp
@@ -56,6 +56,125 @@
   return   nums;
}
//获取整个L2的代码选择区域
list<GPCodeArea> 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<int*> 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<GPCodeArea> areaList;
   list<int*>::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<HWND> wlist = Win32Util::searchWindow("同花顺(");
@@ -165,12 +284,29 @@
      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 >= 16)
         {
         return hwnd;
         }
      }
   }
   return 0;
}
bool THSActionUtil::checkEnv() {
   list<HWND> wlist = Win32Util::searchWindow("同花顺(");
@@ -228,6 +364,86 @@
         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<GPCodeArea>  areaList = splitL2WholeCateArea(img);
   int index = 0;
   for (list<GPCodeArea>::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<uchar>(r)[c];
            }
         }
         mat.data = datas;
         list<OCRResult> results = matOcrFun("首板关注", mat);
         free(datas);
         //获取点击位置
         for (list<OCRResult>::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<uchar>(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() {
@@ -1042,6 +1258,8 @@
}
//分隔L2数据的目录
std::list<GPCodeArea> splitL2Cate(cv::Mat img) {
   int cols = img.cols;
@@ -1168,7 +1386,6 @@
   }
   return areaList;
}
//分隔L2交易队列