From 30f434d78b58e3a4198cf5ba5a9e5a0ce1cd5292 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 07 七月 2022 14:32:36 +0800
Subject: [PATCH] 'bug修复'

---
 ConsoleApplication/THSActionUtil.cpp |  124 +++++++++++++++++++++++++++++-----------
 1 files changed, 89 insertions(+), 35 deletions(-)

diff --git a/ConsoleApplication/THSActionUtil.cpp b/ConsoleApplication/THSActionUtil.cpp
index 331e092..017e87c 100644
--- a/ConsoleApplication/THSActionUtil.cpp
+++ b/ConsoleApplication/THSActionUtil.cpp
@@ -20,6 +20,29 @@
 	return	str.find("同花顺(") != string::npos && str.find("副屏") != string::npos;
 }
 
+std::list<cv::Mat> splitGPCodeNum(cv::Mat img) {
+	std::list<cv::Mat> 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("代码数字分隔出错");
+	}
+	return	nums;
+}
+
+
+
 //获取副屏
 HWND getSecondWindow() {
 	list<HWND> wlist = Win32Util::searchWindow("同花顺(");
@@ -116,7 +139,7 @@
 
 	if (!open) {
 		if (mainPage <= 0) {
-			throw("未找到首页");
+			throw string("未找到首页");
 		}
 
 		Win32Util::focus(mainPage);
@@ -156,13 +179,17 @@
 		sw = getSecondWindow();
 	}
 	if (sw <= 0) {
-		throw("未打开副屏");
+		throw string("未打开副屏");
 	}
-	Win32Util::focus(sw);
-	//打开板块
-	Win32Util::keyboardNum(quickCode, 100);
-	Win32Util::keyboard(VK_RETURN, 200);
 
+	//打开板块
+	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);
 	//设置板块中的股票
 
@@ -208,7 +235,6 @@
 	//获取内容板块坐标
 
 
-	RECT rect;
 	GetWindowRect(sw, &rect);
 	Win32Util::mouseMove(rect.left + 10, rect.top + 5, 1);
 	Win32Util::click(10);
@@ -230,13 +256,16 @@
 	//----增加
 	//截图,识别出增加按钮位置,点击增加,输入内容
 	for (std::list<string>::iterator ele = tempCodeList.begin();ele != tempCodeList.end();++ele) {
-		addGP(*ele);
-		Sleep(100);
+		if ((*ele).length() > 0)
+		{
+			addGP(*ele);
+			Sleep(100);
+		}
 	}
 
 	oimg = CaptureUtil::capture(content);
 	if (oimg.cols <= 0 || oimg.rows <= 0) {
-		throw("板块截屏内容为空");
+		throw string("板块截屏内容为空");
 	}
 	img = ImgUtil::grayImage(oimg);
 	areaList = recognitionGPArea(img);
@@ -273,14 +302,14 @@
 		sw = getSecondWindow();
 	}
 	if (sw <= 0) {
-		throw("未打开副屏");
+		throw string("未打开副屏");
 	}
 
 	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("板块截屏内容为空");
+		throw string("板块截屏内容为空");
 	}
 	std::list<GPCodeArea>  areaList = recognitionGPArea(img);
 	for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();++ele) {
@@ -335,7 +364,7 @@
 				break;
 			}
 
-			if (startf > -1 && endf > -1 && emptyEndRow - emptyStartRow > 4) {
+			if (startf > -1 && endf > -1 && emptyEndRow - emptyStartRow > 3) {
 				//内容坐标
 				   //LogUtil::debug("内容的高度为:%d \n", endf - startf);
 				int* dd = (int*)malloc(sizeof(int) * 4);
@@ -457,7 +486,11 @@
 			area.starty = *(*ele + 1);
 			area.endx = *(*ele + 2);
 			area.endy = *(*ele + 3);
-			resultList.push_back(area);
+			//过滤
+			if (area.endx - area.startx > 20)
+			{
+				resultList.push_back(area);
+			}
 		}
 		else {
 			std::list<int*>::iterator ele = rowDataList.begin();
@@ -522,7 +555,7 @@
 	}
 
 	if (contentStartRow < 0) {
-		throw("起始行分隔出错");
+		throw string("起始行分隔出错");
 	}
 
 	//分隔列
@@ -547,15 +580,20 @@
 	}
 
 	if (startC < 0 || endC < 0) {
-		throw("内容框分隔出错");
+		throw string("内容框分隔出错");
 	}
 	//cv::imshow("内容", cv::Mat(img, cv::Rect(startC, contentStartRow, endC - startC, rows - contentStartRow)));
 
 	startC += 1;
 	endC -= 1;
-	std::list<GPCodeArea> resultList = splitGPCodeArea(img, contentStartRow + 30, startC, rows, endC);
+	std::list<GPCodeArea> resultList;
+	try {
+		resultList = splitGPCodeArea(img, contentStartRow + 25, startC, rows, endC);
+	}
+	catch (...) {
+		throw string("分隔代码区域出错");
+	}
 	return resultList;
-
 }
 
 std::list<GPCodeArea>   THSActionUtil::recognitionNum(cv::Mat img, std::list<GPCodeArea> areaList) {
@@ -581,8 +619,10 @@
 				path.append(".jpg");
 				cv::imwrite(path, nums);
 			}
+		
 
-			std::list<cv::Mat> list2 = ImgUtil::splitNum(nums, 96);
+			std::list<cv::Mat> list2 = splitGPCodeNum(nums);
+
 			if (false) {
 				std::list<cv::Mat>::iterator e;
 				int ci = 0;
@@ -612,6 +652,18 @@
 }
 
 
+std::list<string> THSActionUtil::recognitionGPCode(cv::Mat img) {
+	cv::Mat grayImg = ImgUtil::grayImage(img);
+	std::list<GPCodeArea> areaList = recognitionGPArea(grayImg);
+	std::list<GPCodeArea>  list = recognitionNum(grayImg, areaList);
+	std::list<string> resultList;
+	for (std::list<GPCodeArea>::iterator ele = list.begin();ele != list.end();ele++) {
+		resultList.push_back((*ele).code);
+	}
+	return resultList;
+}
+
+
 //分隔L2数据的目录
 std::list<GPCodeArea> splitL2Cate(cv::Mat img) {
 	int cols = img.cols;
@@ -638,7 +690,7 @@
 	}
 
 	if (contentStartRow < 0 || contentEndRow < 0) {
-		throw("起始行或结束行分隔出错");
+		throw string("起始行或结束行分隔出错");
 	}
 
 	//分隔列
@@ -725,12 +777,12 @@
 
 
 
-std::map<int,string> THSActionUtil::getListenL2GPCodes() {
+std::map<int, string> THSActionUtil::getListenL2GPCodes() {
 	std::list<GPCodeArea> areaList = getListenL2GP();
 	std::map<int, string> map;
 	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) {
+		if (map.count(data.cateIndex) == 0 && data.type == IMG_TYPE_GP) {
 			map[data.cateIndex] = data.code;
 		}
 	}
@@ -770,29 +822,29 @@
 	//分隔图片
 
 	std::list<GPCodeArea>  areaList = splitL2Cate(img);
+	std::list<GPCodeArea> fresultList;
 
 	int index = 0;
 	for (std::list<GPCodeArea>::iterator ele = areaList.begin();ele != areaList.end();ele++) {
 		GPCodeArea area = *ele;
 		index++;
 		std::list<GPCodeArea> resultList = splitGPCodeArea(img, area.starty + 20, area.startx, area.endy, area.endx);
-		if (false) {
-			std::string path = "C:\\Users\\Administrator\\Desktop\\ocr\\gpcode\\";
-			path.append(std::to_string(index)).append("_listen.jpg");
-			imwrite(path, cv::Mat(img, cv::Rect(area.startx, area.starty, area.endx - area.startx + 1, area.endy - area.starty + 1)));
-		}
+
 
 		for (std::list<GPCodeArea>::iterator ele1 = resultList.begin();ele1 != resultList.end();ele1++) {
 			GPCodeArea areaC = *ele1;
 			if (areaC.type == IMG_TYPE_GP) {
-				list<cv::Mat> nums = ImgUtil::splitNum(cv::Mat(img, cv::Rect(areaC.startx, areaC.starty, areaC.endx - areaC.startx + 1, areaC.endy - areaC.starty + 1)), 96);
+				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<cv::Mat> nums = splitGPCodeNum(src);
 				if (!recognitionManager) {
 					recognitionManager = new RecognitionManager();
 				}
-				if (nums.size() != 6) {
-					throw string("代码分隔出错");
-				}
-
 
 				list<uchar>  rresult = recognitionManager->recognitionGPCode(nums);
 				string num;
@@ -800,13 +852,15 @@
 					num.append(to_string(*e));
 				}
 				(*ele1).code = num;
-				(*ele1).cateIndex = index-1;
+				(*ele1).cateIndex = index - 1;
+
+				fresultList.push_back(*ele1);
 			}
 		}
-
-		return resultList;
 	}
 
+	return fresultList;
+
 }
 bool THSActionUtil::setListenL2GP(int p, string code) {
 

--
Gitblit v1.8.0