BaiduOCRController.cs 7.9 KB

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