TencentOCRTools.cs 14 KB

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