BaiduOCRController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using OASystem.API.OAMethodLib;
  2. using OASystem.API.OAMethodLib.BaiduApi;
  3. using OASystem.API.OAMethodLib.TencentCloudAPI;
  4. using OASystem.Domain.Dtos.Baidu;
  5. using OASystem.Domain.ViewModels.OCR;
  6. using Quartz.Util;
  7. namespace OASystem.API.Controllers
  8. {
  9. /// <summary>
  10. /// 百度OCR识别
  11. /// </summary>
  12. [Route("api/[controller]/[action]")]
  13. [ApiController]
  14. public class BaiduOCRController : ControllerBase
  15. {
  16. private readonly SqlSugarClient _sqlSugar;
  17. public BaiduOCRController(SqlSugarClient sqlSugar)
  18. {
  19. _sqlSugar = sqlSugar;
  20. }
  21. /// <summary>
  22. /// 接团客户OCR识别
  23. /// </summary>
  24. /// <param name="dto"></param>
  25. /// <returns></returns>
  26. [HttpPost]
  27. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  28. public IActionResult ClientOCR(ClientOCRDto dto)
  29. {
  30. try
  31. {
  32. string[] picBase64Array = dto.base64img.Split(';');
  33. string picFormat = picBase64Array[0].Split('/')[1];
  34. if (!TencentOCRTools.ImageType(picFormat))
  35. {
  36. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  37. }
  38. }
  39. catch (Exception ex)
  40. {
  41. return Ok(JsonView("请上传图片base64编码数据!"));
  42. }
  43. JsonView jw = new JsonView();
  44. try
  45. {
  46. string api_Response = BaiduApiHelper._apiTableV2(dto.base64img);
  47. JObject json = JObject.Parse(api_Response);
  48. if (json["tables_result"][0]["body"] != null)
  49. {
  50. List<BaiduOCR_TableV2> sourceList = JsonConvert.DeserializeObject<List<BaiduOCR_TableV2>>(json["tables_result"][0]["body"].ToString());
  51. List<BaiduOCR_TableV2> headerList = sourceList.Where(s => s.row_start == 0).OrderBy(s => s.col_start).Select(x => new BaiduOCR_TableV2
  52. {
  53. col_end = x.col_end,
  54. col_start = x.col_start,
  55. row_end = x.row_end,
  56. row_start = x.row_start,
  57. words = Regex.Replace(x.words, "[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\/;'\\\\[\\]·!#¥(——):;“”‘、,|《。》?、【】[\\]]", string.Empty)
  58. }).ToList();
  59. List<BaiduOCR_TableV2> dataList = sourceList.Where(s => s.row_start != 0).ToList();
  60. int maxRowIndex = dataList.Max(s => s.row_start);
  61. int maxColIndex = headerList.Max(s => s.col_start) + 1;
  62. Dictionary<int, string> dicHeader = new Dictionary<int, string>();
  63. List<string> listNameStr = new List<string>() { "姓名", "name", "姓 名" };
  64. List<string> listSexStr = new List<string>() { "性别", "gender", "sex" };
  65. List<string> listDOBStr = new List<string>() { "D.O.B", "出生年月", "生日", "出生日期", "出生年月日" };
  66. List<string> listIdCard = new List<string>() { "身份证号码", "身份证", "身份证号" };
  67. List<string> listOrganizationStr = new List<string>() { "工作单位", "organization", "单位", "所在单位" };
  68. List<string> listJobStr = new List<string>() { "职务", "title", "职位", "所在单位及职务", "单位及职务", "职务或职称" };
  69. List<string> listMobileStr = new List<string>() { "联系电话", "mobile" };
  70. List<string> listAgeStr = new List<string>() { "年龄", "age" };
  71. List<string> listBeiZhu = new List<string>() { "备注", "本团职务" };
  72. try
  73. {
  74. //OCR JSON
  75. var json_baiduOCR = _sqlSugar.Queryable<Sys_SetData>().First(x => x.IsDel == 0 && x.Id == 1347)?.Remark;
  76. if (!string.IsNullOrWhiteSpace(json_baiduOCR))
  77. {
  78. var JObject_json_baiduOCR = JObject.Parse(json_baiduOCR);
  79. // 遍历每个属性,将对应的数组值添加到对应的List中
  80. foreach (var property in JObject_json_baiduOCR.Properties())
  81. {
  82. string key = property.Name;
  83. JArray array = (JArray)property.Value;
  84. switch (key)
  85. {
  86. case "姓名":
  87. foreach (var item in array)
  88. {
  89. listNameStr.Add(item.ToString());
  90. }
  91. break;
  92. case "性别":
  93. foreach (var item in array)
  94. {
  95. listSexStr.Add(item.ToString());
  96. }
  97. break;
  98. case "出生年月":
  99. foreach (var item in array)
  100. {
  101. listDOBStr.Add(item.ToString());
  102. }
  103. break;
  104. case "身份证":
  105. foreach (var item in array)
  106. {
  107. listIdCard.Add(item.ToString());
  108. }
  109. break;
  110. case "工作单位":
  111. foreach (var item in array)
  112. {
  113. listOrganizationStr.Add(item.ToString());
  114. }
  115. break;
  116. case "职务":
  117. foreach (var item in array)
  118. {
  119. listJobStr.Add(item.ToString());
  120. }
  121. break;
  122. case "联系电话":
  123. foreach (var item in array)
  124. {
  125. listMobileStr.Add(item.ToString());
  126. }
  127. break;
  128. case "年龄":
  129. foreach (var item in array)
  130. {
  131. listAgeStr.Add(item.ToString());
  132. }
  133. break;
  134. case "备注":
  135. foreach (var item in array)
  136. {
  137. listBeiZhu.Add(item.ToString());
  138. }
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. catch (Exception ex)
  145. {
  146. }
  147. for (int i = 0; i < headerList.Count; i++)
  148. {
  149. string words = headerList[i].words.Trim().ToLower();
  150. if (listNameStr.Exists(s => s == words))
  151. {
  152. dicHeader.Add(i, "Name");
  153. }
  154. else if (listSexStr.Exists(s => s == words))
  155. {
  156. dicHeader.Add(i, "Sex");
  157. }
  158. else if (listDOBStr.Exists(s => s == words))
  159. {
  160. dicHeader.Add(i, "DOB");
  161. }
  162. else if (listIdCard.Exists(s => s == words))
  163. {
  164. dicHeader.Add(i, "IdCard");
  165. }
  166. else if (listOrganizationStr.Exists(s => s == words))
  167. {
  168. dicHeader.Add(i, "Organization");
  169. }
  170. else if (listJobStr.Exists(s => s == words))
  171. {
  172. dicHeader.Add(i, "Job");
  173. }
  174. else if (listMobileStr.Exists(s => s == words))
  175. {
  176. dicHeader.Add(i, "Mobile");
  177. }
  178. else if (listAgeStr.Exists(s => s == words))
  179. {
  180. dicHeader.Add(i, "Age");
  181. }
  182. else if (listBeiZhu.Exists(s => s == words))
  183. {
  184. dicHeader.Add(i, "BeiZhu");
  185. }
  186. }
  187. List<BaiduClientOCRView> ClientArr = new List<BaiduClientOCRView>();
  188. for (int i = 1; i <= maxRowIndex; i++)
  189. {
  190. BaiduClientOCRView client = new BaiduClientOCRView();
  191. for (int j = 0; j < maxColIndex; j++)
  192. {
  193. var item = dataList.First(s => s.row_start == i && s.col_start == j);
  194. string words = item.words.Replace("\\n", "");
  195. string values = string.Empty;
  196. try
  197. {
  198. values = dicHeader[j];
  199. }
  200. catch (Exception)
  201. {
  202. throw new Exception("有未能识别的列名请检查 : " + JsonConvert.SerializeObject(headerList.Select(x => x.words)));
  203. }
  204. if (words.IsNullOrWhiteSpace())
  205. {
  206. continue;
  207. }
  208. if (values.Equals("Name"))
  209. {
  210. if (Regex.Matches(words, "[a-zA-Z]").Count < 1)
  211. {
  212. string lastName = words.Substring(0, 1);
  213. string name = words.Substring(1);
  214. client.LastName = lastName;
  215. client.FirstName = name;
  216. string lastNamePinYin = lastName.GetTotalPingYin().Count > 0 ? lastName.GetTotalPingYin()[0].ToUpper() : "";
  217. string firstNamePinYin = "";
  218. for (int n = 0; n < name.Length; n++)
  219. {
  220. firstNamePinYin += name[n].ToString().GetTotalPingYin().Count > 0 ? name[n].ToString().GetTotalPingYin()[0].ToUpper() + " " : "";
  221. }
  222. client.Pinyin = lastNamePinYin + "/" + firstNamePinYin;
  223. }
  224. else
  225. {
  226. Regex regForeign = new Regex("[a-zA-Z]+[\\s][a-zA-Z]+");
  227. if (regForeign.IsMatch(words))
  228. {
  229. string[] names = words.Split(' ');
  230. client.LastName = names[1];
  231. client.FirstName = names[0];
  232. }
  233. else
  234. {
  235. client.FirstName = words;
  236. }
  237. }
  238. }
  239. else if (values.Equals("Sex"))
  240. {
  241. client.Sex = words == "男" ? 0 : 1;
  242. }
  243. else if (values.Equals("DOB"))
  244. {
  245. client.Birthday = words.Replace(".", "-");
  246. DateTime time = new DateTime();
  247. if (DateTime.TryParse(client.Birthday, out time))
  248. {
  249. client.Birthday = time.ToString("yyyy-MM-dd");
  250. }
  251. else
  252. {
  253. client.Birthday = "";
  254. }
  255. }
  256. else if (values.Equals("IdCard"))
  257. {
  258. client.IDcard = words;
  259. }
  260. else if (values.Equals("Organization"))
  261. {
  262. client.CompanyFullName = words;
  263. }
  264. else if (values.Equals("Job"))
  265. {
  266. client.Job = words;
  267. }
  268. else if (values.Equals("Mobile"))
  269. {
  270. client.Phone = words;
  271. }
  272. }
  273. if (!(client.FirstName + client.LastName + client.Pinyin + client.CompanyFullName + client.Job + client.Phone + client.Birthday).IsNullOrWhiteSpace())
  274. {
  275. if (string.IsNullOrWhiteSpace(client.CompanyFullName))
  276. {
  277. client.CompanyFullName = "暂无";
  278. }
  279. if (!string.IsNullOrWhiteSpace(client.LastName + client.FirstName))
  280. {
  281. var pinyin = client.LastName.GetTotalPingYinFirst() + "/";
  282. foreach (var cn in client.FirstName)
  283. {
  284. pinyin += cn.ToString().GetTotalPingYinFirst() + " ";
  285. }
  286. client.Pinyin = pinyin.TrimEnd();
  287. }
  288. ClientArr.Add(client);
  289. }
  290. }
  291. jw.Code = 200;
  292. jw.Msg = "获取成功!";
  293. jw.Data = ClientArr;
  294. }
  295. else if (json["error_code"] != null)
  296. {
  297. jw.Data = new string[0];
  298. jw.Msg = "百度接口异常!";
  299. }
  300. else
  301. {
  302. jw.Data = new string[0];
  303. jw.Msg = "百度接口异常!";
  304. }
  305. }
  306. catch (Exception ex)
  307. {
  308. jw.Code = 400;
  309. jw.Msg = "程序异常!" + ex.Message;
  310. jw.Data = new string[0];
  311. }
  312. return Ok(jw);
  313. }
  314. }
  315. }