123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- using Autofac.Core;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using OASystem.API.OAMethodLib.BaiduApi;
- using OASystem.API.OAMethodLib.TencentCloudAPI;
- using OASystem.Domain.Dtos.Baidu;
- using OASystem.Domain.Dtos.CRM;
- using OASystem.Domain.Entities.Customer;
- using OASystem.Domain.ViewModels.OCR;
- namespace OASystem.API.Controllers
- {
- [Route("api/[controller]/[action]")]
- [ApiController]
- public class BaiduOCRController : ControllerBase
- {
- /// <summary>
- /// 接团客户OCR识别
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [HttpPost]
- [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
- public IActionResult ClientOCR(ClientOCRDto dto)
- {
- try
- {
- string[] picBase64Array = dto.base64img.Split(';');
- string picFormat = picBase64Array[0].Split('/')[1];
- if (!TencentOCRTools.ImageType(picFormat))
- {
- return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
- }
- }
- catch (Exception ex)
- {
- return Ok(JsonView("请上传图片base64编码数据!"));
- }
-
- JsonView jw = new JsonView();
- string api_Response = BaiduApiHelper._apiTableV2(dto.base64img);
- JObject json = JObject.Parse(api_Response);
- if (json["tables_result"][0]["body"] != null)
- {
-
- List<BaiduOCR_TableV2> sourceList = JsonConvert.DeserializeObject<List<BaiduOCR_TableV2>>(json["tables_result"][0]["body"].ToString());
- List<BaiduOCR_TableV2> headerList = sourceList.Where(s => s.row_start == 0).OrderBy(s => s.col_start).ToList();
- List<BaiduOCR_TableV2> dataList = sourceList.Where(s => s.row_start != 0).ToList();
- int maxRowIndex = dataList.Max(s => s.row_start);
- int maxColIndex = headerList.Max(s => s.col_start) + 1;
- Dictionary<int, string> dicHeader = new Dictionary<int, string>();
- List<string> listNameStr = new List<string>() { "姓名", "Name" };
- List<string> listSexStr = new List<string>() { "性别", "Gender" };
- List<string> listDOBStr = new List<string>() { "D.O.B", "出生年月" };
- List<string> listIdCard = new List<string>() { "身份证号码", "身份证" };
- List<string> listOrganizationStr = new List<string>() { "工作单位", "Organization", "单位" };
- List<string> listJobStr = new List<string>() { "职务", "Title" };
- List<string> listMobileStr = new List<string>() { "联系电话", "Mobile" };
- List<string> listAgeStr = new List<string>() { "年龄", "Age" };
- for (int i = 0; i < headerList.Count; i++)
- {
- string words = headerList[i].words.Trim();
- if (listNameStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "Name");
- }
- else if (listSexStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "Sex");
- }
- else if (listDOBStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "DOB");
- }
- else if (listIdCard.Exists(s => s == words))
- {
- dicHeader.Add(i, "IdCard");
- }
- else if (listOrganizationStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "Organization");
- }
- else if (listJobStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "Job");
- }
- else if (listMobileStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "Mobile");
- }
- else if (listAgeStr.Exists(s => s == words))
- {
- dicHeader.Add(i, "Age");
- }
- }
- List<BaiduClientOCRView> ClientArr = new List<BaiduClientOCRView>();
- for (int i = 1; i < maxRowIndex; i++)
- {
- BaiduClientOCRView client = new BaiduClientOCRView();
- for (int j = 0; j < maxColIndex; j++)
- {
- var item = dataList.First(s => s.row_start == i && s.col_start == j);
- string words = item.words.Replace("\\n", "");
- string values = dicHeader[j];
- if (values.Equals("Name"))
- {
- if (Regex.Matches(words, "[a-zA-Z]").Count < 1)
- {
- string lastName = words.Substring(0, 1);
- string name = words.Substring(1);
- client.LastName = lastName;
- client.Name = name;
- }
- else
- {
- Regex regForeign = new Regex("[a-zA-Z]+[\\s][a-zA-Z]+");
- if (regForeign.IsMatch(words))
- {
- string[] names = words.Split(' ');
- client.LastName = names[1];
- client.Name = names[0];
- }
- else
- {
- client.Name = words;
- }
- }
- }
- else if (values.Equals("Sex"))
- {
- client.Sex = words;
- }
- else if (values.Equals("DOB"))
- {
- client.Birthday = words;
- }
- else if (values.Equals("IdCard"))
- {
- client.IDcard = words;
- }
- else if (values.Equals("Organization"))
- {
- client.Company = words;
- }
- else if (values.Equals("Job"))
- {
- client.Job = words;
- }
- else if (values.Equals("Mobile"))
- {
- client.Phone = words;
- }
- }
- ClientArr.Add(client);
- }
- jw.Code = 200;
- jw.Msg = "获取成功!";
- jw.Data = ClientArr;
- }
- else if (json["error_code"] != null)
- {
- jw.Data = new string[0];
- jw.Msg = "百度接口异常!";
- }
- else
- {
- jw.Data = new string[0];
- jw.Msg = "百度接口异常!";
- }
- return Ok(jw);
- }
- }
- }
|