TencentOCRController.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. using OASystem.API.OAMethodLib.File;
  2. using OASystem.API.OAMethodLib.TencentCloudAPI;
  3. using OASystem.Domain.Dtos.CRM;
  4. using OASystem.Domain.Dtos.Tencent;
  5. using OASystem.Domain.ViewModels.TencentOCR;
  6. using OASystem.Infrastructure.Repositories.CRM;
  7. using Org.BouncyCastle.Crypto;
  8. using StackExchange.Redis;
  9. using System.Net.NetworkInformation;
  10. using TencentCloud.Ocr.V20181119.Models;
  11. using Ubiety.Dns.Core;
  12. namespace OASystem.API.Controllers
  13. {
  14. /// <summary>
  15. /// TencentOCR 识别
  16. /// </summary>
  17. [Route("api/[controller]/[action]")]
  18. //[ApiController]
  19. public class TencentOCRController : ControllerBase
  20. {
  21. private readonly VisaDeleClientRepository _visaDeleClientRepository;
  22. public TencentOCRController(VisaDeleClientRepository visaDeleClientRepository)
  23. {
  24. this._visaDeleClientRepository = visaDeleClientRepository;
  25. }
  26. /// <summary>
  27. /// 通用印刷体
  28. /// </summary>
  29. /// <param name="dto"></param>
  30. /// <returns></returns>
  31. [HttpPost]
  32. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  33. public async Task<IActionResult> GetGeneralBasic(GeneralBasicOCRDto dto)
  34. {
  35. string[] picBase64Array = dto.picBase64.Split(';');
  36. string picFormat = picBase64Array[0].Split('/')[1];
  37. if (!TencentOCRTools.ImageType(picFormat))
  38. {
  39. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  40. }
  41. double strSize = 1024 * 1024 * 7;
  42. if (dto.picBase64.Length > strSize)
  43. {
  44. return Ok(JsonView("图片不能大于7M!"));
  45. }
  46. var gbData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.GeneralBasic, dto);
  47. if (gbData.Code != 0)
  48. {
  49. return Ok(JsonView(gbData.Msg));
  50. }
  51. if (gbData.Data == null)
  52. {
  53. return Ok(JsonView(gbData.Msg));
  54. }
  55. return Ok(JsonView(gbData.Data));
  56. }
  57. /// <summary>
  58. /// 身份证识别(国徽面/反面)
  59. /// 获取身份证返回数据
  60. /// </summary>
  61. /// <param name="dto"></param>
  62. /// <returns></returns>
  63. [HttpPost]
  64. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  65. public async Task<IActionResult> GetIDCard(IDCardOCRDto dto)
  66. {
  67. string[] picBase64Array = dto.picBase64.Split(';');
  68. string picFormat = picBase64Array[0].Split('/')[1];
  69. if (!TencentOCRTools.ImageType(picFormat))
  70. {
  71. return Ok(JsonView("图片格式不正确!只支持 PNG、JPG、JPEG、BMP 格式!"));
  72. }
  73. double strSize = 1024 * 1024 * 7;
  74. if (dto.picBase64.Length > strSize)
  75. {
  76. return Ok(JsonView("图片不能大于7M!"));
  77. }
  78. var idCardData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.IDCard, dto);
  79. if (idCardData.Code != 0)
  80. {
  81. return Ok(JsonView(idCardData.Msg));
  82. }
  83. if (idCardData.Data == null)
  84. {
  85. return Ok(JsonView(idCardData.Msg));
  86. }
  87. bool cerdStatus = await _visaDeleClientRepository.SetCrmUpdPassIdCardOCR(
  88. new SetCrmUpdPassIdCardOCRDto
  89. {
  90. UserId = dto.UserId,
  91. ClientName = idCardData.Data.Name,
  92. Sex = idCardData.Data.Sex == "男" ? 0 : 1,
  93. CerdNo = idCardData.Data.IdNum,
  94. CerdAddress = idCardData.Data.Address,
  95. });
  96. IDCardOCRAndDownUrlView iDCardOCRView = new IDCardOCRAndDownUrlView()
  97. {
  98. Status = cerdStatus,
  99. Name = idCardData.Data.Name,
  100. Sex = idCardData.Data.Sex,
  101. Nation = idCardData.Data.Nation,
  102. Birth = idCardData.Data.Birth,
  103. Address = idCardData.Data.Address,
  104. IdNum = idCardData.Data.IdNum,
  105. Authority = idCardData.Data.Authority,
  106. ValidDate = idCardData.Data.ValidDate
  107. };
  108. #region word生成 返回地址
  109. Dictionary<string, object> dic = new Dictionary<string, object>();
  110. dic.Add("Name", iDCardOCRView.Name);
  111. dic.Add("Sex", iDCardOCRView.Sex);
  112. dic.Add("Nation", iDCardOCRView.Nation);
  113. dic.Add("Birth", iDCardOCRView.Birth);
  114. dic.Add("Address", iDCardOCRView.Address);
  115. dic.Add("IdNum", iDCardOCRView.IdNum);
  116. dic.Add("Authority", iDCardOCRView.Authority);
  117. dic.Add("ValidDate", iDCardOCRView.ValidDate);
  118. string fileName = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)" + DateTime.Now.ToString("yyyyMMddHHmmss"));
  119. string serverPath = AsposeHelper.ExpertWordToModel("ocr_身份证(人像面).doc", "TencentOCR", fileName, dic, null);
  120. iDCardOCRView.DownUrl = serverPath;
  121. #endregion
  122. return Ok(JsonView(iDCardOCRView));
  123. }
  124. /// <summary>
  125. /// 户口簿识别
  126. /// 获取户口簿返回数据
  127. /// </summary>
  128. /// <param name="dto"></param>
  129. /// <returns></returns>
  130. [HttpPost]
  131. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  132. public async Task<IActionResult> GetResidenceBookletOCR(ResidenceBookletOCRDto dto)
  133. {
  134. double strSize = 1024 * 1024 * 7;
  135. if (dto.picBase64.Length > strSize)
  136. {
  137. return Ok(JsonView("图片不能大于7M!"));
  138. }
  139. var residenceBookData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.ResidenceBooklet, dto);
  140. if (residenceBookData.Code != 0)
  141. {
  142. return Ok(JsonView(residenceBookData.Msg));
  143. }
  144. if (residenceBookData.Data == null)
  145. {
  146. return Ok(JsonView(residenceBookData.Msg));
  147. }
  148. return Ok(JsonView(residenceBookData.Data));
  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> GetBizLicenseOCR(BizLicenseOCRDto 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)TencentOCR.TencentOCREnum.BizLicense, 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> GetOrgCodeCertOCR(OrgCodeCertOCRDto dto)
  185. {
  186. double strSize = 1024 * 1024 * 7;
  187. if (dto.picBase64.Length > strSize)
  188. {
  189. return Ok(JsonView("图片不能大于7M!"));
  190. }
  191. var occData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.OrgCodeCert, dto);
  192. if (occData.Code != 0)
  193. {
  194. return Ok(JsonView(occData.Msg));
  195. }
  196. if (occData.Data == null)
  197. {
  198. return Ok(JsonView(occData.Msg));
  199. }
  200. return Ok(JsonView(occData.Data));
  201. }
  202. /// <summary>
  203. /// 行驶证识别
  204. /// 获取行驶证返回数据
  205. /// CardSide=0 主页正面(有红色印章的一面),CardSide=1 行驶证副页正面(有号码号牌的一面),CardSide=2 行驶证主页正面和副页正面。
  206. /// </summary>
  207. /// <param name="dto"></param>
  208. /// <returns></returns>
  209. [HttpPost]
  210. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  211. public async Task<IActionResult> GetVehicleLicense(VehicleLicenseOCRDto dto)
  212. {
  213. double strSize = 1024 * 1024 * 7;
  214. if (dto.picBase64.Length > strSize)
  215. {
  216. return Ok(JsonView("图片不能大于7M!"));
  217. }
  218. var vehicleLicenseData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.VehicleLicense, dto);
  219. if (vehicleLicenseData.Code != 0)
  220. {
  221. return Ok(JsonView(vehicleLicenseData.Msg));
  222. }
  223. if (vehicleLicenseData.Data == null)
  224. {
  225. return Ok(JsonView(vehicleLicenseData.Msg));
  226. }
  227. return Ok(JsonView(vehicleLicenseData.Data));
  228. }
  229. /// <summary>
  230. /// 房产证识别
  231. /// 获取房产证返回数据
  232. /// </summary>
  233. /// <param name="dto"></param>
  234. /// <returns></returns>
  235. [HttpPost]
  236. [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)]
  237. public async Task<IActionResult> GetPropOwnerCert(PropOwnerCertOCRDto dto)
  238. {
  239. double strSize = 1024 * 1024 * 7;
  240. if (dto.picBase64.Length > strSize)
  241. {
  242. return Ok(JsonView("图片不能大于7M!"));
  243. }
  244. var pcData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.PropOwnerCert, dto);
  245. if (pcData.Code != 0)
  246. {
  247. return Ok(JsonView(pcData.Msg));
  248. }
  249. if (pcData.Data == null)
  250. {
  251. return Ok(JsonView(pcData.Msg));
  252. }
  253. return Ok(JsonView(pcData.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> GetMarriageLicense(MarriageLicenseOCRDto dto)
  264. {
  265. double strSize = 1024 * 1024 * 7;
  266. if (dto.picBase64.Length > strSize)
  267. {
  268. return Ok(JsonView("图片不能大于7M!"));
  269. }
  270. var mlData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.GeneralBasic, dto);
  271. if (mlData.Code != 0)
  272. {
  273. return Ok(JsonView(mlData.Msg));
  274. }
  275. if (mlData.Data == null)
  276. {
  277. return Ok(JsonView(mlData.Msg));
  278. }
  279. return Ok(JsonView(mlData.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> GetUniformCreditCode(UniformCreditCodeOCRDto dto)
  290. {
  291. double strSize = 1024 * 1024 * 7;
  292. if (dto.picBase64.Length > strSize)
  293. {
  294. return Ok(JsonView("图片不能大于7M!"));
  295. }
  296. var gbData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.GeneralBasic, dto);
  297. if (gbData.Code != 0)
  298. {
  299. return Ok(JsonView(gbData.Msg));
  300. }
  301. if (gbData.Data == null)
  302. {
  303. return Ok(JsonView(gbData.Msg));
  304. }
  305. return Ok(JsonView(gbData.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> GetPassport(PassportOCRDto dto)
  316. {
  317. double strSize = 1024 * 1024 * 7;
  318. if (dto.picBase64.Length > strSize)
  319. {
  320. return Ok(JsonView("图片不能大于7M!"));
  321. }
  322. var pData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.Passport, dto);
  323. if (pData.Code != 0)
  324. {
  325. return Ok(JsonView(pData.Msg));
  326. }
  327. if (pData.Data == null)
  328. {
  329. return Ok(JsonView(pData.Msg));
  330. }
  331. return Ok(JsonView(pData.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> GetMLIDPassport(PassportOCRDto dto)
  342. {
  343. double strSize = 1024 * 1024 * 7;
  344. if (dto.picBase64.Length > strSize)
  345. {
  346. return Ok(JsonView("图片不能大于7M!"));
  347. }
  348. var mlidpData = TencentOCRTools.GetOCR((int)TencentOCR.TencentOCREnum.MLIDPassport, dto);
  349. if (mlidpData.Code != 0)
  350. {
  351. return Ok(JsonView(mlidpData.Msg));
  352. }
  353. if (mlidpData.Data == null)
  354. {
  355. return Ok(JsonView(mlidpData.Msg));
  356. }
  357. return Ok(JsonView(mlidpData.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> GetBusinessCard(BusinessCardOCRDto 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)TencentOCR.TencentOCREnum.BusinessCard, 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. }
  386. }