BaiduOCRController.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. namespace OASystem.API.Controllers
  12. {
  13. /// <summary>
  14. /// 百度OCR识别
  15. /// </summary>
  16. [Route("api/[controller]/[action]")]
  17. [ApiController]
  18. public class BaiduOCRController : ControllerBase
  19. {
  20. /// <summary>
  21. /// 接团客户OCR识别
  22. /// </summary>
  23. /// <param name="dto"></param>
  24. /// <returns></returns>
  25. [HttpPost]
  26. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  27. public IActionResult ClientOCR(ClientOCRDto dto)
  28. {
  29. try
  30. {
  31. string[] picBase64Array = dto.base64img.Split(';');
  32. string picFormat = picBase64Array[0].Split('/')[1];
  33. if (!TencentOCRTools.ImageType(picFormat))
  34. {
  35. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. return Ok(JsonView("请上传图片base64编码数据!"));
  41. }
  42. JsonView jw = new JsonView();
  43. string api_Response = BaiduApiHelper._apiTableV2(dto.base64img);
  44. JObject json = JObject.Parse(api_Response);
  45. if (json["tables_result"][0]["body"] != null)
  46. {
  47. List<BaiduOCR_TableV2> sourceList = JsonConvert.DeserializeObject<List<BaiduOCR_TableV2>>(json["tables_result"][0]["body"].ToString());
  48. List<BaiduOCR_TableV2> headerList = sourceList.Where(s => s.row_start == 0).OrderBy(s => s.col_start).ToList();
  49. List<BaiduOCR_TableV2> dataList = sourceList.Where(s => s.row_start != 0).ToList();
  50. int maxRowIndex = dataList.Max(s => s.row_start);
  51. int maxColIndex = headerList.Max(s => s.col_start) + 1;
  52. Dictionary<int, string> dicHeader = new Dictionary<int, string>();
  53. List<string> listNameStr = new List<string>() { "姓名", "Name" };
  54. List<string> listSexStr = new List<string>() { "性别", "Gender" };
  55. List<string> listDOBStr = new List<string>() { "D.O.B", "出生年月","生日" };
  56. List<string> listIdCard = new List<string>() { "身份证号码", "身份证" };
  57. List<string> listOrganizationStr = new List<string>() { "工作单位", "Organization", "单位" };
  58. List<string> listJobStr = new List<string>() { "职务", "Title", "职位" };
  59. List<string> listMobileStr = new List<string>() { "联系电话", "Mobile" };
  60. List<string> listAgeStr = new List<string>() { "年龄", "Age" };
  61. for (int i = 0; i < headerList.Count; i++)
  62. {
  63. string words = headerList[i].words.Trim();
  64. if (listNameStr.Exists(s => s == words))
  65. {
  66. dicHeader.Add(i, "Name");
  67. }
  68. else if (listSexStr.Exists(s => s == words))
  69. {
  70. dicHeader.Add(i, "Sex");
  71. }
  72. else if (listDOBStr.Exists(s => s == words))
  73. {
  74. dicHeader.Add(i, "DOB");
  75. }
  76. else if (listIdCard.Exists(s => s == words))
  77. {
  78. dicHeader.Add(i, "IdCard");
  79. }
  80. else if (listOrganizationStr.Exists(s => s == words))
  81. {
  82. dicHeader.Add(i, "Organization");
  83. }
  84. else if (listJobStr.Exists(s => s == words))
  85. {
  86. dicHeader.Add(i, "Job");
  87. }
  88. else if (listMobileStr.Exists(s => s == words))
  89. {
  90. dicHeader.Add(i, "Mobile");
  91. }
  92. else if (listAgeStr.Exists(s => s == words))
  93. {
  94. dicHeader.Add(i, "Age");
  95. }
  96. }
  97. List<BaiduClientOCRView> ClientArr = new List<BaiduClientOCRView>();
  98. for (int i = 1; i <= maxRowIndex; i++)
  99. {
  100. BaiduClientOCRView client = new BaiduClientOCRView();
  101. for (int j = 0; j < maxColIndex; j++)
  102. {
  103. var item = dataList.First(s => s.row_start == i && s.col_start == j);
  104. string words = item.words.Replace("\\n", "");
  105. string values = dicHeader[j];
  106. if (values.Equals("Name"))
  107. {
  108. if (Regex.Matches(words, "[a-zA-Z]").Count < 1)
  109. {
  110. string lastName = words.Substring(0, 1);
  111. string name = words.Substring(1);
  112. client.LastName = lastName;
  113. client.FirstName = name;
  114. string lastNamePinYin = lastName.GetTotalPingYin()[0].ToUpper();
  115. string firstNamePinYin = "";
  116. for (int n = 0; n < name.Length; n++)
  117. {
  118. firstNamePinYin += name.GetTotalPingYin()[0].ToUpper() + " ";
  119. }
  120. client.Pinyin = lastNamePinYin + "//"+ firstNamePinYin;
  121. }
  122. else
  123. {
  124. Regex regForeign = new Regex("[a-zA-Z]+[\\s][a-zA-Z]+");
  125. if (regForeign.IsMatch(words))
  126. {
  127. string[] names = words.Split(' ');
  128. client.LastName = names[1];
  129. client.FirstName = names[0];
  130. }
  131. else
  132. {
  133. client.FirstName = words;
  134. }
  135. }
  136. }
  137. else if (values.Equals("Sex"))
  138. {
  139. client.Sex = words;
  140. }
  141. else if (values.Equals("DOB"))
  142. {
  143. client.Birthday = words;
  144. DateTime time = new DateTime();
  145. if (DateTime.TryParse(client.Birthday, out time))
  146. {
  147. client.Birthday = time.ToString("yyyy-MM-dd");
  148. }
  149. else
  150. {
  151. client.Birthday = "";
  152. }
  153. }
  154. else if (values.Equals("IdCard"))
  155. {
  156. client.IDcard = words;
  157. }
  158. else if (values.Equals("Organization"))
  159. {
  160. client.CompanyFullName = words;
  161. }
  162. else if (values.Equals("Job"))
  163. {
  164. client.Job = words;
  165. }
  166. else if (values.Equals("Mobile"))
  167. {
  168. client.Phone = words;
  169. }
  170. }
  171. ClientArr.Add(client);
  172. }
  173. jw.Code = 200;
  174. jw.Msg = "获取成功!";
  175. jw.Data = ClientArr;
  176. }
  177. else if (json["error_code"] != null)
  178. {
  179. jw.Data = new string[0];
  180. jw.Msg = "百度接口异常!";
  181. }
  182. else
  183. {
  184. jw.Data = new string[0];
  185. jw.Msg = "百度接口异常!";
  186. }
  187. return Ok(jw);
  188. }
  189. }
  190. }