BaiduOCRController.cs 7.4 KB

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