TencentOCRTools.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. using OASystem.Domain.Dtos.Tencent;
  2. using TencentCloud.Common;
  3. using TencentCloud.Ocr.V20181119.Models;
  4. using TencentCloud.Ocr.V20181119;
  5. using OASystem.Domain.ViewModels.OCR;
  6. using Aspose.Words;
  7. using NPOI.SS.Formula.Functions;
  8. namespace OASystem.API.OAMethodLib.TencentCloudAPI
  9. {
  10. /// <summary>
  11. /// 腾讯OCR Tools
  12. /// </summary>
  13. public static class TencentOCRTools
  14. {
  15. /// <summary>
  16. /// 初始化 腾讯云账户密钥对 SecretId,SecretKey
  17. /// </summary>
  18. static Credential cred = new Credential
  19. {
  20. SecretId = "AKIDoQUHLBELagatzoScfdMpMddMnz8apqIc",
  21. SecretKey = "hKiGbhkX5NWWXu3zDWwAATVK3e5sWuBB"
  22. };
  23. /// <summary>
  24. /// ocr识别 Web
  25. /// </summary>
  26. /// <param name="ocrEnum">转换类型</param>
  27. /// <param name="data">请求数据源</param>
  28. /// <returns></returns>
  29. public static async Task<Result> GetOCR(int ocrEnum, object data)
  30. {
  31. Result result = new Result { Code = -1, Msg = "未知错误", Data = null };
  32. OcrClient client = new OcrClient(cred, "ap-guangzhou");
  33. try
  34. {
  35. switch (ocrEnum)
  36. {
  37. case 0: //身份证识别
  38. IDCardOCRDto iDCardDto = (IDCardOCRDto)data;
  39. IDCardOCRRequest req = new IDCardOCRRequest();
  40. if (iDCardDto.CardSide == 0) req.CardSide = "FRONT";
  41. else if (iDCardDto.CardSide == 1) req.CardSide = "BACK";
  42. req.ImageBase64 = iDCardDto.picBase64;
  43. IDCardOCRResponse resp = client.IDCardOCRSync(req);
  44. result.Data = resp;
  45. break;
  46. case 1: //户口本识别
  47. ResidenceBookletOCRDto rbOCR = (ResidenceBookletOCRDto)data;
  48. ResidenceBookletOCRRequest rbReq = new ResidenceBookletOCRRequest();
  49. rbReq.ImageBase64 = rbOCR.picBase64;
  50. ResidenceBookletOCRResponse rbRep = client.ResidenceBookletOCRSync(rbReq);
  51. result.Data = rbRep;
  52. break;
  53. case 2: //营业执照识别 OrgCodeCert
  54. BizLicenseOCRDto blOCR = (BizLicenseOCRDto)data;
  55. BizLicenseOCRRequest blReq = new BizLicenseOCRRequest();
  56. blReq.ImageBase64 = blOCR.picBase64;
  57. BizLicenseOCRResponse blRep = client.BizLicenseOCRSync(blReq);
  58. result.Data = blRep;
  59. break;
  60. case 3: //组织机构代码证识别
  61. OrgCodeCertOCRDto occOCR = (OrgCodeCertOCRDto)data;
  62. OrgCodeCertOCRRequest ocReq = new OrgCodeCertOCRRequest();
  63. ocReq.ImageBase64 = occOCR.picBase64;
  64. OrgCodeCertOCRResponse occReq = client.OrgCodeCertOCRSync(ocReq);
  65. result.Data = occReq;
  66. break;
  67. case 4: //行驶证识别
  68. VehicleLicenseOCRDto vlOCR = (VehicleLicenseOCRDto)data;
  69. VehicleLicenseOCRRequest vlReq = new VehicleLicenseOCRRequest();
  70. if (vlOCR.CardSide == 0) vlReq.CardSide = "FRONT";
  71. else if (vlOCR.CardSide == 1) vlReq.CardSide = "BACK";
  72. else if (vlOCR.CardSide == 2) vlReq.CardSide = "DOUBLE";
  73. vlReq.ImageBase64 = vlOCR.picBase64;
  74. VehicleLicenseOCRResponse vlRep = client.VehicleLicenseOCRSync(vlReq);
  75. result.Data = vlRep;
  76. break;
  77. case 5: //房产证识别 通用文字识别
  78. PropOwnerCertOCRDto pocOCR = (PropOwnerCertOCRDto)data;
  79. PropOwnerCertOCRRequest pocReq = new PropOwnerCertOCRRequest();
  80. pocReq.ImageBase64 = pocOCR.picBase64;
  81. PropOwnerCertOCRResponse pocRep = client.PropOwnerCertOCRSync(pocReq);
  82. result.Data = pocRep;
  83. break;
  84. case 6: // 通用文字识别
  85. GeneralBasicOCRDto gbOCR = (GeneralBasicOCRDto)data;
  86. GeneralBasicOCRRequest gbReq = new GeneralBasicOCRRequest();
  87. gbReq.ImageBase64 = gbOCR.picBase64;
  88. GeneralBasicOCRResponse gbRep = client.GeneralBasicOCRSync(gbReq);
  89. result.Data = gbRep;
  90. break;
  91. case 7: // 护照识别(中国大陆地区护照)
  92. PassportOCRDto pOCR = (PassportOCRDto)data;
  93. PassportOCRRequest pReq = new PassportOCRRequest();
  94. pReq.ImageBase64 = pOCR.picBase64;
  95. PassportOCRResponse pRep = client.PassportOCRSync(pReq);
  96. result.Data = pRep;
  97. break;
  98. case 8: // 护照识别(港澳台地区及境外护照)
  99. MLIDPassportOCRDto mlidpOCR = (MLIDPassportOCRDto)data;
  100. MLIDPassportOCRRequest mlidpReq = new MLIDPassportOCRRequest();
  101. mlidpReq.ImageBase64 = mlidpOCR.picBase64;
  102. MLIDPassportOCRResponse mlidpRep = client.MLIDPassportOCRSync(mlidpReq);
  103. result.Data = mlidpRep;
  104. break;
  105. case 9: // 护照识别(港澳台地区及境外护照)
  106. BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data;
  107. BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest();
  108. bcReq.ImageBase64 = bcOCR.picBase64;
  109. BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq);
  110. result.Data = bcRep;
  111. break;
  112. case 10: // 智能结构化识别V2
  113. //SmartStructuralOCRV2
  114. BusinessCardOCRDto ssOCR = (BusinessCardOCRDto)data;
  115. SmartStructuralOCRRequest ssReq = new SmartStructuralOCRRequest()
  116. {
  117. ImageBase64 = ssOCR.picBase64,
  118. IsPdf = true,
  119. ReturnFullText = true
  120. };
  121. SmartStructuralOCRResponse ssRep = client.SmartStructuralOCRSync(ssReq);
  122. List<StructuralItem> structuralItems = ssRep.StructuralItems.ToList();
  123. if (structuralItems.Count() < 1 )
  124. {
  125. return new Result() { Code = -1, Msg = "智能结构化识别V2:未识别出可用信息" };
  126. }
  127. List<ocrGeneralView> _views = structuralItems.Select(it => new ocrGeneralView() { name = it.Name, value = it.Value }).ToList();
  128. result.Data = _views;
  129. break;
  130. }
  131. result.Code = 0;
  132. result.Msg = "成功";
  133. return result;
  134. }
  135. catch (Exception ex)
  136. {
  137. result.Msg = "TencentOCRAPIError:" + ex.Message;
  138. return result;
  139. }
  140. }
  141. /// <summary>
  142. /// ocr识别 android ios
  143. /// </summary>
  144. /// <param name="ocrEnum">转换类型</param>
  145. /// <param name="data">请求数据源</param>
  146. /// <returns></returns>
  147. public static Result PostOCRApp(int ocrEnum, object data)
  148. {
  149. Result result = new Result { Code = -1, Msg = "未知错误", Data = null };
  150. OcrClient client = new OcrClient(cred, "ap-guangzhou");
  151. try
  152. {
  153. switch (ocrEnum)
  154. {
  155. case 9: // 名片识别
  156. BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data;
  157. BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest();
  158. bcReq.ImageBase64 = bcOCR.picBase64;
  159. BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq);
  160. BusinessCardInfoView businessCardInfo = new BusinessCardInfoView();
  161. if (bcRep.BusinessCardInfos.Length > 0)
  162. {
  163. List<BusinessCardInfo> BusinessCardInfos = new List<BusinessCardInfo>();
  164. BusinessCardInfos = bcRep.BusinessCardInfos.ToList();
  165. var nameData = BusinessCardInfos.Where(x => x.Name == "姓名").FirstOrDefault();
  166. if (nameData != null) businessCardInfo.Name = nameData.Value;
  167. var companyData = BusinessCardInfos.Where(x => x.Name.Equals("公司")).FirstOrDefault();
  168. if (companyData != null) businessCardInfo.Company = companyData.Value;
  169. var addressData = BusinessCardInfos.Where(x => x.Name == "地址").FirstOrDefault();
  170. if (addressData != null) businessCardInfo.Address = addressData.Value;
  171. var jobData = BusinessCardInfos.Where(x => x.Name == "职位").FirstOrDefault();
  172. if (jobData != null) businessCardInfo.Job = jobData.Value;
  173. var emailData = BusinessCardInfos.Where(x => x.Name == "邮箱").FirstOrDefault();
  174. if (emailData != null) businessCardInfo.Email = emailData.Value;
  175. var phoneData = BusinessCardInfos.Where(x => x.Name == "手机").ToList();
  176. if (phoneData.Count > 0)
  177. {
  178. string phone = "";
  179. foreach (var item in phoneData)
  180. {
  181. phone += item.Value + ",";
  182. }
  183. if (phone.Length > 0 )
  184. {
  185. phone = phone.Substring(0,phone.Length - 1);
  186. }
  187. businessCardInfo.Phone = phone;
  188. }
  189. var telData = BusinessCardInfos.Where(x => x.Name == "电话").ToList();
  190. if (telData.Count > 0)
  191. {
  192. string tel = "";
  193. foreach (var item in telData)
  194. {
  195. tel += item.Value + ",";
  196. }
  197. if (tel.Length > 0)
  198. {
  199. tel = tel.Substring(0,tel.Length - 1);
  200. }
  201. businessCardInfo.Tel = tel;
  202. }
  203. }
  204. result.Data = businessCardInfo;
  205. break;
  206. }
  207. result.Code = 0;
  208. result.Msg = "成功";
  209. return result;
  210. }
  211. catch (Exception ex)
  212. {
  213. result.Msg = "TencentOCRAPIError:" + ex.Message;
  214. return result;
  215. }
  216. }
  217. /// <summary>
  218. /// 图片转Base64
  219. /// </summary>
  220. /// <param name="ImageFileName">图片的完整路径</param>
  221. /// <returns></returns>
  222. public static string ImgToBase64(string ImageFileName)
  223. {
  224. try
  225. {
  226. Bitmap bmp = new Bitmap(ImageFileName);
  227. MemoryStream ms = new MemoryStream();
  228. bmp.Save(ms, ImageFormat.Jpeg);
  229. byte[] arr = new byte[ms.Length];
  230. ms.Position = 0;
  231. ms.Read(arr, 0, (int)ms.Length);
  232. ms.Close();
  233. return Convert.ToBase64String(arr);
  234. }
  235. catch (Exception e)
  236. {
  237. return e.Message.ToString();
  238. }
  239. }
  240. /// <summary>
  241. /// TencentOCR图片类型处理
  242. /// PNG、JPG、JPEG、BMP
  243. /// </summary>
  244. /// <param name="type"></param>
  245. /// <returns></returns>
  246. public static bool ImageType(string type)
  247. {
  248. if (type.ToLower() == "png" || type.ToLower() == "jpg" || type.ToLower() == "jpeg" || type.ToLower() == "bmp")
  249. return true;
  250. return false;
  251. }
  252. private const double KBCount = 1024;
  253. private const double MBCount = KBCount * 1024;
  254. private const double GBCount = MBCount * 1024;
  255. private const double TBCount = GBCount * 1024;
  256. /// <summary>
  257. /// 得到适应的大小
  258. /// </summary>
  259. /// <param name="path"></param>
  260. /// <returns>string</returns>
  261. public static string GetAutoSizeString(double size, int roundCount)
  262. {
  263. if (KBCount > size)
  264. {
  265. return Math.Round(size, roundCount) + "B";
  266. }
  267. else if (MBCount > size)
  268. {
  269. return Math.Round(size / KBCount, roundCount) + "KB";
  270. }
  271. else if (GBCount > size)
  272. {
  273. return Math.Round(size / MBCount, roundCount) + "MB";
  274. }
  275. else if (TBCount > size)
  276. {
  277. return Math.Round(size / GBCount, roundCount) + "GB";
  278. }
  279. else
  280. {
  281. return Math.Round(size / TBCount, roundCount) + "TB";
  282. }
  283. }
  284. }
  285. }