using OASystem.API.OAMethodLib;
using OASystem.API.OAMethodLib.BaiduApi;
using OASystem.API.OAMethodLib.TencentCloudAPI;
using OASystem.Domain.Dtos.Baidu;
using OASystem.Domain.ViewModels.OCR;
using Quartz.Util;
namespace OASystem.API.Controllers
{
///
/// 百度OCR识别
///
[Route("api/[controller]/[action]")]
[ApiController]
public class BaiduOCRController : ControllerBase
{
///
/// 接团客户OCR识别
///
///
///
[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();
try
{
string api_Response = BaiduApiHelper._apiTableV2(dto.base64img);
JObject json = JObject.Parse(api_Response);
if (json["tables_result"][0]["body"] != null)
{
List sourceList = JsonConvert.DeserializeObject>(json["tables_result"][0]["body"].ToString());
List headerList = sourceList.Where(s => s.row_start == 0).OrderBy(s => s.col_start).Select(x => new BaiduOCR_TableV2
{
col_end = x.col_end,
col_start = x.col_start,
row_end = x.row_end,
row_start = x.row_start,
words = Regex.Replace(x.words, "[`~!@#$%^&*()_\\-+=<>?:\"{}|,.\\/;'\\\\[\\]·!#¥(——):;“”‘、,|《。》?、【】[\\]]", string.Empty)
}).ToList();
List 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 dicHeader = new Dictionary();
List listNameStr = new List() { "姓名", "name", "姓 名" };
List listSexStr = new List() { "性别", "gender", "sex" };
List listDOBStr = new List() { "D.O.B", "出生年月", "生日", "出生日期", "出生年月日" };
List listIdCard = new List() { "身份证号码", "身份证", "身份证号" };
List listOrganizationStr = new List() { "工作单位", "organization", "单位", "所在单位" };
List listJobStr = new List() { "职务", "title", "职位", "所在单位及职务", "单位及职务" };
List listMobileStr = new List() { "联系电话", "mobile" };
List listAgeStr = new List() { "年龄", "age" };
List listBeiZhu = new List() { "备注", "本团职务" };
for (int i = 0; i < headerList.Count; i++)
{
string words = headerList[i].words.Trim().ToLower();
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");
}
else if (listBeiZhu.Exists(s => s == words))
{
dicHeader.Add(i, "BeiZhu");
}
}
List ClientArr = new List();
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 = string.Empty;
try
{
values = dicHeader[j];
}
catch (Exception)
{
throw new Exception("有未能识别的列名请检查 : " + JsonConvert.SerializeObject(headerList.Select(x => x.words)));
}
if (words.IsNullOrWhiteSpace())
{
continue;
}
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.FirstName = name;
string lastNamePinYin = lastName.GetTotalPingYin().Count > 0 ? lastName.GetTotalPingYin()[0].ToUpper() : "";
string firstNamePinYin = "";
for (int n = 0; n < name.Length; n++)
{
firstNamePinYin += name[n].ToString().GetTotalPingYin().Count > 0 ? name[n].ToString().GetTotalPingYin()[0].ToUpper() + " " : "";
}
client.Pinyin = lastNamePinYin + "/" + firstNamePinYin;
}
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.FirstName = names[0];
}
else
{
client.FirstName = words;
}
}
}
else if (values.Equals("Sex"))
{
client.Sex = words == "男" ? 0 : 1;
}
else if (values.Equals("DOB"))
{
client.Birthday = words.Replace(".", "-");
DateTime time = new DateTime();
if (DateTime.TryParse(client.Birthday, out time))
{
client.Birthday = time.ToString("yyyy-MM-dd");
}
else
{
client.Birthday = "";
}
}
else if (values.Equals("IdCard"))
{
client.IDcard = words;
}
else if (values.Equals("Organization"))
{
client.CompanyFullName = words;
}
else if (values.Equals("Job"))
{
client.Job = words;
}
else if (values.Equals("Mobile"))
{
client.Phone = words;
}
}
if (!(client.FirstName + client.LastName + client.Pinyin + client.CompanyFullName + client.Job + client.Phone + client.Birthday).IsNullOrWhiteSpace())
{
if (string.IsNullOrWhiteSpace(client.CompanyFullName))
{
client.CompanyFullName = "暂无";
}
if (!string.IsNullOrWhiteSpace(client.LastName + client.FirstName))
{
var pinyin = client.LastName.GetTotalPingYinFirst() + "/";
foreach (var cn in client.FirstName)
{
pinyin += cn.ToString().GetTotalPingYinFirst() + " ";
}
client.Pinyin = pinyin.TrimEnd();
}
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 = "百度接口异常!";
}
}
catch (Exception ex)
{
jw.Code = 400;
jw.Msg = "程序异常!" + ex.Message;
jw.Data = new string[0];
}
return Ok(jw);
}
}
}