From 125db633619a0b4c7bd1d498ea2bf1cefa4f73d3 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期四, 09 二月 2023 18:54:13 +0800
Subject: [PATCH] '远程OCR'

---
 app/appDlg.cpp |  290 ++++++++++++++++++++++++---------------------------------
 1 files changed, 122 insertions(+), 168 deletions(-)

diff --git a/app/appDlg.cpp b/app/appDlg.cpp
index a32377e..e226c6c 100644
--- a/app/appDlg.cpp
+++ b/app/appDlg.cpp
@@ -16,6 +16,7 @@
 #endif 
 #include "JsonUtil.h"
 #include <exception>
+#include "OcrUtil.h"
 
 
 #ifdef _DEBUG
@@ -294,7 +295,7 @@
 				}
 			}
 			autoMsgStatic.SetWindowTextW(_T("绛夊緟鎵撳紑鍓睆1..."));
-			
+
 			for (int i = 0; i < 5; i++) {
 				HWND second = THSActionUtil::getSecondWindow();
 				if (second > 0) {
@@ -303,7 +304,7 @@
 				THSActionUtil::openSecondScreen();
 				Sleep(1000);
 			}
-		
+
 			autoMsgStatic.SetWindowTextW(_T("鍓睆1宸叉墦寮�..."));
 			Sleep(1000);
 			// 寮�鍚� 娑ㄥ仠璇嗗埆
@@ -311,9 +312,9 @@
 			CappDlg::OnBnClickedButton12();
 			autoMsgStatic.SetWindowTextW(_T("鍚姩娑ㄥ仠鍚姩鎴愬姛"));
 
-           // 寮�鍚鎵樿瘑鍒�
+			// 寮�鍚鎵樿瘑鍒�
 
-           // 寮�鍚氦鏄撴垚鍔熻瘑鍒�
+			// 寮�鍚氦鏄撴垚鍔熻瘑鍒�
 
 
 
@@ -481,18 +482,29 @@
 
 
 void  CappDlg::OnL2DataCallback(int index, string code, long captureTime, long processTime, list<TradeData*> dataList, void* context) {
+	clock_t time_start = clock();
 	//杞负json
 	CappDlg* app = (CappDlg*)context;
 	//cout << "鍥炶皟:" << std::this_thread::get_id() << ":" << index << endl;
 	CButton* btn = (CButton*)app->GetDlgItem(IDC_CHECK1);
 	bool check = btn->GetCheck();
 	if (check) {
+		if (index == 0)
+		{
+			cout << "\n*****鐘舵�佸垽鏂敤鏃�*****锛�" << (clock() - time_start) << endl;
+		}
+		time_start = clock();
 		string data = JsonUtil::loadL2Data(app->clientNum, index, code, captureTime, processTime, dataList);
-		for (list<TradeData*>::iterator el = dataList.begin(); el != dataList.end();++el) {
+		for (list<TradeData*>::iterator el = dataList.begin(); el != dataList.end(); ++el) {
 			delete (*el);
 		}
 		dataList.clear();
-		clock_t time_start = clock();
+		if (index == 0)
+		{
+			cout << "\n*****缁勮鏁版嵁澶勭悊鐢ㄦ椂*****锛�" << (clock() - time_start) << endl;
+		}
+
+		time_start = clock();
 		try {
 			app->socketManager->sendMsg(index, data.c_str());
 		}
@@ -501,7 +513,10 @@
 			app->socketManager->resetClient(index);
 			app->socketManager->Connect(index);
 		}
-		//cout << "*****鏁版嵁澶勭悊鏃堕棿锛�" << (clock() - time_start) << endl;
+		if (index == 0)
+		{
+			cout << "\n*****鏈嶅姟绔暟鎹鐞嗘椂闂�*****锛�" << (clock() - time_start) << endl;
+		}
 	}
 }
 
@@ -583,23 +598,19 @@
 		clock_t time_start = clock();
 
 		try {
-			app->socketManager->sendMsg(8, data.c_str());
+			SocketManager::sendMsg(data.c_str());
 		}
 		catch (string st) {
-			//閲嶆柊杩炴帴鏈嶅姟鍣�
-			app->socketManager->resetClient(8);
-			app->socketManager->Connect(8);
+			
 		}
 
 
 		//涓婁紶璐︽埛鍙敤浣欓
 		data = JsonUtil::loadAvailableMoney(app->clientNum, money);
 		try {
-			app->socketManager->sendMsg(8, data.c_str());
+			SocketManager::sendMsg(data.c_str());
 		}
 		catch (string st) {
-			app->socketManager->resetClient(8);
-			app->socketManager->Connect(8);
 		}
 
 		cout << "####浜ゆ槗鎴愬姛鏁版嵁澶勭悊鏃堕棿锛�" << (clock() - time_start) << endl;
@@ -837,8 +848,8 @@
 		//app->onlineCheck.PostMessage(WM_LBUTTONUP, 0, 0);
 		RECT rect;
 		Win32Util::getWindowRect(app->onlineCheck.m_hWnd, &rect);
-		Win32Util::click((rect.left + rect.right )/ 2, (rect.top + rect.bottom) / 2, 10);
-	
+		Win32Util::click((rect.left + rect.right) / 2, (rect.top + rect.bottom) / 2, 10);
+
 	}
 	else if (action == "repairL2Data") {
 		//淇浠g爜涓嶭2琛屾儏浠g爜涓嶅搴� 鎴栬�� L2琛屾儏鏃犳暟鎹殑鎯呭喌
@@ -1042,6 +1053,11 @@
 	return string();
 }
 
+list<OCRResult> CappDlg::MatOcr(string key_regex, cv::Mat mat)
+{
+	return OcrUtil::mat_ocr(key_regex, mat);
+}
+
 //蹇冭烦淇″彿
 void  CappDlg::onHeartbeat() {
 	//5s涓�娆″績璺�
@@ -1115,7 +1131,7 @@
 	if (!limitUpCapture->isInited())
 	{
 		try {
-			limitUpCapture->init(OnLimitUpDataCallback, this);
+			limitUpCapture->init(OnLimitUpDataCallback,  MatOcr, this);
 		}
 		catch (string st) {
 			if (notify)
@@ -1185,6 +1201,13 @@
 		Win32Util::showWindow(l2);
 		//鍒濆鍖杔2鐨勫悇涓獥鍙e彞鏌�
 		app->capture->refreshHWND();
+		//閫変腑鏉垮潡
+		try {
+			//THSActionUtil::correctL2ScreenPlate(OcrUtil::mat_ocr);
+		}
+		catch (string st) {
+
+		}
 	}
 
 	HWND  second = THSActionUtil::getSecondWindow();
@@ -1213,6 +1236,14 @@
 	//灏嗗壇灞�1涓庡壇灞�2缂╁皬涓哄睆骞曠殑涓�鍗�
 	Win32Util::moveWin(second, device_pos.x, device_pos.y + device_height / 2, device_width / 2 - 50, device_height / 2);
 	Win32Util::moveWin(third, device_pos.x + device_width / 2 + 30, device_pos.y + device_height / 2, device_width / 2 - 50, device_height / 2);
+
+	try {
+		Sleep(500);
+		THSActionUtil::correctL2ScreenPlate(OcrUtil::mat_ocr);
+	}
+	catch (...) {
+
+	}
 }
 
 void CappDlg::autoStartL2(void* context)
@@ -1267,7 +1298,7 @@
 		app->socketManager->resetClient(8);
 		app->socketManager->Connect(8);
 	}
-	
+
 }
 
 void CappDlg::autoStartTradeDelegate(void* context)
@@ -1346,8 +1377,8 @@
 	{
 		try {
 			socketManager->sendMsg(i, "test");
-				scount++;
-			
+			scount++;
+
 		}
 		catch (string st) {
 
@@ -1377,7 +1408,7 @@
 	{
 		socketManager->sendMsg(i, "test");
 		scount++;
-	
+
 	}
 
 
@@ -1446,7 +1477,7 @@
 void CappDlg::OnBnClickedButton20()
 {
 	try {
-		THSActionUtil::checkEnv();
+		checkTHSEnv(this);
 		AfxMessageBox(_T("妫�娴嬮�氳繃"));
 	}
 	catch (const string& st) {
@@ -1459,16 +1490,6 @@
 //鍚姩浠诲姟
 void CappDlg::OnBnClickedButton5()
 {
-	//妫�娴嬬幆澧�
-
-	try {
-		THSActionUtil::checkEnv();
-	}
-	catch (string st) {
-		CString msg(st.c_str());
-		AfxMessageBox(msg);
-		return;
-	}
 
 	CButton* btn = (CButton*)GetDlgItem(IDC_BUTTON5);
 
@@ -1484,6 +1505,9 @@
 		}
 	}
 	if (!capture->isRunning()) {
+
+		// 妫�娴嬬幆澧�
+		checkTHSEnv(this);
 
 		try {
 			map<int, string> result = THSActionUtil::getListenL2GPCodes(recognitionManager);
@@ -1513,6 +1537,14 @@
 
 		//TODO 娴嬭瘯鍥剧墖鍒囧壊
 		//THSActionUtil::getListenL2GPAreaActionBar();
+		// 妫�娴嬪悓鑺遍『鏉垮潡鏄惁閫変腑
+		try {
+			Sleep(500);
+			THSActionUtil::correctL2ScreenPlate(OcrUtil::mat_ocr);
+		}
+		catch (...) {
+
+		}
 
 	}
 	else {
@@ -1655,7 +1687,7 @@
 	if (!limitUpCapture->isInited()) {
 		try
 		{
-			limitUpCapture->init(OnLimitUpDataCallback, this);
+			limitUpCapture->init(OnLimitUpDataCallback,MatOcr,this);
 
 		}
 		catch (string st)
@@ -1726,6 +1758,13 @@
 			AfxMessageBox(msg);
 			return;
 		}
+		//濡傛灉宸茬粡鎵撳紑浜嗚繙鑸増鐨勪氦鏄擄紝灏遍渶瑕佺偣鍑诲綋鏃ュ鎵�
+		try {
+			tradeListCapture->selectTradeSuccess(MatOcr);
+		}
+		catch (...) {
+
+		}
 
 		tradeListCapture->startTradeSuccess();
 		tradeBtn.SetWindowTextW(_T("鏆傚仠鎴愪氦璇嗗埆浠诲姟"));
@@ -1778,62 +1817,40 @@
 
 	//鎵撳紑鍚岃姳椤鸿繙鑸増鐨勫鎵樼晫闈�
 	HWND win = 0;
-   if (win <= 0) {
-	   //鍒ゆ柇鏄惁宸茬粡鎵撳紑浜嗗悓鑺遍『杩滆埅鐗�
-	   for (int i = 0; i < 10; i++)
-	   {
-		   win = THSActionUtil::getTradeSimpleDelegateWindow();
-		   if (win <= 0)
-		   {
-			   list<HWND> wlist = Win32Util::searchWindow("鍚岃姳椤鸿繙鑸増");
-			   if (wlist.size() > 0) {
-				   HWND mainWin = *(wlist.begin());
-				   //鎸塅12
-				   Win32Util::sendMessage(mainWin, WM_KEYDOWN, VK_F12, 0);
-				   Win32Util::sendMessage(mainWin, WM_KEYUP, VK_F12, 0);
-				   //绛夊緟鍙嶅簲
-				   Sleep(500);
-			   }
-			   else {
-				   //绛夊緟杩滆埅鐗堟墦寮�
-				   Sleep(1000);
-			   }
-		   }
-		   else {
-			   break;
-		   }
-	   }
-   }
-
-   //濡傛灉宸茬粡鎵撳紑浜嗚繙鑸増鐨勪氦鏄擄紝灏遍渶瑕佺偣鍑诲綋鏃ュ鎵�
-   try {
-	  HWND menuWin = TradeListCapture::getSimpleTradeLeftMenu(win);
-	  if (menuWin > 0) {
-		cv::Mat img =  CaptureUtil::capture(menuWin);
-		string result = JsonUtil::loadOcrData(clientNum, img, "褰撴棩濮旀墭");
-		result = socketManager->sendMsg(result.c_str());
-		Json::Value root = JsonUtil::parseJson(result);
-		if (root["code"] == 0) {
-		  //鍥惧儚璇嗗埆鎴愬姛
-			Json::Value datas = root["data"]["datas"];
-			Json::Value ps = datas[0]["position"];
-			//position鏍煎紡锛歔[0,13],[12,13],[12,15],[0,15]]
-			//鑾峰彇涓績鐐�
-			int x = (ps[0][0].asInt() + ps[1][0].asInt()) / 2;
-			int y= (ps[0][1].asInt() + ps[2][1].asInt()) / 2;
-			RECT rect;
-			Win32Util::getWindowRect(menuWin,&rect);
-			x = rect.left + x;
-			y = rect.top + y;
-			//鐐瑰嚮
-			Win32Util::click(x,y,10);
+	if (win <= 0) {
+		//鍒ゆ柇鏄惁宸茬粡鎵撳紑浜嗗悓鑺遍『杩滆埅鐗�
+		for (int i = 0; i < 10; i++)
+		{
+			win = THSActionUtil::getTradeSimpleDelegateWindow();
+			if (win <= 0)
+			{
+				list<HWND> wlist = Win32Util::searchWindow("鍚岃姳椤鸿繙鑸増");
+				if (wlist.size() > 0) {
+					HWND mainWin = *(wlist.begin());
+					//鎸塅12
+					Win32Util::sendMessage(mainWin, WM_KEYDOWN, VK_F12, 0);
+					Win32Util::sendMessage(mainWin, WM_KEYUP, VK_F12, 0);
+					//绛夊緟鍙嶅簲
+					Sleep(500);
+				}
+				else {
+					//绛夊緟杩滆埅鐗堟墦寮�
+					Sleep(1000);
+				}
+			}
+			else {
+				break;
+			}
 		}
-	  }
-   
-   }
-   catch (...) {
-   
-   }
+	}
+
+	//濡傛灉宸茬粡鎵撳紑浜嗚繙鑸増鐨勪氦鏄擄紝灏遍渶瑕佺偣鍑诲綋鏃ュ鎵�
+	try {
+		tradeListCapture->selectTradeSimpleDelegate(MatOcr);
+	}
+	catch (...) {
+
+	}
 
 
 
@@ -1871,33 +1888,24 @@
 		tradeListCapture->startTradeDelegate();
 		delegateBtn.SetWindowTextW(_T("鏆傚仠濮旀墭璇嗗埆浠诲姟"));
 	}
-
-
-
 }
+
+
+
 
 //娴嬭瘯鎸夐挳
 void CappDlg::OnBnClickedButtonTest()
 {
-	cv::Mat mat = cv::imread("C:\\Users\\Administrator\\Desktop\\ocr\\trade\\Snipaste_2023-01-13_18-23-34_gray.png", cv::IMREAD_GRAYSCALE);
-	Json::Value array;
-	int len = mat.rows * mat.cols;
-	for (int i = 0; i < len; i++) {
-		array[i] = mat.data[i];
-	}
-	Json::Value data;
-	data["data"] = array;
-	data["width"] = mat.cols;
-	data["height"] = mat.rows;
-	data["key"] = "褰撴棩濮旀墭" ;
-	Json::Value root;
-	root["data"] = data;
-	root["type"] = 100;
-	string result = JsonUtil::jsonValue2String(root);
-	try {
-		socketManager->sendMsg(result.c_str());
-	}
-	catch (string st) {
+	if (1 > 0) {
+		//THSActionUtil::correctL2ScreenPlate(MatOcr);
+		//濡傛灉宸茬粡鎵撳紑浜嗚繙鑸増鐨勪氦鏄擄紝灏遍渶瑕佺偣鍑诲綋鏃ュ鎵�
+		try {
+			THSActionUtil::correctL2ScreenPlate(OcrUtil::mat_ocr);
+		}
+		catch (...) {
+
+		}
+		return;
 	}
 }
 
@@ -1912,67 +1920,13 @@
 {
 
 	try {
-		THSActionUtil::checkEnv();
+		checkTHSEnv(this);
 	}
-	catch (string e) {
-		//鎵撳紑鍚岃姳椤虹殑鎵�鏈夊睆骞�
-		try {
-			THSActionUtil::openL2Screen();
-			THSActionUtil::openSecondScreen();
-			Sleep(1000);
-			THSActionUtil::openThirdScreen();
-			Sleep(1000);
-		}
-		catch (...) {
-
-		}
-	}
-
-
-
-	try {
-		HWND l2 = THSActionUtil::getL2Win();
-		if (l2 > 0)
-		{
-			Win32Util::showWindow(l2);
-			//鍒濆鍖杔2鐨勫悇涓獥鍙e彞鏌�
-			capture->refreshHWND();
-		}
-
-		HWND  second = THSActionUtil::getSecondWindow();
-		if (second <= 0) {
-			AfxMessageBox(_T("鍚岃姳椤哄壇灞�1鏈墦寮�"));
-			return;
-		}
-
-		Win32Util::showWindow(second);
-
-
-		HWND  third = THSActionUtil::getThirdWindow();
-		if (third <= 0) {
-			AfxMessageBox(_T("鍚岃姳椤哄壇灞�2鏈墦寮�"));
-			return;
-		}
-
-		Win32Util::showWindow(third);
-
-
-		//璁剧疆
-		DEVMODE device = Win32Util::getL2ScreenInfo();
-		int device_width = device.dmPelsWidth;
-		int device_height = device.dmPelsHeight;
-		POINTL device_pos = device.dmPosition;
-		//鏈�澶у寲L2
-		Win32Util::moveWin(l2, device_pos.x, device_pos.y, device_width, device_height);
-		//灏嗗壇灞�1涓庡壇灞�2缂╁皬涓哄睆骞曠殑涓�鍗�
-		Win32Util::moveWin(second, device_pos.x, device_pos.y + device_height / 2, device_width / 2 - 50, device_height / 2);
-		Win32Util::moveWin(third, device_pos.x + device_width / 2 + 30, device_pos.y + device_height / 2, device_width / 2 - 50, device_height / 2);
-	}
-	catch (string e) {
-		CString msg(e.c_str());
+	catch (string st) {
+		CString msg(st.c_str());
 		AfxMessageBox(msg);
+		return;
 	}
-
 
 }
 

--
Gitblit v1.8.0