TencentOCRController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. using Aspose.Cells;
  2. using OASystem.API.OAMethodLib.File;
  3. //using OASystem.API.OAMethodLib.NPOI;
  4. using OASystem.API.OAMethodLib.TencentCloudAPI;
  5. using OASystem.API.OAMethodLib.YouDaoAPI;
  6. using OASystem.Domain.Dtos.CRM;
  7. using OASystem.Domain.Dtos.Tencent;
  8. using OASystem.Domain.ViewModels.TencentOCR;
  9. using OASystem.Infrastructure.Repositories.CRM;
  10. using Org.BouncyCastle.Crypto;
  11. using Org.BouncyCastle.Utilities.Encoders;
  12. using StackExchange.Redis;
  13. using System.Net.NetworkInformation;
  14. using TencentCloud.Ocr.V20181119.Models;
  15. using Ubiety.Dns.Core;
  16. namespace OASystem.API.Controllers
  17. {
  18. /// <summary>
  19. /// TencentOCR 识别
  20. /// </summary>
  21. [Route("api/[controller]/[action]")]
  22. //[ApiController]
  23. public class TencentOCRController : ControllerBase
  24. {
  25. private readonly VisaDeleClientRepository _visaDeleClientRepository;
  26. public TencentOCRController(VisaDeleClientRepository visaDeleClientRepository)
  27. {
  28. this._visaDeleClientRepository = visaDeleClientRepository;
  29. }
  30. #region web
  31. /// <summary>
  32. /// 通用印刷体
  33. /// </summary>
  34. /// <param name="dto"></param>
  35. /// <returns></returns>
  36. [HttpPost]
  37. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  38. public async Task<IActionResult> GetGeneralBasic(GeneralBasicOCRDto dto)
  39. {
  40. string[] picBase64Array = dto.picBase64.Split(';');
  41. string picFormat = picBase64Array[0].Split('/')[1];
  42. if (!TencentOCRTools.ImageType(picFormat))
  43. {
  44. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  45. }
  46. double strSize = 1024 * 1024 * 7;
  47. if (dto.picBase64.Length > strSize)
  48. {
  49. return Ok(JsonView("图片不能大于7M!"));
  50. }
  51. var gbData = TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  52. if (gbData.Code != 0)
  53. {
  54. return Ok(JsonView(gbData.Msg));
  55. }
  56. if (gbData.Data == null)
  57. {
  58. return Ok(JsonView(gbData.Msg));
  59. }
  60. return Ok(JsonView(gbData.Data));
  61. }
  62. /// <summary>
  63. /// 身份证识别(国徽面/反面)
  64. /// 获取身份证返回数据
  65. /// </summary>
  66. /// <param name="dto"></param>
  67. /// <returns></returns>
  68. [HttpPost]
  69. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  70. public async Task<IActionResult> GetIDCard(IDCardOCRDto dto)
  71. {
  72. string[] picBase64Array = dto.picBase64.Split(';');
  73. string picFormat = picBase64Array[0].Split('/')[1];
  74. if (!TencentOCRTools.ImageType(picFormat))
  75. {
  76. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  77. }
  78. double strSize = 1024 * 1024 * 7;
  79. if (dto.picBase64.Length > strSize)
  80. {
  81. return Ok(JsonView("图片不能大于7M!"));
  82. }
  83. var idCardData = TencentOCRTools.GetOCR((int)TencentOCREnum.IDCard, dto);
  84. if (idCardData.Code != 0)
  85. {
  86. return Ok(JsonView(idCardData.Msg));
  87. }
  88. if (idCardData.Data == null)
  89. {
  90. return Ok(JsonView(idCardData.Msg));
  91. }
  92. IDCardOCRAndDownUrlView iDCardOCRView = new IDCardOCRAndDownUrlView() { };
  93. if (dto.Language.ToLower() == "ch")
  94. {
  95. bool cerdStatus = await _visaDeleClientRepository.SetCrmUpdPassIdCardOCR(
  96. new SetCrmUpdPassIdCardOCRDto
  97. {
  98. UserId = dto.UserId,
  99. ClientName = idCardData.Data.Name,
  100. Sex = idCardData.Data.Sex == "男" ? 0 : 1,
  101. CerdNo = idCardData.Data.IdNum,
  102. CerdAddress = idCardData.Data.Address,
  103. });
  104. iDCardOCRView.Status = cerdStatus;
  105. iDCardOCRView.Name = idCardData.Data.Name;
  106. iDCardOCRView.Sex = idCardData.Data.Sex;
  107. iDCardOCRView.Nation = idCardData.Data.Nation;
  108. iDCardOCRView.Birth = idCardData.Data.Birth;
  109. iDCardOCRView.Address = idCardData.Data.Address;
  110. iDCardOCRView.IdNum = idCardData.Data.IdNum;
  111. iDCardOCRView.Authority = idCardData.Data.Authority;
  112. iDCardOCRView.ValidDate = idCardData.Data.ValidDate;
  113. }
  114. else if (dto.Language.ToLower() == "en")
  115. {
  116. iDCardOCRView.Name = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Name);
  117. iDCardOCRView.Sex = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Sex);
  118. iDCardOCRView.Nation = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Nation);
  119. iDCardOCRView.Birth = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Birth);
  120. iDCardOCRView.Address = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Address);
  121. iDCardOCRView.IdNum = idCardData.Data.IdNum;
  122. iDCardOCRView.Authority = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Authority);
  123. iDCardOCRView.ValidDate = await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.ValidDate);
  124. }
  125. #region word生成 返回地址
  126. Dictionary<string, object> dic = new Dictionary<string, object>();
  127. dic.Add("Name", iDCardOCRView.Name);
  128. dic.Add("Sex", iDCardOCRView.Sex);
  129. dic.Add("Nation", iDCardOCRView.Nation);
  130. dic.Add("Birth", iDCardOCRView.Birth);
  131. dic.Add("Address", iDCardOCRView.Address);
  132. dic.Add("IdNum", iDCardOCRView.IdNum);
  133. dic.Add("Authority", iDCardOCRView.Authority);
  134. dic.Add("ValidDate", iDCardOCRView.ValidDate);
  135. string serverPath = "";
  136. if (dto.Language.ToLower() == "ch")
  137. {
  138. string fileNameCh = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[CH]" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  139. serverPath = AsposeHelper.ExpertWordToModel("ocr_身份证(人像面).doc", "TencentOCR", fileNameCh, dic, null);
  140. }
  141. else if (dto.Language.ToLower() == "en")
  142. {
  143. string fileNameEn = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[EN]" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  144. serverPath = AsposeHelper.ExpertWordToModel("(EN)ocr_身份证(人像面).doc", "TencentOCR", fileNameEn, dic, null);
  145. }
  146. iDCardOCRView.DownUrl = serverPath;
  147. #endregion
  148. return Ok(JsonView(iDCardOCRView));
  149. }
  150. /// <summary>
  151. /// 户口簿识别
  152. /// 获取户口簿返回数据
  153. /// </summary>
  154. /// <param name="dto"></param>
  155. /// <returns></returns>
  156. [HttpPost]
  157. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  158. public async Task<IActionResult> GetResidenceBookletOCR(ResidenceBookletOCRDto dto)
  159. {
  160. double strSize = 1024 * 1024 * 7;
  161. if (dto.picBase64.Length > strSize)
  162. {
  163. return Ok(JsonView("图片不能大于7M!"));
  164. }
  165. var residenceBookData = TencentOCRTools.GetOCR((int)TencentOCREnum.ResidenceBooklet, dto);
  166. if (residenceBookData.Code != 0)
  167. {
  168. return Ok(JsonView(residenceBookData.Msg));
  169. }
  170. if (residenceBookData.Data == null)
  171. {
  172. return Ok(JsonView(residenceBookData.Msg));
  173. }
  174. return Ok(JsonView(residenceBookData.Data));
  175. }
  176. /// <summary>
  177. /// 营业执照识别
  178. /// 获取营业执照返回数据
  179. /// </summary>
  180. /// <param name="dto"></param>
  181. /// <returns></returns>
  182. [HttpPost]
  183. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  184. public async Task<IActionResult> GetBizLicenseOCR(BizLicenseOCRDto dto)
  185. {
  186. double strSize = 1024 * 1024 * 7;
  187. if (dto.picBase64.Length > strSize)
  188. {
  189. return Ok(JsonView("图片不能大于7M!"));
  190. }
  191. var residenceBookData = TencentOCRTools.GetOCR((int)TencentOCREnum.BizLicense, dto);
  192. if (residenceBookData.Code != 0)
  193. {
  194. return Ok(JsonView(residenceBookData.Msg));
  195. }
  196. if (residenceBookData.Data == null)
  197. {
  198. return Ok(JsonView(residenceBookData.Msg));
  199. }
  200. return Ok(JsonView(residenceBookData.Data));
  201. }
  202. /// <summary>
  203. /// 组织结构识别
  204. /// 获取组织结构返回数据
  205. /// </summary>
  206. /// <param name="dto"></param>
  207. /// <returns></returns>
  208. [HttpPost]
  209. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  210. public async Task<IActionResult> GetOrgCodeCertOCR(OrgCodeCertOCRDto dto)
  211. {
  212. double strSize = 1024 * 1024 * 7;
  213. if (dto.picBase64.Length > strSize)
  214. {
  215. return Ok(JsonView("图片不能大于7M!"));
  216. }
  217. var occData = TencentOCRTools.GetOCR((int)TencentOCREnum.OrgCodeCert, dto);
  218. if (occData.Code != 0)
  219. {
  220. return Ok(JsonView(occData.Msg));
  221. }
  222. if (occData.Data == null)
  223. {
  224. return Ok(JsonView(occData.Msg));
  225. }
  226. return Ok(JsonView(occData.Data));
  227. }
  228. /// <summary>
  229. /// 行驶证识别
  230. /// 获取行驶证返回数据
  231. /// CardSide=0 主页正面(有红色印章的一面),CardSide=1 行驶证副页正面(有号码号牌的一面),CardSide=2 行驶证主页正面和副页正面。
  232. /// </summary>
  233. /// <param name="dto"></param>
  234. /// <returns></returns>
  235. [HttpPost]
  236. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  237. public async Task<IActionResult> GetVehicleLicense(VehicleLicenseOCRDto dto)
  238. {
  239. double strSize = 1024 * 1024 * 7;
  240. if (dto.picBase64.Length > strSize)
  241. {
  242. return Ok(JsonView("图片不能大于7M!"));
  243. }
  244. var vehicleLicenseData = TencentOCRTools.GetOCR((int)TencentOCREnum.VehicleLicense, dto);
  245. if (vehicleLicenseData.Code != 0)
  246. {
  247. return Ok(JsonView(vehicleLicenseData.Msg));
  248. }
  249. if (vehicleLicenseData.Data == null)
  250. {
  251. return Ok(JsonView(vehicleLicenseData.Msg));
  252. }
  253. return Ok(JsonView(vehicleLicenseData.Data));
  254. }
  255. /// <summary>
  256. /// 房产证识别
  257. /// 获取房产证返回数据
  258. /// </summary>
  259. /// <param name="dto"></param>
  260. /// <returns></returns>
  261. [HttpPost]
  262. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  263. public async Task<IActionResult> GetPropOwnerCert(PropOwnerCertOCRDto dto)
  264. {
  265. double strSize = 1024 * 1024 * 7;
  266. if (dto.picBase64.Length > strSize)
  267. {
  268. return Ok(JsonView("图片不能大于7M!"));
  269. }
  270. var pcData = TencentOCRTools.GetOCR((int)TencentOCREnum.PropOwnerCert, dto);
  271. if (pcData.Code != 0)
  272. {
  273. return Ok(JsonView(pcData.Msg));
  274. }
  275. if (pcData.Data == null)
  276. {
  277. return Ok(JsonView(pcData.Msg));
  278. }
  279. return Ok(JsonView(pcData.Data));
  280. }
  281. /// <summary>
  282. /// 结婚证识别
  283. /// 获取结婚证返回数据
  284. /// </summary>
  285. /// <param name="dto"></param>
  286. /// <returns></returns>
  287. [HttpPost]
  288. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  289. public async Task<IActionResult> GetMarriageLicense(MarriageLicenseOCRDto dto)
  290. {
  291. double strSize = 1024 * 1024 * 7;
  292. if (dto.picBase64.Length > strSize)
  293. {
  294. return Ok(JsonView("图片不能大于7M!"));
  295. }
  296. var mlData = TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  297. if (mlData.Code != 0)
  298. {
  299. return Ok(JsonView(mlData.Msg));
  300. }
  301. if (mlData.Data == null)
  302. {
  303. return Ok(JsonView(mlData.Msg));
  304. }
  305. return Ok(JsonView(mlData.Data));
  306. }
  307. /// <summary>
  308. /// 统一信用代码识别
  309. /// 获取统一信用代码返回数据
  310. /// </summary>
  311. /// <param name="dto"></param>
  312. /// <returns></returns>
  313. [HttpPost]
  314. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  315. public async Task<IActionResult> GetUniformCreditCode(UniformCreditCodeOCRDto dto)
  316. {
  317. double strSize = 1024 * 1024 * 7;
  318. if (dto.picBase64.Length > strSize)
  319. {
  320. return Ok(JsonView("图片不能大于7M!"));
  321. }
  322. var gbData = TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto);
  323. if (gbData.Code != 0)
  324. {
  325. return Ok(JsonView(gbData.Msg));
  326. }
  327. if (gbData.Data == null)
  328. {
  329. return Ok(JsonView(gbData.Msg));
  330. }
  331. return Ok(JsonView(gbData.Data));
  332. }
  333. /// <summary>
  334. /// 护照识别(中国大陆地区护照)
  335. /// 获取护照识别(中国大陆地区护照)返回数据
  336. /// </summary>
  337. /// <param name="dto"></param>
  338. /// <returns></returns>
  339. [HttpPost]
  340. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  341. public async Task<IActionResult> GetPassport(PassportOCRDto dto)
  342. {
  343. double strSize = 1024 * 1024 * 7;
  344. if (dto.picBase64.Length > strSize)
  345. {
  346. return Ok(JsonView("图片不能大于7M!"));
  347. }
  348. var pData = TencentOCRTools.GetOCR((int)TencentOCREnum.Passport, dto);
  349. if (pData.Code != 0)
  350. {
  351. return Ok(JsonView(pData.Msg));
  352. }
  353. if (pData.Data == null)
  354. {
  355. return Ok(JsonView(pData.Msg));
  356. }
  357. return Ok(JsonView(pData.Data));
  358. }
  359. /// <summary>
  360. /// 护照识别(港澳台地区及境外护照)
  361. /// 获取护照识别(港澳台地区及境外护照)返回数据
  362. /// </summary>
  363. /// <param name="dto"></param>
  364. /// <returns></returns>
  365. [HttpPost]
  366. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  367. public async Task<IActionResult> GetMLIDPassport(PassportOCRDto dto)
  368. {
  369. double strSize = 1024 * 1024 * 7;
  370. if (dto.picBase64.Length > strSize)
  371. {
  372. return Ok(JsonView("图片不能大于7M!"));
  373. }
  374. var mlidpData = TencentOCRTools.GetOCR((int)TencentOCREnum.MLIDPassport, dto);
  375. if (mlidpData.Code != 0)
  376. {
  377. return Ok(JsonView(mlidpData.Msg));
  378. }
  379. if (mlidpData.Data == null)
  380. {
  381. return Ok(JsonView(mlidpData.Msg));
  382. }
  383. return Ok(JsonView(mlidpData.Data));
  384. }
  385. /// <summary>
  386. /// 名片识别
  387. /// 获取名片 返回数据
  388. /// </summary>
  389. /// <param name="dto"></param>
  390. /// <returns></returns>
  391. [HttpPost]
  392. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  393. public async Task<IActionResult> GetBusinessCard(BusinessCardOCRDto dto)
  394. {
  395. double strSize = 1024 * 1024 * 7;
  396. if (dto.picBase64.Length > strSize)
  397. {
  398. return Ok(JsonView("图片不能大于7M!"));
  399. }
  400. try
  401. {
  402. var mlidpData = TencentOCRTools.GetOCR((int)TencentOCREnum.BusinessCard, dto);
  403. if (mlidpData.Code != 0)
  404. {
  405. return Ok(JsonView(mlidpData.Msg));
  406. }
  407. if (mlidpData.Data == null)
  408. {
  409. return Ok(JsonView(mlidpData.Msg));
  410. }
  411. return Ok(JsonView(mlidpData.Data));
  412. }
  413. catch (Exception ex)
  414. {
  415. return Ok(JsonView(false, ex.Message));
  416. }
  417. }
  418. #endregion
  419. #region android ios
  420. /// <summary>
  421. /// 名片识别
  422. /// 获取名片 移动端 返回数据
  423. /// </summary>
  424. /// <param name="dto"></param>
  425. /// <returns></returns>
  426. [HttpPost]
  427. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  428. public async Task<IActionResult> PostBusinessCardApp(BusinessCardOCRDto dto)
  429. {
  430. double strSize = 1024 * 1024 * 7;
  431. if (dto.picBase64.Length > strSize)
  432. {
  433. return Ok(JsonView("图片不能大于7M!"));
  434. }
  435. Logs(dto.picBase64);
  436. Logs("名片识别开始时间:" + DateTime.Now);
  437. try
  438. {
  439. var mlidpData = TencentOCRTools.PostOCRApp((int)TencentOCREnum.BusinessCard, dto);
  440. if (mlidpData.Code != 0)
  441. {
  442. return Ok(JsonView(mlidpData.Msg));
  443. }
  444. if (mlidpData.Data == null)
  445. {
  446. return Ok(JsonView(mlidpData.Msg));
  447. }
  448. Logs("名片识别结束时间:" + DateTime.Now);
  449. return Ok(JsonView(mlidpData.Data));
  450. }
  451. catch (Exception ex)
  452. {
  453. return Ok(JsonView(false, ex.Message));
  454. }
  455. }
  456. #endregion
  457. }
  458. }