TencentOCRTools.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. gbReq.IsPdf = true;
  89. GeneralBasicOCRResponse gbRep = client.GeneralBasicOCRSync(gbReq);
  90. result.Data = gbRep;
  91. break;
  92. case 7: // 护照识别(中国大陆地区护照)
  93. PassportOCRDto pOCR = (PassportOCRDto)data;
  94. PassportOCRRequest pReq = new PassportOCRRequest();
  95. pReq.ImageBase64 = pOCR.picBase64;
  96. PassportOCRResponse pRep = client.PassportOCRSync(pReq);
  97. result.Data = pRep;
  98. break;
  99. case 8: // 护照识别(港澳台地区及境外护照)
  100. MLIDPassportOCRDto mlidpOCR = (MLIDPassportOCRDto)data;
  101. MLIDPassportOCRRequest mlidpReq = new MLIDPassportOCRRequest();
  102. mlidpReq.ImageBase64 = mlidpOCR.picBase64;
  103. MLIDPassportOCRResponse mlidpRep = client.MLIDPassportOCRSync(mlidpReq);
  104. result.Data = mlidpRep;
  105. break;
  106. case 9: // 护照识别(港澳台地区及境外护照)
  107. BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data;
  108. BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest();
  109. bcReq.ImageBase64 = bcOCR.picBase64;
  110. BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq);
  111. result.Data = bcRep;
  112. break;
  113. case 10: // 智能结构化识别V2
  114. //SmartStructuralOCRV2
  115. BusinessCardOCRDto ssOCR = (BusinessCardOCRDto)data;
  116. SmartStructuralOCRRequest ssReq = new SmartStructuralOCRRequest()
  117. {
  118. ImageBase64 = ssOCR.picBase64,
  119. IsPdf = true,
  120. ReturnFullText = true
  121. };
  122. SmartStructuralOCRResponse ssRep = client.SmartStructuralOCRSync(ssReq);
  123. List<StructuralItem> structuralItems = ssRep.StructuralItems.ToList();
  124. if (structuralItems.Count() < 1 )
  125. {
  126. return new Result() { Code = -1, Msg = "智能结构化识别V2:未识别出可用信息" };
  127. }
  128. List<ocrGeneralView> _views = structuralItems.Select(it => new ocrGeneralView() { name = it.Name, value = it.Value }).ToList();
  129. result.Data = _views;
  130. break;
  131. }
  132. result.Code = 0;
  133. result.Msg = "成功";
  134. return result;
  135. }
  136. catch (Exception ex)
  137. {
  138. result.Msg = "TencentOCRAPIError:" + ex.Message;
  139. return result;
  140. }
  141. }
  142. /// <summary>
  143. /// ocr识别 android ios
  144. /// </summary>
  145. /// <param name="ocrEnum">转换类型</param>
  146. /// <param name="data">请求数据源</param>
  147. /// <returns></returns>
  148. public static Result PostOCRApp(int ocrEnum, object data)
  149. {
  150. Result result = new Result { Code = -1, Msg = "未知错误", Data = null };
  151. OcrClient client = new OcrClient(cred, "ap-guangzhou");
  152. try
  153. {
  154. switch (ocrEnum)
  155. {
  156. case 9: // 名片识别
  157. BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data;
  158. BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest();
  159. bcReq.ImageBase64 = bcOCR.picBase64;
  160. BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq);
  161. BusinessCardInfoView businessCardInfo = new BusinessCardInfoView();
  162. if (bcRep.BusinessCardInfos.Length > 0)
  163. {
  164. List<BusinessCardInfo> BusinessCardInfos = new List<BusinessCardInfo>();
  165. BusinessCardInfos = bcRep.BusinessCardInfos.ToList();
  166. var nameData = BusinessCardInfos.Where(x => x.Name == "姓名").FirstOrDefault();
  167. if (nameData != null) businessCardInfo.Name = nameData.Value;
  168. var companyData = BusinessCardInfos.Where(x => x.Name.Equals("公司")).FirstOrDefault();
  169. if (companyData != null) businessCardInfo.Company = companyData.Value;
  170. var addressData = BusinessCardInfos.Where(x => x.Name == "地址").FirstOrDefault();
  171. if (addressData != null) businessCardInfo.Address = addressData.Value;
  172. var jobData = BusinessCardInfos.Where(x => x.Name == "职位").FirstOrDefault();
  173. if (jobData != null) businessCardInfo.Job = jobData.Value;
  174. var emailData = BusinessCardInfos.Where(x => x.Name == "邮箱").FirstOrDefault();
  175. if (emailData != null) businessCardInfo.Email = emailData.Value;
  176. var phoneData = BusinessCardInfos.Where(x => x.Name == "手机").ToList();
  177. if (phoneData.Count > 0)
  178. {
  179. string phone = "";
  180. foreach (var item in phoneData)
  181. {
  182. phone += item.Value + ",";
  183. }
  184. if (phone.Length > 0 )
  185. {
  186. phone = phone.Substring(0,phone.Length - 1);
  187. }
  188. businessCardInfo.Phone = phone;
  189. }
  190. var telData = BusinessCardInfos.Where(x => x.Name == "电话").ToList();
  191. if (telData.Count > 0)
  192. {
  193. string tel = "";
  194. foreach (var item in telData)
  195. {
  196. tel += item.Value + ",";
  197. }
  198. if (tel.Length > 0)
  199. {
  200. tel = tel.Substring(0,tel.Length - 1);
  201. }
  202. businessCardInfo.Tel = tel;
  203. }
  204. }
  205. result.Data = businessCardInfo;
  206. break;
  207. }
  208. result.Code = 0;
  209. result.Msg = "成功";
  210. return result;
  211. }
  212. catch (Exception ex)
  213. {
  214. result.Msg = "TencentOCRAPIError:" + ex.Message;
  215. return result;
  216. }
  217. }
  218. /// <summary>
  219. /// 图片转Base64
  220. /// </summary>
  221. /// <param name="ImageFileName">图片的完整路径</param>
  222. /// <returns></returns>
  223. public static string ImgToBase64(string ImageFileName)
  224. {
  225. try
  226. {
  227. Bitmap bmp = new Bitmap(ImageFileName);
  228. MemoryStream ms = new MemoryStream();
  229. bmp.Save(ms, ImageFormat.Jpeg);
  230. byte[] arr = new byte[ms.Length];
  231. ms.Position = 0;
  232. ms.Read(arr, 0, (int)ms.Length);
  233. ms.Close();
  234. return Convert.ToBase64String(arr);
  235. }
  236. catch (Exception e)
  237. {
  238. return e.Message.ToString();
  239. }
  240. }
  241. /// <summary>
  242. /// TencentOCR图片类型处理
  243. /// PNG、JPG、JPEG、BMP
  244. /// </summary>
  245. /// <param name="type"></param>
  246. /// <returns></returns>
  247. public static bool ImageType(string type)
  248. {
  249. if (type.ToLower() == "png" || type.ToLower() == "jpg" || type.ToLower() == "jpeg" || type.ToLower() == "bmp")
  250. return true;
  251. return false;
  252. }
  253. private const double KBCount = 1024;
  254. private const double MBCount = KBCount * 1024;
  255. private const double GBCount = MBCount * 1024;
  256. private const double TBCount = GBCount * 1024;
  257. /// <summary>
  258. /// 得到适应的大小
  259. /// </summary>
  260. /// <param name="path"></param>
  261. /// <returns>string</returns>
  262. public static string GetAutoSizeString(double size, int roundCount)
  263. {
  264. if (KBCount > size)
  265. {
  266. return Math.Round(size, roundCount) + "B";
  267. }
  268. else if (MBCount > size)
  269. {
  270. return Math.Round(size / KBCount, roundCount) + "KB";
  271. }
  272. else if (GBCount > size)
  273. {
  274. return Math.Round(size / MBCount, roundCount) + "MB";
  275. }
  276. else if (TBCount > size)
  277. {
  278. return Math.Round(size / GBCount, roundCount) + "GB";
  279. }
  280. else
  281. {
  282. return Math.Round(size / TBCount, roundCount) + "TB";
  283. }
  284. }
  285. }
  286. }