TencentOCRController.cs 15 KB

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