TencentOCRController.cs 17 KB

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