TencentOCRController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. Dictionary<string, object> dic = new Dictionary<string, object>();
  111. dic.Add("Name", iDCardOCRView.Name);
  112. dic.Add("Sex", iDCardOCRView.Sex);
  113. dic.Add("Nation", iDCardOCRView.Nation);
  114. dic.Add("Birth", iDCardOCRView.Birth);
  115. dic.Add("Address", iDCardOCRView.Address);
  116. dic.Add("IdNum", iDCardOCRView.IdNum);
  117. dic.Add("Authority", iDCardOCRView.Authority);
  118. dic.Add("ValidDate", iDCardOCRView.ValidDate);
  119. string fileName = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  120. string serverPath = AsposeHelper.ExpertWordToModel("ocr_身份证(人像面).doc", "TencentOCR", fileName, dic, null);
  121. iDCardOCRView.DownUrl = serverPath;
  122. #endregion
  123. return Ok(JsonView(iDCardOCRView));
  124. }
  125. /// <summary>
  126. /// 户口簿识别
  127. /// 获取户口簿返回数据
  128. /// </summary>
  129. /// <param name="dto"></param>
  130. /// <returns></returns>
  131. [HttpPost]
  132. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  133. public async Task<IActionResult> GetResidenceBookletOCR(ResidenceBookletOCRDto dto)
  134. {
  135. double strSize = 1024 * 1024 * 7;
  136. if (dto.picBase64.Length > strSize)
  137. {
  138. return Ok(JsonView("图片不能大于7M!"));
  139. }
  140. var residenceBookData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.ResidenceBooklet, dto);
  141. if (residenceBookData.Code != 0)
  142. {
  143. return Ok(JsonView(residenceBookData.Msg));
  144. }
  145. if (residenceBookData.Data == null)
  146. {
  147. return Ok(JsonView(residenceBookData.Msg));
  148. }
  149. return Ok(JsonView(residenceBookData.Data));
  150. }
  151. /// <summary>
  152. /// 营业执照识别
  153. /// 获取营业执照返回数据
  154. /// </summary>
  155. /// <param name="dto"></param>
  156. /// <returns></returns>
  157. [HttpPost]
  158. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  159. public async Task<IActionResult> GetBizLicenseOCR(BizLicenseOCRDto dto)
  160. {
  161. double strSize = 1024 * 1024 * 7;
  162. if (dto.picBase64.Length > strSize)
  163. {
  164. return Ok(JsonView("图片不能大于7M!"));
  165. }
  166. var residenceBookData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.BizLicense, dto);
  167. if (residenceBookData.Code != 0)
  168. {
  169. return Ok(JsonView(residenceBookData.Msg));
  170. }
  171. if (residenceBookData.Data == null)
  172. {
  173. return Ok(JsonView(residenceBookData.Msg));
  174. }
  175. return Ok(JsonView(residenceBookData.Data));
  176. }
  177. /// <summary>
  178. /// 营业执照识别
  179. /// 获取营业执照返回数据
  180. /// </summary>
  181. /// <param name="dto"></param>
  182. /// <returns></returns>
  183. [HttpPost]
  184. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  185. public async Task<IActionResult> GetOrgCodeCertOCR(OrgCodeCertOCRDto dto)
  186. {
  187. double strSize = 1024 * 1024 * 7;
  188. if (dto.picBase64.Length > strSize)
  189. {
  190. return Ok(JsonView("图片不能大于7M!"));
  191. }
  192. var occData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.OrgCodeCert, dto);
  193. if (occData.Code != 0)
  194. {
  195. return Ok(JsonView(occData.Msg));
  196. }
  197. if (occData.Data == null)
  198. {
  199. return Ok(JsonView(occData.Msg));
  200. }
  201. return Ok(JsonView(occData.Data));
  202. }
  203. /// <summary>
  204. /// 行驶证识别
  205. /// 获取行驶证返回数据
  206. /// CardSide=0 主页正面(有红色印章的一面),CardSide=1 行驶证副页正面(有号码号牌的一面),CardSide=2 行驶证主页正面和副页正面。
  207. /// </summary>
  208. /// <param name="dto"></param>
  209. /// <returns></returns>
  210. [HttpPost]
  211. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  212. public async Task<IActionResult> GetVehicleLicense(VehicleLicenseOCRDto dto)
  213. {
  214. double strSize = 1024 * 1024 * 7;
  215. if (dto.picBase64.Length > strSize)
  216. {
  217. return Ok(JsonView("图片不能大于7M!"));
  218. }
  219. var vehicleLicenseData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.VehicleLicense, dto);
  220. if (vehicleLicenseData.Code != 0)
  221. {
  222. return Ok(JsonView(vehicleLicenseData.Msg));
  223. }
  224. if (vehicleLicenseData.Data == null)
  225. {
  226. return Ok(JsonView(vehicleLicenseData.Msg));
  227. }
  228. return Ok(JsonView(vehicleLicenseData.Data));
  229. }
  230. /// <summary>
  231. /// 房产证识别
  232. /// 获取房产证返回数据
  233. /// </summary>
  234. /// <param name="dto"></param>
  235. /// <returns></returns>
  236. [HttpPost]
  237. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  238. public async Task<IActionResult> GetPropOwnerCert(PropOwnerCertOCRDto dto)
  239. {
  240. double strSize = 1024 * 1024 * 7;
  241. if (dto.picBase64.Length > strSize)
  242. {
  243. return Ok(JsonView("图片不能大于7M!"));
  244. }
  245. var pcData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.PropOwnerCert, dto);
  246. if (pcData.Code != 0)
  247. {
  248. return Ok(JsonView(pcData.Msg));
  249. }
  250. if (pcData.Data == null)
  251. {
  252. return Ok(JsonView(pcData.Msg));
  253. }
  254. return Ok(JsonView(pcData.Data));
  255. }
  256. /// <summary>
  257. /// 结婚证识别
  258. /// 获取结婚证返回数据
  259. /// </summary>
  260. /// <param name="dto"></param>
  261. /// <returns></returns>
  262. [HttpPost]
  263. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  264. public async Task<IActionResult> GetMarriageLicense(MarriageLicenseOCRDto dto)
  265. {
  266. double strSize = 1024 * 1024 * 7;
  267. if (dto.picBase64.Length > strSize)
  268. {
  269. return Ok(JsonView("图片不能大于7M!"));
  270. }
  271. var mlData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.GeneralBasic, dto);
  272. if (mlData.Code != 0)
  273. {
  274. return Ok(JsonView(mlData.Msg));
  275. }
  276. if (mlData.Data == null)
  277. {
  278. return Ok(JsonView(mlData.Msg));
  279. }
  280. return Ok(JsonView(mlData.Data));
  281. }
  282. /// <summary>
  283. /// 统一信用代码识别
  284. /// 获取统一信用代码返回数据
  285. /// </summary>
  286. /// <param name="dto"></param>
  287. /// <returns></returns>
  288. [HttpPost]
  289. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  290. public async Task<IActionResult> GetUniformCreditCode(UniformCreditCodeOCRDto dto)
  291. {
  292. double strSize = 1024 * 1024 * 7;
  293. if (dto.picBase64.Length > strSize)
  294. {
  295. return Ok(JsonView("图片不能大于7M!"));
  296. }
  297. var gbData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.GeneralBasic, dto);
  298. if (gbData.Code != 0)
  299. {
  300. return Ok(JsonView(gbData.Msg));
  301. }
  302. if (gbData.Data == null)
  303. {
  304. return Ok(JsonView(gbData.Msg));
  305. }
  306. return Ok(JsonView(gbData.Data));
  307. }
  308. /// <summary>
  309. /// 护照识别(中国大陆地区护照)
  310. /// 获取护照识别(中国大陆地区护照)返回数据
  311. /// </summary>
  312. /// <param name="dto"></param>
  313. /// <returns></returns>
  314. [HttpPost]
  315. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  316. public async Task<IActionResult> GetPassport(PassportOCRDto dto)
  317. {
  318. double strSize = 1024 * 1024 * 7;
  319. if (dto.picBase64.Length > strSize)
  320. {
  321. return Ok(JsonView("图片不能大于7M!"));
  322. }
  323. var pData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.Passport, dto);
  324. if (pData.Code != 0)
  325. {
  326. return Ok(JsonView(pData.Msg));
  327. }
  328. if (pData.Data == null)
  329. {
  330. return Ok(JsonView(pData.Msg));
  331. }
  332. return Ok(JsonView(pData.Data));
  333. }
  334. /// <summary>
  335. /// 护照识别(港澳台地区及境外护照)
  336. /// 获取护照识别(港澳台地区及境外护照)返回数据
  337. /// </summary>
  338. /// <param name="dto"></param>
  339. /// <returns></returns>
  340. [HttpPost]
  341. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  342. public async Task<IActionResult> GetMLIDPassport(PassportOCRDto dto)
  343. {
  344. double strSize = 1024 * 1024 * 7;
  345. if (dto.picBase64.Length > strSize)
  346. {
  347. return Ok(JsonView("图片不能大于7M!"));
  348. }
  349. var mlidpData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.MLIDPassport, dto);
  350. if (mlidpData.Code != 0)
  351. {
  352. return Ok(JsonView(mlidpData.Msg));
  353. }
  354. if (mlidpData.Data == null)
  355. {
  356. return Ok(JsonView(mlidpData.Msg));
  357. }
  358. return Ok(JsonView(mlidpData.Data));
  359. }
  360. /// <summary>
  361. /// 名片识别
  362. /// 获取名片 返回数据
  363. /// </summary>
  364. /// <param name="dto"></param>
  365. /// <returns></returns>
  366. [HttpPost]
  367. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  368. public async Task<IActionResult> GetBusinessCard(BusinessCardOCRDto dto)
  369. {
  370. double strSize = 1024 * 1024 * 7;
  371. if (dto.picBase64.Length > strSize)
  372. {
  373. return Ok(JsonView("图片不能大于7M!"));
  374. }
  375. var mlidpData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.BusinessCard, dto);
  376. if (mlidpData.Code != 0)
  377. {
  378. return Ok(JsonView(mlidpData.Msg));
  379. }
  380. if (mlidpData.Data == null)
  381. {
  382. return Ok(JsonView(mlidpData.Msg));
  383. }
  384. return Ok(JsonView(mlidpData.Data));
  385. }
  386. }
  387. }