TencentOCRController.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. using NPOI.XWPF.UserModel;
  2. using OASystem.API.OAMethodLib.File;
  3. using OASystem.API.OAMethodLib.TencentCloudAPI;
  4. using OASystem.API.OAMethodLib.YouDaoAPI;
  5. using OASystem.Domain.Dtos.CRM;
  6. using OASystem.Domain.Dtos.Tencent;
  7. using OASystem.Domain.ViewModels.OCR;
  8. using OASystem.Domain.ViewModels.TencentOCR;
  9. using OASystem.Infrastructure.Repositories.CRM;
  10. using TencentCloud.Ocr.V20181119.Models;
  11. namespace OASystem.API.Controllers
  12. {
  13. /// <summary>
  14. /// TencentOCR 识别
  15. /// </summary>
  16. [Route("api/[controller]/[action]")]
  17. //[ApiController]
  18. public class TencentOCRController : ControllerBase
  19. {
  20. private readonly VisaDeleClientRepository _visaDeleClientRepository;
  21. public TencentOCRController(VisaDeleClientRepository visaDeleClientRepository)
  22. {
  23. this._visaDeleClientRepository = visaDeleClientRepository;
  24. }
  25. #region web
  26. /// <summary>
  27. /// 通用印刷体
  28. /// 文档下载
  29. /// </summary>
  30. /// <param name="dto"></param>
  31. /// <returns></returns>
  32. [HttpPost]
  33. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  34. public async Task<IActionResult> GetGeneralBasic(GeneralBasicOCRDto dto)
  35. {
  36. var validator = new GeneralBasicOCRDtoFoalidator();
  37. var validatorRes = await validator.ValidateAsync(dto);
  38. if (!validatorRes.IsValid)
  39. {
  40. var errors = new StringBuilder();
  41. foreach (var valid in validatorRes.Errors)
  42. {
  43. errors.AppendLine(valid.ErrorMessage);
  44. }
  45. return Ok(JsonView(false, errors.ToString()));
  46. }
  47. var gbData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  48. if (gbData.Code != 0)
  49. {
  50. return Ok(JsonView(gbData.Msg));
  51. }
  52. if (gbData.Data == null)
  53. {
  54. return Ok(JsonView(gbData.Msg));
  55. }
  56. #region word下载
  57. GeneralBasicOCRResponse res = gbData.Data as GeneralBasicOCRResponse;
  58. string fileName = "通用印刷体[CH]";
  59. string content = $"";
  60. foreach (var view in res.TextDetections)
  61. {
  62. content += $"Content:{view.DetectedText} \n";
  63. }
  64. if (dto.language == 1) { }
  65. else if (dto.language == 2)
  66. {
  67. fileName = "通用印刷体[EN]";
  68. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  69. }
  70. string url = NPOICreateWord(fileName, content);
  71. #endregion
  72. return Ok(JsonView(true, "操作成功", new { dataSource = res, url = url }));
  73. }
  74. /// <summary>
  75. /// 身份证识别(国徽面/反面)
  76. /// 文档下载
  77. /// </summary>
  78. /// <param name="dto"></param>
  79. /// <returns></returns>
  80. [HttpPost]
  81. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  82. public async Task<IActionResult> GetIDCard(IDCardOCRDto dto)
  83. {
  84. var validator = new IDCardOCRDtoFoalidator();
  85. var validatorRes = await validator.ValidateAsync(dto);
  86. if (!validatorRes.IsValid)
  87. {
  88. var errors = new StringBuilder();
  89. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  90. return Ok(JsonView(false, errors.ToString()));
  91. }
  92. var idCardData = await TencentOCRTools.GetOCR((int)TencentOCREnum.IDCard, dto);
  93. if (idCardData.Code != 0)
  94. {
  95. return Ok(JsonView(idCardData.Msg));
  96. }
  97. if (idCardData.Data == null)
  98. {
  99. return Ok(JsonView(idCardData.Msg));
  100. }
  101. IDCardOCRAndDownUrlView iDCardOCRView = new IDCardOCRAndDownUrlView() { };
  102. if (dto.Language == 1)
  103. {
  104. bool cerdStatus = await _visaDeleClientRepository.SetCrmUpdPassIdCardOCR(
  105. new SetCrmUpdPassIdCardOCRDto
  106. {
  107. UserId = dto.UserId,
  108. ClientName = idCardData.Data.Name,
  109. Sex = idCardData.Data.Sex == "男" ? 0 : 1,
  110. CerdNo = idCardData.Data.IdNum,
  111. CerdAddress = idCardData.Data.Address,
  112. });
  113. iDCardOCRView.Status = cerdStatus;
  114. iDCardOCRView.Name = idCardData.Data.Name;
  115. iDCardOCRView.Sex = idCardData.Data.Sex;
  116. iDCardOCRView.Nation = idCardData.Data.Nation;
  117. iDCardOCRView.Birth = idCardData.Data.Birth;
  118. iDCardOCRView.Address = idCardData.Data.Address;
  119. iDCardOCRView.IdNum = idCardData.Data.IdNum;
  120. iDCardOCRView.Authority = idCardData.Data.Authority;
  121. iDCardOCRView.ValidDate = idCardData.Data.ValidDate;
  122. }
  123. else if (dto.Language == 2)
  124. {
  125. iDCardOCRView.Name = !string.IsNullOrEmpty(idCardData.Data.Name) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Name) : "";
  126. iDCardOCRView.Sex = !string.IsNullOrEmpty(idCardData.Data.Sex) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Sex) : "";
  127. iDCardOCRView.Nation = !string.IsNullOrEmpty(idCardData.Data.Nation) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Nation) : "";
  128. iDCardOCRView.Birth = !string.IsNullOrEmpty(idCardData.Data.Birth) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Birth) : "";
  129. iDCardOCRView.Address = !string.IsNullOrEmpty(idCardData.Data.Address) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Address) : "";
  130. iDCardOCRView.IdNum = idCardData.Data.IdNum;
  131. iDCardOCRView.Authority = !string.IsNullOrEmpty(idCardData.Data.Authority) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Authority) : "";
  132. iDCardOCRView.ValidDate = !string.IsNullOrEmpty(idCardData.Data.ValidDate) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.ValidDate) : "";
  133. }
  134. #region word生成 返回地址
  135. Dictionary<string, object> dic = new Dictionary<string, object>();
  136. dic.Add("Name", iDCardOCRView.Name);
  137. dic.Add("Sex", iDCardOCRView.Sex);
  138. dic.Add("Nation", iDCardOCRView.Nation);
  139. dic.Add("Birth", iDCardOCRView.Birth);
  140. dic.Add("Address", iDCardOCRView.Address);
  141. dic.Add("IdNum", iDCardOCRView.IdNum);
  142. dic.Add("Authority", iDCardOCRView.Authority);
  143. dic.Add("ValidDate", iDCardOCRView.ValidDate);
  144. string serverPath = "";
  145. if (dto.Language == 1)
  146. {
  147. string fileNameCh = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[CH]" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  148. serverPath = AsposeHelper.ExpertWordToModel("ocr_身份证(人像面).doc", "TencentOCR", fileNameCh, dic, null);
  149. }
  150. else if (dto.Language == 2)
  151. {
  152. string fileNameEn = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[EN]" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  153. serverPath = AsposeHelper.ExpertWordToModel("(EN)ocr_身份证(人像面).doc", "TencentOCR", fileNameEn, dic, null);
  154. }
  155. iDCardOCRView.DownUrl = serverPath;
  156. #endregion
  157. return Ok(JsonView(iDCardOCRView));
  158. }
  159. /// <summary>
  160. /// 户口簿识别
  161. /// 文档下载
  162. /// </summary>
  163. /// <param name="dto"></param>
  164. /// <returns></returns>
  165. [HttpPost]
  166. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  167. public async Task<IActionResult> GetResidenceBookletOCR(ResidenceBookletOCRDto dto)
  168. {
  169. var validator = new ResidenceBookletOCRDtoFoalidator();
  170. var validatorRes = validator.Validate(dto);
  171. if (!validatorRes.IsValid)
  172. {
  173. var errors = new StringBuilder();
  174. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  175. return Ok(JsonView(false, errors.ToString()));
  176. }
  177. var residenceBookData = await TencentOCRTools.GetOCR((int)TencentOCREnum.ResidenceBooklet, dto);
  178. if (residenceBookData.Code != 0)
  179. {
  180. return Ok(JsonView(residenceBookData.Msg));
  181. }
  182. if (residenceBookData.Data == null)
  183. {
  184. return Ok(JsonView(residenceBookData.Msg));
  185. }
  186. #region Feil Down
  187. ResidenceBookletOCRResponse res = residenceBookData.Data as ResidenceBookletOCRResponse;
  188. // 创建文档对象
  189. XWPFDocument doc = new XWPFDocument();
  190. XWPFParagraph p = doc.CreateParagraph();
  191. XWPFRun runText = p.CreateRun();
  192. string fileName = "户口簿识别[CH]";
  193. string content = $"{fileName}\n";
  194. if (res != null)
  195. {
  196. content += $"户号:{res.HouseholdNumber ?? "Unknown"}\n";
  197. content += $"姓名:{res.Name ?? "Unknown"}\n";
  198. content += $"曾用名:{res.FormerName ?? "Unknown"}\n";
  199. content += $"性别:{res.Sex ?? "Unknown"}\n";
  200. content += $"出生地:{res.BirthPlace ?? "Unknown"}\n";
  201. content += $"民族:{res.Nation ?? "Unknown"}\n";
  202. content += $"籍贯:{res.NativePlace ?? "Unknown"}\n";
  203. content += $"出生日期:{res.BirthDate ?? "Unknown"}\n";
  204. content += $"公民身份证件编号:{res.IdCardNumber ?? "Unknown"}\n";
  205. content += $"文化程度:{res.EducationDegree ?? "Unknown"}\n";
  206. content += $"服务处所:{res.ServicePlace ?? "Unknown"}\n";
  207. content += $"户别:{res.Household ?? "Unknown"}\n";
  208. content += $"住址:{res.Address ?? "Unknown"}\n";
  209. content += $"承办人签章文字:{res.Signature ?? "Unknown"}\n";
  210. content += $"签发日期:{res.IssueDate ?? "Unknown"}\n";
  211. content += $"户主页编号:{res.HomePageNumber ?? "Unknown"}\n";
  212. content += $"户主姓名:{res.HouseholderName ?? "Unknown"}\n";
  213. content += $"户主或与户主关系:{res.Relationship ?? "Unknown"}\n";
  214. content += $"本市(县)其他住址:{res.OtherAddresses ?? "Unknown"}\n";
  215. content += $"宗教信仰:{res.ReligiousBelief ?? "Unknown"}\n";
  216. content += $"身高:{res.Height ?? "Unknown"}\n";
  217. content += $"血型:{res.BloodType ?? "Unknown"}\n";
  218. content += $"婚姻状况:{res.MaritalStatus ?? "Unknown"}\n";
  219. content += $"兵役状况:{res.VeteranStatus ?? "Unknown"}\n";
  220. content += $"职业:{res.Profession ?? "Unknown"}\n";
  221. content += $"何时由何地迁来本市(县):{res.MoveToCityInformation ?? "Unknown"}\n";
  222. content += $"何时由何地迁来本址:{res.MoveToSiteInformation ?? "Unknown"}\n";
  223. content += $"登记日期:{res.RegistrationDate ?? "Unknown"}\n";
  224. }
  225. if (dto.language == 1) { }
  226. else if (dto.language == 2)
  227. {
  228. fileName = "户口簿识别[EN]";
  229. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  230. }
  231. setWrap(content, p, runText);
  232. runText.FontSize = 12;
  233. runText.SetFontFamily("宋体", FontCharRange.None); //设置宋体字体
  234. //工作流写入,通过流的方式进行创建生成文件
  235. MemoryStream stream = new MemoryStream();
  236. doc.Write(stream);
  237. string FilePath = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  238. if (!Directory.Exists(FilePath))
  239. {
  240. Directory.CreateDirectory(FilePath);
  241. }
  242. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  243. string sDate = DateTime.Now.ToString("yyyy年MM月dd日-HH时mm分ss秒-ffff");
  244. FilePath = FilePath + "/" + outputFile;
  245. FileStream Fs = new FileStream(FilePath, FileMode.OpenOrCreate);
  246. doc.Write(Fs);
  247. Fs.Close();
  248. string url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  249. var data = new
  250. {
  251. dataSource = residenceBookData.Data,
  252. url = url
  253. };
  254. #endregion
  255. return Ok(JsonView(data));
  256. }
  257. /// <summary>
  258. /// npoi 创建 Word
  259. /// </summary>
  260. /// <param name="typeName"></param>
  261. /// <param name="content"></param>
  262. /// <returns></returns>
  263. private string NPOICreateWord(string typeName, string content)
  264. {
  265. try
  266. {
  267. // 创建文档对象
  268. XWPFDocument doc = new XWPFDocument();
  269. XWPFParagraph p = doc.CreateParagraph();
  270. XWPFRun runText = p.CreateRun();
  271. string fileName = typeName;
  272. string content1 = $"{fileName}\n{content}";
  273. setWrap(content1, p, runText);
  274. runText.FontSize = 12;
  275. runText.SetFontFamily("宋体", FontCharRange.None); //设置宋体字体
  276. //工作流写入,通过流的方式进行创建生成文件
  277. MemoryStream stream = new MemoryStream();
  278. doc.Write(stream);
  279. string FilePath = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  280. if (!Directory.Exists(FilePath))
  281. {
  282. Directory.CreateDirectory(FilePath);
  283. }
  284. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  285. string sDate = DateTime.Now.ToString("yyyy年MM月dd日-HH时mm分ss秒-ffff");
  286. FilePath = FilePath + "/" + outputFile;
  287. FileStream Fs = new FileStream(FilePath, FileMode.OpenOrCreate);
  288. doc.Write(Fs);
  289. Fs.Close();
  290. return $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  291. }
  292. catch (Exception ex)
  293. {
  294. return ex.Message;
  295. }
  296. }
  297. /// <summary>
  298. /// npoi 换行
  299. /// </summary>
  300. /// <param name="value"></param>
  301. /// <param name="paragraph"></param>
  302. /// <param name="run"></param>
  303. private void setWrap(Object value, XWPFParagraph paragraph, XWPFRun run)
  304. {
  305. if (((String)value).IndexOf("\n") > 0)
  306. {
  307. //设置换行
  308. String[] text = value.ToString().Split("\n");
  309. for (int f = 0; f < text.Length; f++)
  310. {
  311. XWPFRun run1 = paragraph.CreateRun();
  312. if (f == 0)
  313. {
  314. //此处不缩进因为word模板已经缩进了。
  315. run1.SetText(text[f].Trim());
  316. }
  317. else
  318. {
  319. run1.AddCarriageReturn();//硬回车
  320. //注意:wps换行首行缩进是三个空格符,office要的话可以用 run.addTab();缩进或者四个空格符
  321. run1.SetText(text[f].Trim());
  322. }
  323. }
  324. }
  325. else
  326. {
  327. run.SetText((String)value);
  328. }
  329. }
  330. /// <summary>
  331. /// 营业执照识别
  332. /// 文档下载
  333. /// </summary>
  334. /// <param name="dto"></param>
  335. /// <returns></returns>
  336. [HttpPost]
  337. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  338. public async Task<IActionResult> GetBizLicenseOCR(BizLicenseOCRDto dto)
  339. {
  340. var validator = new BizLicenseOCRDtoFoalidator();
  341. var validatorRes = await validator.ValidateAsync(dto);
  342. if (!validatorRes.IsValid)
  343. {
  344. var errors = new StringBuilder();
  345. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  346. return Ok(JsonView(false, errors.ToString()));
  347. }
  348. var residenceBookData = await TencentOCRTools.GetOCR((int)TencentOCREnum.BizLicense, dto);
  349. if (residenceBookData.Code != 0)
  350. {
  351. return Ok(JsonView(residenceBookData.Msg));
  352. }
  353. if (residenceBookData.Data == null)
  354. {
  355. return Ok(JsonView(residenceBookData.Msg));
  356. }
  357. BizLicenseOCRResponse dataInfo = residenceBookData.Data as BizLicenseOCRResponse;
  358. string fileName = "营业执照识别[CH]";
  359. string content = "";
  360. if (dataInfo != null)
  361. {
  362. content += $"统一社会信用代码(三合一之前为注册号):{dataInfo.RegNum}\n";
  363. content += $"公司名称:{dataInfo.Name}\n";
  364. content += $"注册资本:{dataInfo.Capital}\n";
  365. content += $"法定代表人:{dataInfo.Person}\n";
  366. content += $"地址:{dataInfo.Address} \n";
  367. content += $"经营范围:{dataInfo.Business}\n";
  368. content += $"主体类型:{dataInfo.Type} \n";
  369. content += $"营业期限:{dataInfo.Period}\n";
  370. content += $"组成形式:{dataInfo.ComposingForm}\n";
  371. content += $"成立日期:{dataInfo.SetDate}\n";
  372. }
  373. if (dto.language == 2)
  374. {
  375. fileName = "营业执照识别[EN]";
  376. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  377. }
  378. string url = NPOICreateWord(fileName, content);
  379. var data = new
  380. {
  381. dataSource = residenceBookData.Data,
  382. url = url
  383. };
  384. return Ok(JsonView(data));
  385. }
  386. /// <summary>
  387. /// 组织结构识别
  388. /// 文档下载
  389. /// </summary>
  390. /// <param name="dto"></param>
  391. /// <returns></returns>
  392. [HttpPost]
  393. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  394. public async Task<IActionResult> GetOrgCodeCertOCR(OrgCodeCertOCRDto dto)
  395. {
  396. var validator = new OrgCodeCertOCRDtoFoalidator();
  397. var validatorRes = await validator.ValidateAsync(dto);
  398. if (!validatorRes.IsValid)
  399. {
  400. var errors = new StringBuilder();
  401. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  402. return Ok(JsonView(false, errors.ToString()));
  403. }
  404. var occData = await TencentOCRTools.GetOCR((int)TencentOCREnum.OrgCodeCert, dto);
  405. if (occData.Code != 0)
  406. {
  407. return Ok(JsonView(occData.Msg));
  408. }
  409. if (occData.Data == null)
  410. {
  411. return Ok(JsonView(occData.Msg));
  412. }
  413. string fileName = "组织结构识别[CH]";
  414. string content = "";
  415. OrgCodeCertOCRResponse occInfo = occData.Data as OrgCodeCertOCRResponse;
  416. if (occInfo != null)
  417. {
  418. content += $"代码:{occInfo.OrgCode}\n";
  419. content += $"机构名称:{occInfo.Name}\n";
  420. content += $"地址:{occInfo.Address}\n";
  421. content += $"有效期:{occInfo.ValidDate}\n";
  422. }
  423. if (dto.language == 2) { fileName = "组织结构识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); }
  424. string url = NPOICreateWord(fileName, content);
  425. var data = new
  426. {
  427. dataSource = occInfo,
  428. url = url
  429. };
  430. return Ok(JsonView(data));
  431. }
  432. /// <summary>
  433. /// 行驶证识别
  434. /// 文档下载
  435. /// CardSide=0 主页正面(有红色印章的一面),CardSide=1 行驶证副页正面(有号码号牌的一面),CardSide=2 行驶证主页正面和副页正面。
  436. /// </summary>
  437. /// <param name="dto"></param>
  438. /// <returns></returns>
  439. [HttpPost]
  440. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  441. public async Task<IActionResult> GetVehicleLicense(VehicleLicenseOCRDto dto)
  442. {
  443. var validator = new VehicleLicenseOCRDtoFoalidator();
  444. var validatorRes = await validator.ValidateAsync(dto);
  445. if (!validatorRes.IsValid)
  446. {
  447. var errors = new StringBuilder();
  448. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  449. return Ok(JsonView(false, errors.ToString()));
  450. }
  451. var vehicleLicenseData = TencentOCRTools.GetOCR((int)TencentOCREnum.VehicleLicense, dto);
  452. if (vehicleLicenseData.Result.Code != 0)
  453. {
  454. return Ok(JsonView(vehicleLicenseData.Result.Msg));
  455. }
  456. if (vehicleLicenseData.Result.Data == null)
  457. {
  458. return Ok(JsonView(vehicleLicenseData.Result.Msg));
  459. }
  460. #region word下载
  461. VehicleLicenseOCRResponse vlData = vehicleLicenseData.Result.Data as VehicleLicenseOCRResponse;
  462. string url = "";
  463. string fileName = "";
  464. string content = "";
  465. dynamic data = null;
  466. //Aspose.Words.License lic = new Aspose.Words.License();
  467. //lic.SetLicense("Aspose.Wrods.lic");//破解版不用设置license
  468. if (dto.CardSide == 0) // 正面
  469. {
  470. fileName = "行驶证识别-主页[CH]";
  471. TextVehicleFront frontInfo = vlData.FrontInfo;
  472. if (frontInfo != null)
  473. {
  474. data = frontInfo;
  475. content += $"号牌号码:{frontInfo.PlateNo ?? "Unknown"}\n";
  476. content += $"车辆类型:{frontInfo.VehicleType ?? "Unknown"}\n";
  477. content += $"所有人:{frontInfo.Owner ?? "Unknown"}\n";
  478. content += $"住址:{frontInfo.Address ?? "Unknown"}\n";
  479. content += $"使用性质:{frontInfo.UseCharacter ?? "Unknown"}\n";
  480. content += $"品牌型号:{frontInfo.Model ?? "Unknown"}\n";
  481. content += $"车辆识别代号:{frontInfo.Vin ?? "Unknown"}\n";
  482. content += $"发动机号码:{frontInfo.EngineNo ?? "Unknown"}\n";
  483. content += $"注册日期:{frontInfo.RegisterDate ?? "Unknown"}\n";
  484. content += $"发证日期:{frontInfo.IssueDate ?? "Unknown"}\n";
  485. content += $"印章:{frontInfo.Seal ?? "Unknown"}\n";
  486. }
  487. if (dto.language == 2)
  488. {
  489. fileName = "行驶证识别-主页[EN]";
  490. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  491. }
  492. url = NPOICreateWord(fileName, content);
  493. }
  494. else if (dto.CardSide == 1) //反面
  495. {
  496. TextVehicleBack backInfo = vlData.BackInfo;
  497. fileName = "行驶证识别-副页[CH]";
  498. if (backInfo != null)
  499. {
  500. data = backInfo;
  501. content += $"号牌号码:{backInfo.PlateNo ?? "Unknown"}\n";
  502. content += $"档案编号:{backInfo.FileNo ?? "Unknown"}\n";
  503. content += $"核定人数:{backInfo.AllowNum ?? "Unknown"}\n";
  504. content += $"总质量:{backInfo.TotalMass ?? "Unknown"}\n";
  505. content += $"整备质量:{backInfo.CurbWeight ?? "Unknown"}\n";
  506. content += $"核定载质量:{backInfo.LoadQuality ?? "Unknown"}\n";
  507. content += $"外廓尺寸:{backInfo.ExternalSize ?? "Unknown"}\n";
  508. content += $"备注:{backInfo.Marks ?? "Unknown"}\n";
  509. content += $"检验记录:{backInfo.Record ?? "Unknown"}\n";
  510. content += $"准牵引总质量:{backInfo.TotalQuasiMass ?? "Unknown"}\n";
  511. }
  512. if (dto.language == 2)
  513. {
  514. fileName = "行驶证识别-副页[EN]";
  515. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  516. }
  517. url = NPOICreateWord(fileName, content);
  518. }
  519. #endregion
  520. return Ok(JsonView(true, "操作成功", new { dataSource = data, url = url }));
  521. }
  522. /// <summary>
  523. /// 房产证识别
  524. /// 文档下载
  525. /// </summary>
  526. /// <param name="dto"></param>
  527. /// <returns></returns>
  528. [HttpPost]
  529. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  530. public async Task<IActionResult> GetPropOwnerCert(PropOwnerCertOCRDto dto)
  531. {
  532. var validator = new PropOwnerCertOCRDtoFoalidator();
  533. var validatorRes = await validator.ValidateAsync(dto);
  534. if (!validatorRes.IsValid)
  535. {
  536. var errors = new StringBuilder();
  537. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  538. return Ok(JsonView(false, errors.ToString()));
  539. }
  540. var pcData = await TencentOCRTools.GetOCR((int)TencentOCREnum.PropOwnerCert, dto);
  541. if (pcData.Code != 0)
  542. {
  543. return Ok(JsonView(pcData.Msg));
  544. }
  545. if (pcData.Data == null)
  546. {
  547. return Ok(JsonView(pcData.Msg));
  548. }
  549. PropOwnerCertOCRResponse pocInfo = pcData.Data as PropOwnerCertOCRResponse;
  550. string fileName = "房产证识别[CH]";
  551. string content = "";
  552. if (pocInfo != null)
  553. {
  554. content += $"房地产权利人:{pocInfo.Owner}\n";
  555. content += $"共有情况:{pocInfo.Possession}\n";
  556. content += $"登记时间:{pocInfo.RegisterTime}\n";
  557. content += $"规划用途:{pocInfo.Purpose}\n";
  558. content += $"房屋性质:{pocInfo.Nature}\n";
  559. content += $"房地坐落:{pocInfo.Location}\n";
  560. }
  561. if (dto.language == 2)
  562. {
  563. fileName = "房产证识别[EN]";
  564. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  565. }
  566. string url = NPOICreateWord(fileName, content);
  567. return Ok(JsonView(new { dataSource = pocInfo, url = url }));
  568. }
  569. /// <summary>
  570. /// 结婚证识别
  571. /// 文档下载
  572. /// </summary>
  573. /// <param name="dto"></param>
  574. /// <returns></returns>
  575. [HttpPost]
  576. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  577. public async Task<IActionResult> GetMarriageLicense(GeneralBasicOCRDto dto)
  578. {
  579. var validator = new GeneralBasicOCRDtoFoalidator();
  580. var validatorRes = await validator.ValidateAsync(dto);
  581. if (!validatorRes.IsValid)
  582. {
  583. var errors = new StringBuilder();
  584. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  585. return Ok(JsonView(false, errors.ToString()));
  586. }
  587. var mlData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  588. if (mlData.Code != 0)
  589. {
  590. return Ok(JsonView(mlData.Msg));
  591. }
  592. if (mlData.Data == null)
  593. {
  594. return Ok(JsonView(mlData.Msg));
  595. }
  596. string fileName = "结婚证识别[CH]";
  597. string content = "";
  598. GeneralBasicOCRResponse data = mlData.Data as GeneralBasicOCRResponse;
  599. List<TextDetection> infos = data.TextDetections.ToList();
  600. foreach (var item in infos)
  601. {
  602. content += $"Content:{item.DetectedText}\n";
  603. }
  604. if (dto.language == 2)
  605. {
  606. fileName = "结婚证识别[EN]";
  607. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  608. }
  609. string url = NPOICreateWord(fileName, content);
  610. return Ok(JsonView(new { dataSource = infos, url = url }));
  611. }
  612. /// <summary>
  613. /// 统一信用代码识别
  614. /// 文档下载
  615. /// </summary>
  616. /// <param name="dto"></param>
  617. /// <returns></returns>
  618. [HttpPost]
  619. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  620. public async Task<IActionResult> GetUniformCreditCode(GeneralBasicOCRDto dto)
  621. {
  622. var validator = new GeneralBasicOCRDtoFoalidator();
  623. var validatorRes = await validator.ValidateAsync(dto);
  624. if (!validatorRes.IsValid)
  625. {
  626. var errors = new StringBuilder();
  627. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  628. return Ok(JsonView(false, errors.ToString()));
  629. }
  630. var gbData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  631. if (gbData.Code != 0)
  632. {
  633. return Ok(JsonView(gbData.Msg));
  634. }
  635. if (gbData.Data == null)
  636. {
  637. return Ok(JsonView(gbData.Msg));
  638. }
  639. string fileName = "统一信用代码识别[CH]";
  640. string content = "";
  641. GeneralBasicOCRResponse data = gbData.Data as GeneralBasicOCRResponse;
  642. List<TextDetection> infos = data.TextDetections.ToList();
  643. foreach (var item in infos)
  644. {
  645. content += $"Content:{item.DetectedText}\n";
  646. }
  647. if (dto.language == 2)
  648. {
  649. fileName = "统一信用代码识别[EN]";
  650. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  651. }
  652. string url = NPOICreateWord(fileName, content);
  653. return Ok(JsonView(new { dataSource = infos, url = url }));
  654. }
  655. /// <summary>
  656. /// 护照识别(中国大陆地区护照)
  657. /// 文档下载
  658. /// </summary>
  659. /// <param name="dto"></param>
  660. /// <returns></returns>
  661. [HttpPost]
  662. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  663. public async Task<IActionResult> GetPassport(PassportOCRDto dto)
  664. {
  665. var validator = new PassportOCRDtoFoalidator();
  666. var validatorRes = await validator.ValidateAsync(dto);
  667. if (!validatorRes.IsValid)
  668. {
  669. var errors = new StringBuilder();
  670. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  671. return Ok(JsonView(false, errors.ToString()));
  672. }
  673. var pData = await TencentOCRTools.GetOCR((int)TencentOCREnum.Passport, dto);
  674. if (pData.Code != 0)
  675. {
  676. return Ok(JsonView(pData.Msg));
  677. }
  678. string url = "";
  679. #region 文档下载
  680. string fileName = "护照识别(中国大陆地区护照)[CH]";
  681. string content = $"";
  682. PassportOCRResponse pInfo = pData.Data as PassportOCRResponse;
  683. if (pInfo != null)
  684. {
  685. content += $"国籍:{pInfo.Nationality ?? "Unknown"}\n";
  686. content += $"国家码:{pInfo.Country ?? "Unknown"}\n";
  687. content += $"护照号:{pInfo.PassportNo ?? "Unknown"}\n";
  688. content += $"姓名:{pInfo.Name ?? "Unknown"}\n";
  689. content += $"姓:{pInfo.FamilyName ?? "Unknown"}\n";
  690. content += $"名:{pInfo.FirstName ?? "Unknown"}\n";
  691. content += $"性别:{pInfo.Sex ?? "Unknown"}\n";
  692. content += $"出生日期:{pInfo.BirthDate ?? "Unknown"}\n";
  693. content += $"签发日期:{pInfo.IssueDate ?? "Unknown"}\n";
  694. content += $"签发地点:{pInfo.IssuePlace ?? "Unknown"}\n";
  695. content += $"有效期:{pInfo.ExpiryDate ?? "Unknown"}\n";
  696. content += $"持证人签名:{pInfo.Signature ?? "Unknown"}\n";
  697. content += $"最下方第一行 MRZ Code 序列:{pInfo.CodeSet ?? "Unknown"}\n";
  698. content += $"最下方第二行 MRZ Code 序列:{pInfo.CodeCrc ?? "Unknown"}\n";
  699. }
  700. if (dto.language == 2)
  701. {
  702. fileName = "护照识别(中国大陆地区护照)[EN]";
  703. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  704. }
  705. url = NPOICreateWord(fileName, content);
  706. #endregion
  707. return Ok(JsonView(true, "操作成功", new { dataSource = pInfo, url = url }));
  708. }
  709. /// <summary>
  710. /// 护照识别(港澳台地区及境外护照)
  711. /// 文档下载
  712. /// </summary>
  713. /// <param name="dto"></param>
  714. /// <returns></returns>
  715. [HttpPost]
  716. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  717. public async Task<IActionResult> GetMLIDPassport(MLIDPassportOCRDto dto)
  718. {
  719. var validator = new MLIDPassportOCRDtoFoavalidator();
  720. var validatorRes = await validator.ValidateAsync(dto);
  721. if (!validatorRes.IsValid)
  722. {
  723. var errors = new StringBuilder();
  724. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  725. return Ok(JsonView(false, errors.ToString()));
  726. }
  727. var mlidpData = await TencentOCRTools.GetOCR((int)TencentOCREnum.MLIDPassport, dto);
  728. if (mlidpData.Code != 0)
  729. {
  730. return Ok(JsonView(mlidpData.Msg));
  731. }
  732. if (mlidpData.Data == null)
  733. {
  734. return Ok(JsonView(mlidpData.Msg));
  735. }
  736. string url = "";
  737. #region 文档下载
  738. string fileName = "护照识别(港澳台地区及境外护照)[CH]";
  739. string content = $"";
  740. MLIDPassportOCRResponse mlidInfo = mlidpData.Data as MLIDPassportOCRResponse;
  741. if (mlidInfo != null)
  742. {
  743. content += $"护照ID:{mlidInfo.ID ?? "Unknown"}\n";
  744. content += $"姓名:{mlidInfo.Name ?? "Unknown"}\n";
  745. content += $"出生日期:{mlidInfo.DateOfBirth ?? "Unknown"}\n";
  746. content += $"性别(F女,M男):{mlidInfo.Sex ?? "Unknown"}\n";
  747. content += $"有效期:{mlidInfo.DateOfExpiration ?? "Unknown"}\n";
  748. content += $"发行国:{mlidInfo.IssuingCountry ?? "Unknown"}\n";
  749. content += $"国家地区代码:{mlidInfo.Nationality ?? "Unknown"}\n";
  750. }
  751. if (dto.language == 2)
  752. {
  753. fileName = "护照识别(港澳台地区及境外护照)[EN]";
  754. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  755. }
  756. url = NPOICreateWord(fileName, content);
  757. #endregion
  758. return Ok(JsonView(true, "操作成功", new { dataSource = mlidInfo, url = url }));
  759. }
  760. /// <summary>
  761. /// 名片识别
  762. /// 文档下载
  763. /// </summary>
  764. /// <param name="dto"></param>
  765. /// <returns></returns>
  766. [HttpPost]
  767. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  768. public async Task<IActionResult> GetBusinessCard(BusinessCardOCRDto dto)
  769. {
  770. var validator = new BusinessCardOCRDtoFoavalidator();
  771. var validatorRes = await validator.ValidateAsync(dto);
  772. if (!validatorRes.IsValid)
  773. {
  774. var errors = new StringBuilder();
  775. foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage);
  776. return Ok(JsonView(false, errors.ToString()));
  777. }
  778. var mlidpData = await TencentOCRTools.GetOCR((int)TencentOCREnum.BusinessCard, dto);
  779. if (mlidpData.Code != 0)
  780. {
  781. return Ok(JsonView(mlidpData.Msg));
  782. }
  783. if (mlidpData.Data == null)
  784. {
  785. return Ok(JsonView(mlidpData.Msg));
  786. }
  787. string url = "";
  788. #region 文档下载
  789. string fileName = "名片识别[CH]";
  790. string content = $"";
  791. BusinessCardOCRResponse bcData = mlidpData.Data as BusinessCardOCRResponse;
  792. List<BusinessCardInfo> bcInfos = new List<BusinessCardInfo>();
  793. bcInfos = bcData.BusinessCardInfos.ToList();
  794. foreach (var view in bcInfos)
  795. {
  796. content += $"TypeName:{view?.Name ?? "Unknown"}\nContent:{view?.Value ?? "Unknown"}\n";
  797. }
  798. if (dto.language == 2)
  799. {
  800. fileName = "名片识别[EN]";
  801. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  802. }
  803. url = NPOICreateWord(fileName, content);
  804. #endregion
  805. //return Ok(JsonView(true, "操作成功", new { dataSource = bcData, url = url }));
  806. return Ok(JsonView(true, "操作成功", new { businessCardInfos = bcData.BusinessCardInfos, url = url }));
  807. }
  808. /// <summary>
  809. /// 智能结构化识别V2
  810. /// 文档下载
  811. /// </summary>
  812. /// <param name="dto"></param>
  813. /// <returns></returns>
  814. [HttpPost]
  815. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  816. public async Task<IActionResult> GetSmartStructuralOCRV2(BusinessCardOCRDto dto)
  817. {
  818. var validator = new BusinessCardOCRDtoFoavalidator();
  819. var validatorRes = await validator.ValidateAsync(dto);
  820. if (!validatorRes.IsValid)
  821. {
  822. var errors = new StringBuilder();
  823. foreach (var valid in validatorRes.Errors) { errors.AppendLine(valid.ErrorMessage); }
  824. return Ok(JsonView(false, errors.ToString()));
  825. }
  826. var ssData = await TencentOCRTools.GetOCR((int)TencentOCREnum.SmartStructuralOCRV2, dto);
  827. if (ssData.Code != 0)
  828. {
  829. return Ok(JsonView(false, ssData.Msg));
  830. }
  831. if (ssData.Data == null)
  832. {
  833. return Ok(JsonView(false, ssData.Msg));
  834. }
  835. string fileName = "智能结构化识别V2[CH]";
  836. List<ocrGeneralView>? datas = ssData.Data as List<ocrGeneralView>;
  837. string content = $"";
  838. foreach (var view in datas)
  839. {
  840. content += $"TypeName:{view.name}\nContent:{view.value}\n";
  841. }
  842. if (dto.language == 2)
  843. {
  844. fileName = "智能结构化识别V2[EN]";
  845. content = await YouDaoApiTools.GetOCR_ReTrans(content);
  846. }
  847. string url = NPOICreateWord(fileName, content);
  848. var data = new
  849. {
  850. dataSource = ssData.Data,
  851. url = url
  852. };
  853. return Ok(JsonView(true, ssData.Msg, data));
  854. }
  855. #endregion
  856. #region android ios
  857. /// <summary>
  858. /// 名片识别
  859. /// 获取名片 移动端 返回数据
  860. /// </summary>
  861. /// <param name="dto"></param>
  862. /// <returns></returns>
  863. [HttpPost]
  864. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  865. public async Task<IActionResult> PostBusinessCardApp(BusinessCardOCRDto dto)
  866. {
  867. double strSize = 1024 * 1024 * 7;
  868. if (dto.picBase64.Length > strSize)
  869. {
  870. return Ok(JsonView("图片不能大于7M!"));
  871. }
  872. Logs(dto.picBase64);
  873. Logs("名片识别开始时间:" + DateTime.Now);
  874. try
  875. {
  876. var mlidpData = TencentOCRTools.PostOCRApp((int)TencentOCREnum.BusinessCard, dto);
  877. if (mlidpData.Code != 0)
  878. {
  879. return Ok(JsonView(mlidpData.Msg));
  880. }
  881. if (mlidpData.Data == null)
  882. {
  883. return Ok(JsonView(mlidpData.Msg));
  884. }
  885. Logs("名片识别结束时间:" + DateTime.Now);
  886. return Ok(JsonView(mlidpData.Data));
  887. }
  888. catch (Exception ex)
  889. {
  890. return Ok(JsonView(false, ex.Message));
  891. }
  892. }
  893. #endregion
  894. }
  895. }