BaiduOCRController.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. try
  44. {
  45. string api_Response = BaiduApiHelper._apiTableV2(dto.base64img);
  46. JObject json = JObject.Parse(api_Response);
  47. if (json["tables_result"][0]["body"] != null)
  48. {
  49. List<BaiduOCR_TableV2> sourceList = JsonConvert.DeserializeObject<List<BaiduOCR_TableV2>>(json["tables_result"][0]["body"].ToString());
  50. List<BaiduOCR_TableV2> headerList = sourceList.Where(s => s.row_start == 0).OrderBy(s => s.col_start).Select(x => new BaiduOCR_TableV2
  51. {
  52. col_end = x.col_end,
  53. col_start = x.col_start,
  54. row_end = x.row_end,
  55. row_start = x.row_start,
  56. words = Regex.Replace(x.words, "[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\/;'\\\\[\\]·!#¥(——):;“”‘、,|《。》?、【】[\\]]", string.Empty)
  57. }).ToList();
  58. List<BaiduOCR_TableV2> dataList = sourceList.Where(s => s.row_start != 0).ToList();
  59. int maxRowIndex = dataList.Max(s => s.row_start);
  60. int maxColIndex = headerList.Max(s => s.col_start) + 1;
  61. Dictionary<int, string> dicHeader = new Dictionary<int, string>();
  62. List<string> listNameStr = new List<string>() { "姓名", "Name" };
  63. List<string> listSexStr = new List<string>() { "性别", "Gender" };
  64. List<string> listDOBStr = new List<string>() { "D.O.B", "出生年月", "生日" };
  65. List<string> listIdCard = new List<string>() { "身份证号码", "身份证" };
  66. List<string> listOrganizationStr = new List<string>() { "工作单位", "Organization", "单位" };
  67. List<string> listJobStr = new List<string>() { "职务", "Title", "职位" };
  68. List<string> listMobileStr = new List<string>() { "联系电话", "Mobile" };
  69. List<string> listAgeStr = new List<string>() { "年龄", "Age" };
  70. for (int i = 0; i < headerList.Count; i++)
  71. {
  72. string words = headerList[i].words.Trim();
  73. if (listNameStr.Exists(s => s == words))
  74. {
  75. dicHeader.Add(i, "Name");
  76. }
  77. else if (listSexStr.Exists(s => s == words))
  78. {
  79. dicHeader.Add(i, "Sex");
  80. }
  81. else if (listDOBStr.Exists(s => s == words))
  82. {
  83. dicHeader.Add(i, "DOB");
  84. }
  85. else if (listIdCard.Exists(s => s == words))
  86. {
  87. dicHeader.Add(i, "IdCard");
  88. }
  89. else if (listOrganizationStr.Exists(s => s == words))
  90. {
  91. dicHeader.Add(i, "Organization");
  92. }
  93. else if (listJobStr.Exists(s => s == words))
  94. {
  95. dicHeader.Add(i, "Job");
  96. }
  97. else if (listMobileStr.Exists(s => s == words))
  98. {
  99. dicHeader.Add(i, "Mobile");
  100. }
  101. else if (listAgeStr.Exists(s => s == words))
  102. {
  103. dicHeader.Add(i, "Age");
  104. }
  105. }
  106. List<BaiduClientOCRView> ClientArr = new List<BaiduClientOCRView>();
  107. for (int i = 1; i <= maxRowIndex; i++)
  108. {
  109. BaiduClientOCRView client = new BaiduClientOCRView();
  110. for (int j = 0; j < maxColIndex; j++)
  111. {
  112. var item = dataList.First(s => s.row_start == i && s.col_start == j);
  113. string words = item.words.Replace("\\n", "");
  114. string values = dicHeader[j];
  115. if (values.Equals("Name"))
  116. {
  117. if (Regex.Matches(words, "[a-zA-Z]").Count < 1)
  118. {
  119. string lastName = words.Substring(0, 1);
  120. string name = words.Substring(1);
  121. client.LastName = lastName;
  122. client.FirstName = name;
  123. string lastNamePinYin = lastName.GetTotalPingYin()[0].ToUpper();
  124. string firstNamePinYin = "";
  125. for (int n = 0; n < name.Length; n++)
  126. {
  127. firstNamePinYin += name[n].ToString().GetTotalPingYin()[0].ToUpper() + " ";
  128. }
  129. client.Pinyin = lastNamePinYin + "/" + firstNamePinYin;
  130. }
  131. else
  132. {
  133. Regex regForeign = new Regex("[a-zA-Z]+[\\s][a-zA-Z]+");
  134. if (regForeign.IsMatch(words))
  135. {
  136. string[] names = words.Split(' ');
  137. client.LastName = names[1];
  138. client.FirstName = names[0];
  139. }
  140. else
  141. {
  142. client.FirstName = words;
  143. }
  144. }
  145. }
  146. else if (values.Equals("Sex"))
  147. {
  148. client.Sex = words == "男" ? 0 : 1;
  149. }
  150. else if (values.Equals("DOB"))
  151. {
  152. client.Birthday = words;
  153. DateTime time = new DateTime();
  154. if (DateTime.TryParse(client.Birthday, out time))
  155. {
  156. client.Birthday = time.ToString("yyyy-MM-dd");
  157. }
  158. else
  159. {
  160. client.Birthday = "";
  161. }
  162. }
  163. else if (values.Equals("IdCard"))
  164. {
  165. client.IDcard = words;
  166. }
  167. else if (values.Equals("Organization"))
  168. {
  169. client.CompanyFullName = words;
  170. }
  171. else if (values.Equals("Job"))
  172. {
  173. client.Job = words;
  174. }
  175. else if (values.Equals("Mobile"))
  176. {
  177. client.Phone = words;
  178. }
  179. }
  180. ClientArr.Add(client);
  181. }
  182. jw.Code = 200;
  183. jw.Msg = "获取成功!";
  184. jw.Data = ClientArr;
  185. }
  186. else if (json["error_code"] != null)
  187. {
  188. jw.Data = new string[0];
  189. jw.Msg = "百度接口异常!";
  190. }
  191. else
  192. {
  193. jw.Data = new string[0];
  194. jw.Msg = "百度接口异常!";
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. jw.Code = 400;
  200. jw.Msg = "base64数据有误!";
  201. jw.Data = new string[0];
  202. }
  203. return Ok(jw);
  204. }
  205. }
  206. }