TencentOCRController.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. using Aspose.Cells;
  2. using Aspose.Words;
  3. using Aspose.Words.Fonts;
  4. using Microsoft.AspNetCore.Mvc.ViewEngines;
  5. using NPOI.HPSF;
  6. using OASystem.API.OAMethodLib.File;
  7. //using OASystem.API.OAMethodLib.NPOI;
  8. using OASystem.API.OAMethodLib.TencentCloudAPI;
  9. using OASystem.API.OAMethodLib.YouDaoAPI;
  10. using OASystem.Domain.Dtos.CRM;
  11. using OASystem.Domain.Dtos.QiYeWeChat;
  12. using OASystem.Domain.Dtos.Tencent;
  13. using OASystem.Domain.ViewModels.OCR;
  14. using OASystem.Domain.ViewModels.QiYeWeChat;
  15. using OASystem.Domain.ViewModels.TencentOCR;
  16. using OASystem.Infrastructure.Repositories.CRM;
  17. using Org.BouncyCastle.Crypto;
  18. using Org.BouncyCastle.Utilities.Encoders;
  19. using StackExchange.Redis;
  20. using System.Collections.Generic;
  21. using System.Data;
  22. using System.Net.NetworkInformation;
  23. using TencentCloud.Ocr.V20181119.Models;
  24. using Ubiety.Dns.Core;
  25. namespace OASystem.API.Controllers
  26. {
  27. /// <summary>
  28. /// TencentOCR 识别
  29. /// </summary>
  30. [Route("api/[controller]/[action]")]
  31. //[ApiController]
  32. public class TencentOCRController : ControllerBase
  33. {
  34. private readonly VisaDeleClientRepository _visaDeleClientRepository;
  35. public TencentOCRController(VisaDeleClientRepository visaDeleClientRepository)
  36. {
  37. this._visaDeleClientRepository = visaDeleClientRepository;
  38. }
  39. #region web
  40. /// <summary>
  41. /// 通用印刷体
  42. /// 文档下载
  43. /// </summary>
  44. /// <param name="dto"></param>
  45. /// <returns></returns>
  46. [HttpPost]
  47. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  48. public async Task<IActionResult> GetGeneralBasic(GeneralBasicOCRDto dto)
  49. {
  50. string[] picBase64Array = dto.picBase64.Split(';');
  51. string picFormat = picBase64Array[0].Split('/')[1];
  52. if (!TencentOCRTools.ImageType(picFormat))
  53. {
  54. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  55. }
  56. double strSize = 1024 * 1024 * 7;
  57. if (dto.picBase64.Length > strSize)
  58. {
  59. return Ok(JsonView("图片不能大于7M!"));
  60. }
  61. var gbData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  62. if (gbData.Code != 0)
  63. {
  64. return Ok(JsonView(gbData.Msg));
  65. }
  66. if (gbData.Data == null)
  67. {
  68. return Ok(JsonView(gbData.Msg));
  69. }
  70. #region word下载
  71. GeneralBasicOCRResponse res = gbData.Data as GeneralBasicOCRResponse;
  72. Document doc = new Document();
  73. DocumentBuilder builder = new DocumentBuilder(doc);
  74. string fileName = "通用印刷体";
  75. string content = $"{fileName}\r\n\r\n";
  76. foreach (var view in res.TextDetections)
  77. {
  78. content += $"Content:{view.DetectedText}\r\n\r\n";
  79. }
  80. builder.Write(content);
  81. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  82. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  83. string outputPath = Path.Combine(outputDir, outputFile);
  84. if (!Directory.Exists(outputDir))
  85. {
  86. Directory.CreateDirectory(outputDir);
  87. }
  88. doc.Save(outputPath);
  89. string url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  90. #endregion
  91. return Ok(JsonView(true, "操作成功", new { dataSource = res, url = url }));
  92. }
  93. /// <summary>
  94. /// 身份证识别(国徽面/反面)
  95. /// 文档下载
  96. /// </summary>
  97. /// <param name="dto"></param>
  98. /// <returns></returns>
  99. [HttpPost]
  100. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  101. public async Task<IActionResult> GetIDCard(IDCardOCRDto dto)
  102. {
  103. string[] picBase64Array = dto.picBase64.Split(';');
  104. string picFormat = picBase64Array[0].Split('/')[1];
  105. if (!TencentOCRTools.ImageType(picFormat))
  106. {
  107. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  108. }
  109. double strSize = 1024 * 1024 * 7;
  110. if (dto.picBase64.Length > strSize)
  111. {
  112. return Ok(JsonView("图片不能大于7M!"));
  113. }
  114. var idCardData =await TencentOCRTools.GetOCR((int)TencentOCREnum.IDCard, dto);
  115. if (idCardData.Code != 0)
  116. {
  117. return Ok(JsonView(idCardData.Msg));
  118. }
  119. if (idCardData.Data == null)
  120. {
  121. return Ok(JsonView(idCardData.Msg));
  122. }
  123. IDCardOCRAndDownUrlView iDCardOCRView = new IDCardOCRAndDownUrlView() { };
  124. if (dto.Language.ToLower() == "ch")
  125. {
  126. bool cerdStatus = await _visaDeleClientRepository.SetCrmUpdPassIdCardOCR(
  127. new SetCrmUpdPassIdCardOCRDto
  128. {
  129. UserId = dto.UserId,
  130. ClientName = idCardData.Data.Name,
  131. Sex = idCardData.Data.Sex == "男" ? 0 : 1,
  132. CerdNo = idCardData.Data.IdNum,
  133. CerdAddress = idCardData.Data.Address,
  134. });
  135. iDCardOCRView.Status = cerdStatus;
  136. iDCardOCRView.Name = idCardData.Data.Name;
  137. iDCardOCRView.Sex = idCardData.Data.Sex;
  138. iDCardOCRView.Nation = idCardData.Data.Nation;
  139. iDCardOCRView.Birth = idCardData.Data.Birth;
  140. iDCardOCRView.Address = idCardData.Data.Address;
  141. iDCardOCRView.IdNum = idCardData.Data.IdNum;
  142. iDCardOCRView.Authority = idCardData.Data.Authority;
  143. iDCardOCRView.ValidDate = idCardData.Data.ValidDate;
  144. }
  145. else if (dto.Language.ToLower() == "en")
  146. {
  147. iDCardOCRView.Name = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Name);
  148. iDCardOCRView.Sex = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Sex);
  149. iDCardOCRView.Nation = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Nation);
  150. iDCardOCRView.Birth = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Birth);
  151. iDCardOCRView.Address = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Address);
  152. iDCardOCRView.IdNum = idCardData.Data.IdNum;
  153. iDCardOCRView.Authority = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Authority);
  154. iDCardOCRView.ValidDate = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.ValidDate);
  155. }
  156. #region word生成 返回地址
  157. Dictionary<string, object> dic = new Dictionary<string, object>();
  158. dic.Add("Name", iDCardOCRView.Name);
  159. dic.Add("Sex", iDCardOCRView.Sex);
  160. dic.Add("Nation", iDCardOCRView.Nation);
  161. dic.Add("Birth", iDCardOCRView.Birth);
  162. dic.Add("Address", iDCardOCRView.Address);
  163. dic.Add("IdNum", iDCardOCRView.IdNum);
  164. dic.Add("Authority", iDCardOCRView.Authority);
  165. dic.Add("ValidDate", iDCardOCRView.ValidDate);
  166. string serverPath = "";
  167. if (dto.Language.ToLower() == "ch")
  168. {
  169. string fileNameCh = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[CH]" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  170. serverPath = AsposeHelper.ExpertWordToModel("ocr_身份证(人像面).doc", "TencentOCR", fileNameCh, dic, null);
  171. }
  172. else if (dto.Language.ToLower() == "en")
  173. {
  174. string fileNameEn = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[EN]" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  175. serverPath = AsposeHelper.ExpertWordToModel("(EN)ocr_身份证(人像面).doc", "TencentOCR", fileNameEn, dic, null);
  176. }
  177. iDCardOCRView.DownUrl = serverPath;
  178. #endregion
  179. return Ok(JsonView(iDCardOCRView));
  180. }
  181. /// <summary>
  182. /// 户口簿识别
  183. /// 文档下载 ?
  184. /// </summary>
  185. /// <param name="dto"></param>
  186. /// <returns></returns>
  187. [HttpPost]
  188. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  189. public async Task<IActionResult> GetResidenceBookletOCR(ResidenceBookletOCRDto dto)
  190. {
  191. double strSize = 1024 * 1024 * 7;
  192. if (dto.picBase64.Length > strSize)
  193. {
  194. return Ok(JsonView("图片不能大于7M!"));
  195. }
  196. var residenceBookData = await TencentOCRTools.GetOCR((int)TencentOCREnum.ResidenceBooklet, dto);
  197. if (residenceBookData.Code != 0)
  198. {
  199. return Ok(JsonView(residenceBookData.Msg));
  200. }
  201. if (residenceBookData.Data == null)
  202. {
  203. return Ok(JsonView(residenceBookData.Msg));
  204. }
  205. #region Feil Down
  206. BizLicenseOCRResponse res = residenceBookData.Data as BizLicenseOCRResponse;
  207. Document doc = new Document();
  208. DocumentBuilder builder = new DocumentBuilder(doc);
  209. string fileName = "户口簿识别";
  210. string content = $"{fileName}\r\n\r\n";
  211. if (res != null)
  212. {
  213. content += $"公司名称:{res.Name}\r\n\r\n";
  214. content += $"统一社会信用代码(三合一之前为注册号):{res.RegNum}\r\n\r\n";
  215. content += $"注册资本:{res.Capital}\r\n\r\n";
  216. content += $"法人代表:{res.Person}\r\n\r\n";
  217. content += $"地址:{res.Address}\r\n\r\n";
  218. content += $"经营范围:{res.Business}\r\n\r\n";
  219. content += $"主题类型:{res.Type}\r\n\r\n";
  220. content += $"营业期限:{res.Period}\r\n\r\n";
  221. content += $"组成形式:{res.ComposingForm}\r\n\r\n";
  222. content += $"成立日期:{res.SetDate}\r\n\r\n";
  223. }
  224. builder.Write(content);
  225. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  226. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  227. string outputPath = Path.Combine(outputDir, outputFile);
  228. if (!Directory.Exists(outputDir))
  229. {
  230. Directory.CreateDirectory(outputDir);
  231. }
  232. doc.Save(outputPath);
  233. string url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  234. var data = new {
  235. dataSource = residenceBookData.Data,
  236. url = url
  237. };
  238. #endregion
  239. return Ok(JsonView(data));
  240. }
  241. /// <summary>
  242. /// 营业执照识别
  243. /// 获取营业执照返回数据
  244. /// </summary>
  245. /// <param name="dto"></param>
  246. /// <returns></returns>
  247. [HttpPost]
  248. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  249. public async Task<IActionResult> GetBizLicenseOCR(BizLicenseOCRDto dto)
  250. {
  251. double strSize = 1024 * 1024 * 7;
  252. if (dto.picBase64.Length > strSize)
  253. {
  254. return Ok(JsonView("图片不能大于7M!"));
  255. }
  256. var residenceBookData = await TencentOCRTools.GetOCR((int)TencentOCREnum.BizLicense, dto);
  257. if (residenceBookData.Code != 0)
  258. {
  259. return Ok(JsonView(residenceBookData.Msg));
  260. }
  261. if (residenceBookData.Data == null)
  262. {
  263. return Ok(JsonView(residenceBookData.Msg));
  264. }
  265. return Ok(JsonView(residenceBookData.Data));
  266. }
  267. /// <summary>
  268. /// 组织结构识别
  269. /// 获取组织结构返回数据
  270. /// </summary>
  271. /// <param name="dto"></param>
  272. /// <returns></returns>
  273. [HttpPost]
  274. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  275. public async Task<IActionResult> GetOrgCodeCertOCR(OrgCodeCertOCRDto dto)
  276. {
  277. double strSize = 1024 * 1024 * 7;
  278. if (dto.picBase64.Length > strSize)
  279. {
  280. return Ok(JsonView("图片不能大于7M!"));
  281. }
  282. var occData = await TencentOCRTools.GetOCR((int)TencentOCREnum.OrgCodeCert, dto);
  283. if (occData.Code != 0)
  284. {
  285. return Ok(JsonView(occData.Msg));
  286. }
  287. if (occData.Data == null)
  288. {
  289. return Ok(JsonView(occData.Msg));
  290. }
  291. return Ok(JsonView(occData.Data));
  292. }
  293. /// <summary>
  294. /// 行驶证识别
  295. /// 文档下载
  296. /// CardSide=0 主页正面(有红色印章的一面),CardSide=1 行驶证副页正面(有号码号牌的一面),CardSide=2 行驶证主页正面和副页正面。
  297. /// </summary>
  298. /// <param name="dto"></param>
  299. /// <returns></returns>
  300. [HttpPost]
  301. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  302. public async Task<IActionResult> GetVehicleLicense(VehicleLicenseOCRDto dto)
  303. {
  304. double strSize = 1024 * 1024 * 7;
  305. if (dto.picBase64.Length > strSize)
  306. {
  307. return Ok(JsonView("图片不能大于7M!"));
  308. }
  309. var vehicleLicenseData = TencentOCRTools.GetOCR((int)TencentOCREnum.VehicleLicense, dto);
  310. if (vehicleLicenseData.Result.Code != 0)
  311. {
  312. return Ok(JsonView(vehicleLicenseData.Result.Msg));
  313. }
  314. if (vehicleLicenseData.Result.Data == null)
  315. {
  316. return Ok(JsonView(vehicleLicenseData.Result.Msg));
  317. }
  318. #region word下载
  319. VehicleLicenseOCRResponse vlData = vehicleLicenseData.Result.Data as VehicleLicenseOCRResponse;
  320. string url = "";
  321. string fileName = "";
  322. string content = "";
  323. dynamic data = null;
  324. try
  325. {
  326. //Aspose.Words.License lic = new Aspose.Words.License();
  327. //lic.SetLicense("Aspose.Wrods.lic");//破解版不用设置license
  328. if (dto.CardSide == 0) // 正面
  329. {
  330. fileName = "行驶证识别-主页";
  331. content = $"{fileName}\r\n\r\n";
  332. TextVehicleFront frontInfo = vlData.FrontInfo;
  333. Document doc = new Document();
  334. DocumentBuilder builder = new DocumentBuilder(doc);
  335. if (frontInfo != null)
  336. {
  337. data = frontInfo;
  338. content += $"号牌号码:{frontInfo.PlateNo ?? "Unknown"}\r\n\r\n";
  339. content += $"车辆类型:{frontInfo.VehicleType ?? "Unknown"}\r\n\r\n";
  340. content += $"所有人:{frontInfo.Owner ?? "Unknown"}\r\n\r\n";
  341. content += $"住址:{frontInfo.Address ?? "Unknown"}\r\n\r\n";
  342. content += $"使用性质:{frontInfo.UseCharacter ?? "Unknown"}\r\n\r\n";
  343. content += $"品牌型号:{frontInfo.Model ?? "Unknown"}\r\n\r\n";
  344. content += $"车辆识别代号:{frontInfo.Vin ?? "Unknown"}\r\n\r\n";
  345. content += $"发动机号码:{frontInfo.EngineNo ?? "Unknown"}\r\n\r\n";
  346. content += $"注册日期:{frontInfo.RegisterDate ?? "Unknown"}\r\n\r\n";
  347. content += $"发证日期:{frontInfo.IssueDate ?? "Unknown"}\r\n\r\n";
  348. content += $"印章:{frontInfo.Seal ?? "Unknown"}\r\n\r\n";
  349. }
  350. builder.Write(content);
  351. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  352. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  353. string outputPath = Path.Combine(outputDir, outputFile);
  354. if (!Directory.Exists(outputDir))
  355. {
  356. Directory.CreateDirectory(outputDir);
  357. }
  358. //去水印
  359. new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String(AsposeHelper.asposeKey)));
  360. doc.Save(outputPath);
  361. url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  362. }
  363. else if (dto.CardSide == 1) //反面
  364. {
  365. TextVehicleBack backInfo = vlData.BackInfo;
  366. fileName = "行驶证识别-副页";
  367. content = $"{fileName}\r\n\r\n";
  368. Document doc = new Document();
  369. DocumentBuilder builder = new DocumentBuilder(doc);
  370. if (backInfo != null)
  371. {
  372. data = backInfo;
  373. content += $"号牌号码:{backInfo.PlateNo ?? "Unknown"}\r\n\r\n";
  374. content += $"档案编号:{backInfo.FileNo ?? "Unknown"}\r\n\r\n";
  375. content += $"核定人数:{backInfo.AllowNum ?? "Unknown"}\r\n\r\n";
  376. content += $"总质量:{backInfo.TotalMass ?? "Unknown"}\r\n\r\n";
  377. content += $"整备质量:{backInfo.CurbWeight ?? "Unknown"}\r\n\r\n";
  378. content += $"核定载质量:{backInfo.LoadQuality ?? "Unknown"}\r\n\r\n";
  379. content += $"外廓尺寸:{backInfo.ExternalSize ?? "Unknown"}\r\n\r\n";
  380. content += $"备注:{backInfo.Marks ?? "Unknown"}\r\n\r\n";
  381. content += $"检验记录:{backInfo.Record ?? "Unknown"}\r\n\r\n";
  382. content += $"准牵引总质量:{backInfo.TotalQuasiMass ?? "Unknown"}\r\n\r\n";
  383. }
  384. builder.Write(content);
  385. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  386. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  387. string outputPath = Path.Combine(outputDir, outputFile);
  388. if (!Directory.Exists(outputDir))
  389. {
  390. Directory.CreateDirectory(outputDir);
  391. }
  392. doc.Save(outputPath);
  393. url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  394. }
  395. #endregion
  396. return Ok(JsonView(true, "操作成功", new { dataSource = data, url = url }));
  397. }
  398. catch (Exception ex)
  399. {
  400. return Ok(JsonView(false, $"InnerException:{ex.InnerException?.Message ?? "Unknown"} ErrMsg:{ex.Message}"));
  401. }
  402. }
  403. /// <summary>
  404. /// 房产证识别(腾讯OCR 未找到)
  405. /// 返回数据
  406. /// </summary>
  407. /// <param name="dto"></param>
  408. /// <returns></returns>
  409. [HttpPost]
  410. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  411. public async Task<IActionResult> GetPropOwnerCert(PropOwnerCertOCRDto dto)
  412. {
  413. double strSize = 1024 * 1024 * 7;
  414. if (dto.picBase64.Length > strSize)
  415. {
  416. return Ok(JsonView("图片不能大于7M!"));
  417. }
  418. var pcData = await TencentOCRTools.GetOCR((int)TencentOCREnum.PropOwnerCert, dto);
  419. if (pcData.Code != 0)
  420. {
  421. return Ok(JsonView(pcData.Msg));
  422. }
  423. if (pcData.Data == null)
  424. {
  425. return Ok(JsonView(pcData.Msg));
  426. }
  427. return Ok(JsonView(pcData.Data));
  428. }
  429. /// <summary>
  430. /// 结婚证识别(腾讯OCR 未找到)
  431. /// 返回数据
  432. /// </summary>
  433. /// <param name="dto"></param>
  434. /// <returns></returns>
  435. [HttpPost]
  436. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  437. public async Task<IActionResult> GetMarriageLicense(MarriageLicenseOCRDto dto)
  438. {
  439. double strSize = 1024 * 1024 * 7;
  440. if (dto.picBase64.Length > strSize)
  441. {
  442. return Ok(JsonView("图片不能大于7M!"));
  443. }
  444. var mlData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  445. if (mlData.Code != 0)
  446. {
  447. return Ok(JsonView(mlData.Msg));
  448. }
  449. if (mlData.Data == null)
  450. {
  451. return Ok(JsonView(mlData.Msg));
  452. }
  453. return Ok(JsonView(mlData.Data));
  454. }
  455. /// <summary>
  456. /// 统一信用代码识别 (腾讯OCR 未找到)
  457. /// 返回数据
  458. /// </summary>
  459. /// <param name="dto"></param>
  460. /// <returns></returns>
  461. [HttpPost]
  462. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  463. public async Task<IActionResult> GetUniformCreditCode(UniformCreditCodeOCRDto dto)
  464. {
  465. double strSize = 1024 * 1024 * 7;
  466. if (dto.picBase64.Length > strSize)
  467. {
  468. return Ok(JsonView("图片不能大于7M!"));
  469. }
  470. var gbData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  471. if (gbData.Code != 0)
  472. {
  473. return Ok(JsonView(gbData.Msg));
  474. }
  475. if (gbData.Data == null)
  476. {
  477. return Ok(JsonView(gbData.Msg));
  478. }
  479. return Ok(JsonView(gbData.Data));
  480. }
  481. /// <summary>
  482. /// 护照识别(中国大陆地区护照)
  483. /// 获取护照识别(中国大陆地区护照)返回数据
  484. /// </summary>
  485. /// <param name="dto"></param>
  486. /// <returns></returns>
  487. [HttpPost]
  488. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  489. public async Task<IActionResult> GetPassport(PassportOCRDto dto)
  490. {
  491. double strSize = 1024 * 1024 * 7;
  492. if (dto.picBase64.Length > strSize)
  493. {
  494. return Ok(JsonView("图片不能大于7M!"));
  495. }
  496. var pData = await TencentOCRTools.GetOCR((int)TencentOCREnum.Passport, dto);
  497. if (pData.Code != 0)
  498. {
  499. return Ok(JsonView(pData.Msg));
  500. }
  501. if (pData.Data == null)
  502. {
  503. return Ok(JsonView(pData.Msg));
  504. }
  505. string url = "";
  506. #region 文档下载
  507. string fileName = "护照识别(中国大陆地区护照)";
  508. string content = $"{fileName}\r\n\r\n";
  509. PassportOCRResponse pInfo = pData.Data as PassportOCRResponse;
  510. Document doc = new Document();
  511. DocumentBuilder builder = new DocumentBuilder(doc);
  512. if (pInfo != null)
  513. {
  514. content += $"国家码:{pInfo.Country ?? "Unknown"}\r\n\r\n";
  515. content += $"护照号:{pInfo.PassportNo ?? "Unknown"}\r\n\r\n";
  516. content += $"性别:{pInfo.Sex ?? "Unknown"}\r\n\r\n";
  517. content += $"国籍:{pInfo.Nationality ?? "Unknown"}\r\n\r\n";
  518. content += $"出生日期:{pInfo.BirthDate ?? "Unknown"}\r\n\r\n";
  519. content += $"签发日期:{pInfo.IssueDate ?? "Unknown"}\r\n\r\n";
  520. content += $"签发地点:{pInfo.IssuePlace ?? "Unknown"}\r\n\r\n";
  521. content += $"有效期:{pInfo.ExpiryDate ?? "Unknown"}\r\n\r\n";
  522. content += $"持证人签名:{pInfo.Signature ?? "Unknown"}\r\n\r\n";
  523. content += $"最下方第一行 MRZ Code 序列:{pInfo.CodeSet ?? "Unknown"}\r\n\r\n";
  524. content += $"最下方第二行 MRZ Code 序列:{pInfo.CodeCrc ?? "Unknown"}\r\n\r\n";
  525. content += $"姓名:{pInfo.Name ?? "Unknown"}\r\n\r\n";
  526. content += $"姓:{pInfo.FamilyName ?? "Unknown"}\r\n\r\n";
  527. content += $"名:{pInfo.FirstName ?? "Unknown"}\r\n\r\n";
  528. }
  529. builder.Write(content);
  530. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  531. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  532. string outputPath = Path.Combine(outputDir, outputFile);
  533. if (!Directory.Exists(outputDir))
  534. {
  535. Directory.CreateDirectory(outputDir);
  536. }
  537. //去水印
  538. new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String(AsposeHelper.asposeKey)));
  539. doc.Save(outputPath);
  540. url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  541. #endregion
  542. return Ok(JsonView(true,"操作成功", new { dataSource = pInfo, url = url }));
  543. }
  544. /// <summary>
  545. /// 护照识别(港澳台地区及境外护照)
  546. /// 获取护照识别(港澳台地区及境外护照)返回数据
  547. /// </summary>
  548. /// <param name="dto"></param>
  549. /// <returns></returns>
  550. [HttpPost]
  551. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  552. public async Task<IActionResult> GetMLIDPassport(PassportOCRDto dto)
  553. {
  554. double strSize = 1024 * 1024 * 7;
  555. if (dto.picBase64.Length > strSize)
  556. {
  557. return Ok(JsonView("图片不能大于7M!"));
  558. }
  559. var mlidpData = await TencentOCRTools.GetOCR((int)TencentOCREnum.MLIDPassport, dto);
  560. if (mlidpData.Code != 0)
  561. {
  562. return Ok(JsonView(mlidpData.Msg));
  563. }
  564. if (mlidpData.Data == null)
  565. {
  566. return Ok(JsonView(mlidpData.Msg));
  567. }
  568. string url = "";
  569. #region 文档下载
  570. string fileName = "护照识别(中国大陆地区护照)";
  571. string content = $"{fileName}\r\n\r\n";
  572. PassportOCRResponse pInfo = mlidpData.Data as PassportOCRResponse;
  573. Document doc = new Document();
  574. DocumentBuilder builder = new DocumentBuilder(doc);
  575. if (pInfo != null)
  576. {
  577. content += $"国家码:{pInfo.Country ?? "Unknown"}\r\n\r\n";
  578. content += $"护照号:{pInfo.PassportNo ?? "Unknown"}\r\n\r\n";
  579. content += $"性别:{pInfo.Sex ?? "Unknown"}\r\n\r\n";
  580. content += $"国籍:{pInfo.Nationality ?? "Unknown"}\r\n\r\n";
  581. content += $"出生日期:{pInfo.BirthDate ?? "Unknown"}\r\n\r\n";
  582. content += $"签发日期:{pInfo.IssueDate ?? "Unknown"}\r\n\r\n";
  583. content += $"签发地点:{pInfo.IssuePlace ?? "Unknown"}\r\n\r\n";
  584. content += $"有效期:{pInfo.ExpiryDate ?? "Unknown"}\r\n\r\n";
  585. content += $"持证人签名:{pInfo.Signature ?? "Unknown"}\r\n\r\n";
  586. content += $"最下方第一行 MRZ Code 序列:{pInfo.CodeSet ?? "Unknown"}\r\n\r\n";
  587. content += $"最下方第二行 MRZ Code 序列:{pInfo.CodeCrc ?? "Unknown"}\r\n\r\n";
  588. content += $"姓名:{pInfo.Name ?? "Unknown"}\r\n\r\n";
  589. content += $"姓:{pInfo.FamilyName ?? "Unknown"}\r\n\r\n";
  590. content += $"名:{pInfo.FirstName ?? "Unknown"}\r\n\r\n";
  591. }
  592. builder.Write(content);
  593. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  594. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  595. string outputPath = Path.Combine(outputDir, outputFile);
  596. if (!Directory.Exists(outputDir))
  597. {
  598. Directory.CreateDirectory(outputDir);
  599. }
  600. //去水印
  601. new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String(AsposeHelper.asposeKey)));
  602. doc.Save(outputPath);
  603. url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  604. #endregion
  605. return Ok(JsonView(true, "操作成功", new { dataSource = pInfo, url = url }));
  606. }
  607. /// <summary>
  608. /// 名片识别
  609. /// 获取名片 返回数据
  610. /// </summary>
  611. /// <param name="dto"></param>
  612. /// <returns></returns>
  613. [HttpPost]
  614. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  615. public async Task<IActionResult> GetBusinessCard(BusinessCardOCRDto dto)
  616. {
  617. double strSize = 1024 * 1024 * 7;
  618. if (dto.picBase64.Length > strSize)
  619. {
  620. return Ok(JsonView("图片不能大于7M!"));
  621. }
  622. try
  623. {
  624. var mlidpData = await TencentOCRTools.GetOCR((int)TencentOCREnum.BusinessCard, dto);
  625. if (mlidpData.Code != 0)
  626. {
  627. return Ok(JsonView(mlidpData.Msg));
  628. }
  629. if (mlidpData.Data == null)
  630. {
  631. return Ok(JsonView(mlidpData.Msg));
  632. }
  633. return Ok(JsonView(mlidpData.Data));
  634. }
  635. catch (Exception ex)
  636. {
  637. return Ok(JsonView(false, ex.Message));
  638. }
  639. }
  640. /// <summary>
  641. /// 智能结构化识别V2
  642. /// 获取名片 返回数据
  643. /// </summary>
  644. /// <param name="dto"></param>
  645. /// <returns></returns>
  646. [HttpPost]
  647. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  648. public async Task<IActionResult> GetSmartStructuralOCRV2(BusinessCardOCRDto dto)
  649. {
  650. string[] picBase64Array = dto.picBase64.Split(';');
  651. string picFormat = picBase64Array[0].Split('/')[1];
  652. if (!TencentOCRTools.ImageType(picFormat))
  653. {
  654. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  655. }
  656. double strSize = 1024 * 1024 * 7;
  657. if (dto.picBase64.Length > strSize)
  658. {
  659. return Ok(JsonView("图片不能大于7M!"));
  660. }
  661. var ssData = await TencentOCRTools.GetOCR((int)TencentOCREnum.SmartStructuralOCRV2, dto);
  662. if (ssData.Code != 0)
  663. {
  664. return Ok(JsonView(false, ssData.Msg));
  665. }
  666. if (ssData.Data == null)
  667. {
  668. return Ok(JsonView(false, ssData.Msg));
  669. }
  670. //var wordRes = GenerateWord(ssData.Data as List<ocrGeneralView>, "智能结构化识别V2");
  671. List<ocrGeneralView>? datas = ssData.Data as List<ocrGeneralView>;
  672. var data = new {
  673. dataSource = ssData.Data,
  674. url = GenerateWord(datas, "智能结构化识别V2")
  675. };
  676. return Ok(JsonView(true, ssData.Msg, data));
  677. }
  678. /// <summary>
  679. /// 生成 ocr word
  680. /// </summary>
  681. /// <param name="views"></param>
  682. /// <param name="fileName"></param>
  683. /// <returns></returns>
  684. private string GenerateWord(List<ocrGeneralView> views, string fileName)
  685. {
  686. Document doc = new Document();
  687. DocumentBuilder builder = new DocumentBuilder(doc);
  688. string content = $"{fileName}\r\n\r\n";
  689. foreach (var view in views)
  690. {
  691. content += $"TypeName:{view.name}\t\r\nContent:{view.value}\r\n\r\n";
  692. }
  693. builder.Write(content);
  694. string outputDir = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR";
  695. string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx";
  696. string outputPath = Path.Combine(outputDir, outputFile);
  697. if (!Directory.Exists(outputDir))
  698. {
  699. Directory.CreateDirectory(outputDir);
  700. }
  701. doc.Save(outputPath);
  702. return $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}";
  703. }
  704. #endregion
  705. #region android ios
  706. /// <summary>
  707. /// 名片识别
  708. /// 获取名片 移动端 返回数据
  709. /// </summary>
  710. /// <param name="dto"></param>
  711. /// <returns></returns>
  712. [HttpPost]
  713. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  714. public async Task<IActionResult> PostBusinessCardApp(BusinessCardOCRDto dto)
  715. {
  716. double strSize = 1024 * 1024 * 7;
  717. if (dto.picBase64.Length > strSize)
  718. {
  719. return Ok(JsonView("图片不能大于7M!"));
  720. }
  721. Logs(dto.picBase64);
  722. Logs("名片识别开始时间:" + DateTime.Now);
  723. try
  724. {
  725. var mlidpData = TencentOCRTools.PostOCRApp((int)TencentOCREnum.BusinessCard, dto);
  726. if (mlidpData.Code != 0)
  727. {
  728. return Ok(JsonView(mlidpData.Msg));
  729. }
  730. if (mlidpData.Data == null)
  731. {
  732. return Ok(JsonView(mlidpData.Msg));
  733. }
  734. Logs("名片识别结束时间:" + DateTime.Now);
  735. return Ok(JsonView(mlidpData.Data));
  736. }
  737. catch (Exception ex)
  738. {
  739. return Ok(JsonView(false, ex.Message));
  740. }
  741. }
  742. #endregion
  743. }
  744. }