maoyehu 2 gadi atpakaļ
vecāks
revīzija
ebc567c774
1 mainītis faili ar 25 papildinājumiem un 6 dzēšanām
  1. 25 6
      MFCApplication1/MFCApplication1Dlg.cpp

+ 25 - 6
MFCApplication1/MFCApplication1Dlg.cpp

@@ -264,20 +264,20 @@ int handwriting(string strImage, std::string &json_result, const std::string &ac
 	return is_success;
 }
 
-void ParseJson(std::string& strJson, vector<std::tuple<cv::Rect, std::string>>& vec)
+bool ParseJson(std::string& strJson, vector<std::tuple<cv::Rect, std::string>>& vec)
 {
 	Json::Features features;
 	Json::Reader reader(features);
 	Json::Value root;
 	if (!reader.parse(strJson, root))
 	{
-		return;
+		return false;
 	}
 
 	Json::Value words_result = root["words_result"];
 	if (!words_result.isArray())
 	{
-		return;
+		return false;
 	}
 
 	for (int i = 0; i < words_result.size(); i++)
@@ -287,6 +287,8 @@ void ParseJson(std::string& strJson, vector<std::tuple<cv::Rect, std::string>>&
 		cv::Rect rc = cv::Rect(location["left"].asInt(), location["top"].asInt(), location["width"].asInt(), location["height"].asInt());
 		vec.push_back(make_tuple(rc, row["words"].asString()));
 	}
+
+	return true;
 }
 
 int IdentifyCallback(result::spinfo& pinfo, void* param)
@@ -315,13 +317,30 @@ int IdentifyCallback(result::spinfo& pinfo, void* param)
 			number += pow(2, length - 1 - i);
 	}
 
+
 	string strToken = CT2A(CConfig::Instance()->m_strToken);
 	string strResult;
 	string strEncode = GetImageEncodeString(pinfo.vecUrlAPath[0]);
-	handwriting(strEncode, strResult, strToken);
-
 	vector<std::tuple<cv::Rect, std::string>> vecTranslate;
-	ParseJson(strResult, vecTranslate);
+
+	int trycnt = 3;
+	bool ret = false;
+	do
+	{
+		handwriting(strEncode, strResult, strToken);
+		ret = ParseJson(strResult, vecTranslate);
+		if (ret)
+		{
+			break;
+		}
+		trycnt--;
+	} while (trycnt > 0);
+
+	if (!ret)
+	{
+		pWnd->FormatScanMsg("调用百度接口,生成切割图像失败");
+		return 0;
+	}
 
 	std::string strJsonDir = CT2A(CConfig::Instance()->m_strMode);
 	strJsonDir.append("/");