BaiduOCRController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. using Autofac.Core;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using OASystem.API.OAMethodLib;
  5. using OASystem.API.OAMethodLib.BaiduApi;
  6. using OASystem.API.OAMethodLib.TencentCloudAPI;
  7. using OASystem.Domain.Dtos.Baidu;
  8. using OASystem.Domain.Dtos.CRM;
  9. using OASystem.Domain.Entities.Customer;
  10. using OASystem.Domain.ViewModels.OCR;
  11. using Quartz.Util;
  12. namespace OASystem.API.Controllers
  13. {
  14. /// <summary>
  15. /// 百度OCR识别
  16. /// </summary>
  17. [Route("api/[controller]/[action]")]
  18. [ApiController]
  19. public class BaiduOCRController : ControllerBase
  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. for (int i = 0; i < headerList.Count; i++)
  73. {
  74. string words = headerList[i].words.Trim().ToLower();
  75. if (listNameStr.Exists(s => s == words))
  76. {
  77. dicHeader.Add(i, "Name");
  78. }
  79. else if (listSexStr.Exists(s => s == words))
  80. {
  81. dicHeader.Add(i, "Sex");
  82. }
  83. else if (listDOBStr.Exists(s => s == words))
  84. {
  85. dicHeader.Add(i, "DOB");
  86. }
  87. else if (listIdCard.Exists(s => s == words))
  88. {
  89. dicHeader.Add(i, "IdCard");
  90. }
  91. else if (listOrganizationStr.Exists(s => s == words))
  92. {
  93. dicHeader.Add(i, "Organization");
  94. }
  95. else if (listJobStr.Exists(s => s == words))
  96. {
  97. dicHeader.Add(i, "Job");
  98. }
  99. else if (listMobileStr.Exists(s => s == words))
  100. {
  101. dicHeader.Add(i, "Mobile");
  102. }
  103. else if (listAgeStr.Exists(s => s == words))
  104. {
  105. dicHeader.Add(i, "Age");
  106. }
  107. else if (listBeiZhu.Exists(s => s == words))
  108. {
  109. dicHeader.Add(i, "BeiZhu");
  110. }
  111. }
  112. List<BaiduClientOCRView> ClientArr = new List<BaiduClientOCRView>();
  113. for (int i = 1; i <= maxRowIndex; i++)
  114. {
  115. BaiduClientOCRView client = new BaiduClientOCRView();
  116. for (int j = 0; j < maxColIndex; j++)
  117. {
  118. var item = dataList.First(s => s.row_start == i && s.col_start == j);
  119. string words = item.words.Replace("\\n", "");
  120. string values = string.Empty;
  121. try
  122. {
  123. values = dicHeader[j];
  124. }
  125. catch (Exception)
  126. {
  127. throw new Exception("有未能识别的列名请检查 : " + JsonConvert.SerializeObject(headerList.Select(x => x.words)));
  128. }
  129. if (words.IsNullOrWhiteSpace())
  130. {
  131. continue;
  132. }
  133. if (values.Equals("Name"))
  134. {
  135. if (Regex.Matches(words, "[a-zA-Z]").Count < 1)
  136. {
  137. string lastName = words.Substring(0, 1);
  138. string name = words.Substring(1);
  139. client.LastName = lastName;
  140. client.FirstName = name;
  141. string lastNamePinYin = lastName.GetTotalPingYin().Count > 0 ? lastName.GetTotalPingYin()[0].ToUpper():"";
  142. string firstNamePinYin = "";
  143. for (int n = 0; n < name.Length; n++)
  144. {
  145. firstNamePinYin += name[n].ToString().GetTotalPingYin().Count > 0 ? name[n].ToString().GetTotalPingYin()[0].ToUpper() + " " : "";
  146. }
  147. client.Pinyin = lastNamePinYin + "/" + firstNamePinYin;
  148. }
  149. else
  150. {
  151. Regex regForeign = new Regex("[a-zA-Z]+[\\s][a-zA-Z]+");
  152. if (regForeign.IsMatch(words))
  153. {
  154. string[] names = words.Split(' ');
  155. client.LastName = names[1];
  156. client.FirstName = names[0];
  157. }
  158. else
  159. {
  160. client.FirstName = words;
  161. }
  162. }
  163. }
  164. else if (values.Equals("Sex"))
  165. {
  166. client.Sex = words == "男" ? 0 : 1;
  167. }
  168. else if (values.Equals("DOB"))
  169. {
  170. client.Birthday = words.Replace(".", "-");
  171. DateTime time = new DateTime();
  172. if (DateTime.TryParse(client.Birthday, out time))
  173. {
  174. client.Birthday = time.ToString("yyyy-MM-dd");
  175. }
  176. else
  177. {
  178. client.Birthday = "";
  179. }
  180. }
  181. else if (values.Equals("IdCard"))
  182. {
  183. client.IDcard = words;
  184. }
  185. else if (values.Equals("Organization"))
  186. {
  187. client.CompanyFullName = words;
  188. }
  189. else if (values.Equals("Job"))
  190. {
  191. client.Job = words;
  192. }
  193. else if (values.Equals("Mobile"))
  194. {
  195. client.Phone = words;
  196. }
  197. }
  198. if(!(client.FirstName + client.LastName + client.Pinyin + client.CompanyFullName + client.Job + client.Phone + client.Birthday).IsNullOrWhiteSpace())
  199. {
  200. if (string.IsNullOrWhiteSpace(client.CompanyFullName))
  201. {
  202. client.CompanyFullName = "暂无";
  203. }
  204. if (!string.IsNullOrWhiteSpace(client.LastName + client.FirstName))
  205. {
  206. var pinyin = client.LastName.GetTotalPingYinFirst() + "/";
  207. foreach (var cn in client.FirstName)
  208. {
  209. pinyin += cn.ToString().GetTotalPingYinFirst() + " ";
  210. }
  211. client.Pinyin = pinyin.TrimEnd();
  212. }
  213. ClientArr.Add(client);
  214. }
  215. }
  216. jw.Code = 200;
  217. jw.Msg = "获取成功!";
  218. jw.Data = ClientArr;
  219. }
  220. else if (json["error_code"] != null)
  221. {
  222. jw.Data = new string[0];
  223. jw.Msg = "百度接口异常!";
  224. }
  225. else
  226. {
  227. jw.Data = new string[0];
  228. jw.Msg = "百度接口异常!";
  229. }
  230. }
  231. catch (Exception ex)
  232. {
  233. jw.Code = 400;
  234. jw.Msg = "程序异常!" + ex.Message;
  235. jw.Data = new string[0];
  236. }
  237. return Ok(jw);
  238. }
  239. }
  240. }