using OASystem.Domain.Dtos.Tencent; using OASystem.Domain.ViewModels.OCR; using TencentCloud.Common; using TencentCloud.Ocr.V20181119; using TencentCloud.Ocr.V20181119.Models; namespace OASystem.API.OAMethodLib.TencentCloudAPI { /// /// 腾讯OCR Tools /// public static class TencentOCRTools { /// /// 初始化 腾讯云账户密钥对 SecretId,SecretKey /// static Credential cred = new Credential { SecretId = "AKIDoQUHLBELagatzoScfdMpMddMnz8apqIc", SecretKey = "hKiGbhkX5NWWXu3zDWwAATVK3e5sWuBB" }; /// /// ocr识别 Web /// /// 转换类型 /// 请求数据源 /// public static async Task GetOCR(int ocrEnum, object data) { Result result = new Result { Code = -1, Msg = "未知错误", Data = null }; OcrClient client = new OcrClient(cred, "ap-guangzhou"); try { switch (ocrEnum) { case 0: //身份证识别 IDCardOCRDto iDCardDto = (IDCardOCRDto)data; IDCardOCRRequest req = new IDCardOCRRequest(); if (iDCardDto.CardSide == 0) req.CardSide = "FRONT"; else if (iDCardDto.CardSide == 1) req.CardSide = "BACK"; req.ImageBase64 = iDCardDto.picBase64; IDCardOCRResponse resp = client.IDCardOCRSync(req); result.Data = resp; break; case 1: //户口本识别 ResidenceBookletOCRDto rbOCR = (ResidenceBookletOCRDto)data; ResidenceBookletOCRRequest rbReq = new ResidenceBookletOCRRequest(); rbReq.ImageBase64 = rbOCR.picBase64; ResidenceBookletOCRResponse rbRep = client.ResidenceBookletOCRSync(rbReq); result.Data = rbRep; break; case 2: //营业执照识别 OrgCodeCert BizLicenseOCRDto blOCR = (BizLicenseOCRDto)data; BizLicenseOCRRequest blReq = new BizLicenseOCRRequest(); blReq.ImageBase64 = blOCR.picBase64; BizLicenseOCRResponse blRep = client.BizLicenseOCRSync(blReq); result.Data = blRep; break; case 3: //组织机构代码证识别 OrgCodeCertOCRDto occOCR = (OrgCodeCertOCRDto)data; OrgCodeCertOCRRequest ocReq = new OrgCodeCertOCRRequest(); ocReq.ImageBase64 = occOCR.picBase64; OrgCodeCertOCRResponse occReq = client.OrgCodeCertOCRSync(ocReq); result.Data = occReq; break; case 4: //行驶证识别 VehicleLicenseOCRDto vlOCR = (VehicleLicenseOCRDto)data; VehicleLicenseOCRRequest vlReq = new VehicleLicenseOCRRequest(); if (vlOCR.CardSide == 0) vlReq.CardSide = "FRONT"; else if (vlOCR.CardSide == 1) vlReq.CardSide = "BACK"; else if (vlOCR.CardSide == 2) vlReq.CardSide = "DOUBLE"; vlReq.ImageBase64 = vlOCR.picBase64; VehicleLicenseOCRResponse vlRep = client.VehicleLicenseOCRSync(vlReq); result.Data = vlRep; break; case 5: //房产证识别 通用文字识别 PropOwnerCertOCRDto pocOCR = (PropOwnerCertOCRDto)data; PropOwnerCertOCRRequest pocReq = new PropOwnerCertOCRRequest(); pocReq.ImageBase64 = pocOCR.picBase64; PropOwnerCertOCRResponse pocRep = client.PropOwnerCertOCRSync(pocReq); result.Data = pocRep; break; case 6: // 通用文字识别 GeneralBasicOCRDto gbOCR = (GeneralBasicOCRDto)data; GeneralBasicOCRRequest gbReq = new GeneralBasicOCRRequest(); gbReq.ImageBase64 = gbOCR.picBase64; gbReq.IsPdf = true; GeneralBasicOCRResponse gbRep = client.GeneralBasicOCRSync(gbReq); result.Data = gbRep; break; case 7: // 护照识别(中国大陆地区护照) PassportOCRDto pOCR = (PassportOCRDto)data; PassportOCRRequest pReq = new PassportOCRRequest(); pReq.ImageBase64 = pOCR.picBase64; PassportOCRResponse pRep = client.PassportOCRSync(pReq); result.Data = pRep; break; case 8: // 护照识别(港澳台地区及境外护照) MLIDPassportOCRDto mlidpOCR = (MLIDPassportOCRDto)data; MLIDPassportOCRRequest mlidpReq = new MLIDPassportOCRRequest(); mlidpReq.ImageBase64 = mlidpOCR.picBase64; MLIDPassportOCRResponse mlidpRep = client.MLIDPassportOCRSync(mlidpReq); result.Data = mlidpRep; break; case 9: // 名片识别 BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data; BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest(); bcReq.ImageBase64 = bcOCR.picBase64; BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq); result.Data = bcRep; break; case 10: // 智能结构化识别V2 //SmartStructuralOCRV2 BusinessCardOCRDto ssOCR = (BusinessCardOCRDto)data; SmartStructuralOCRRequest ssReq = new SmartStructuralOCRRequest() { ImageBase64 = ssOCR.picBase64, IsPdf = true, ReturnFullText = true }; SmartStructuralOCRResponse ssRep = client.SmartStructuralOCRSync(ssReq); List structuralItems = ssRep.StructuralItems.ToList(); if (structuralItems.Count() < 1) { return new Result() { Code = -1, Msg = "智能结构化识别V2:未识别出可用信息" }; } List _views = structuralItems.Select(it => new ocrGeneralView() { name = it.Name, value = it.Value }).ToList(); result.Data = _views; break; } result.Code = 0; result.Msg = "成功"; return result; } catch (Exception ex) { result.Msg = "TencentOCRAPIError:" + ex.Message; return result; } } /// /// ocr识别 android ios /// /// 转换类型 /// 请求数据源 /// public static Result PostOCRApp(int ocrEnum, object data) { Result result = new Result { Code = -1, Msg = "未知错误", Data = null }; OcrClient client = new OcrClient(cred, "ap-guangzhou"); try { switch (ocrEnum) { case 9: // 名片识别 BusinessCardOCRDto bcOCR = (BusinessCardOCRDto)data; BusinessCardOCRRequest bcReq = new BusinessCardOCRRequest(); bcReq.ImageBase64 = bcOCR.picBase64; BusinessCardOCRResponse bcRep = client.BusinessCardOCRSync(bcReq); BusinessCardInfoView businessCardInfo = new BusinessCardInfoView(); if (bcRep.BusinessCardInfos.Length > 0) { List BusinessCardInfos = new List(); BusinessCardInfos = bcRep.BusinessCardInfos.ToList(); var nameData = BusinessCardInfos.Where(x => x.Name == "姓名").FirstOrDefault(); if (nameData != null) businessCardInfo.Name = nameData.Value; var companyData = BusinessCardInfos.Where(x => x.Name.Equals("公司")).FirstOrDefault(); if (companyData != null) businessCardInfo.Company = companyData.Value; var addressData = BusinessCardInfos.Where(x => x.Name == "地址").FirstOrDefault(); if (addressData != null) businessCardInfo.Address = addressData.Value; var jobData = BusinessCardInfos.Where(x => x.Name == "职位").FirstOrDefault(); if (jobData != null) businessCardInfo.Job = jobData.Value; var emailData = BusinessCardInfos.Where(x => x.Name == "邮箱").FirstOrDefault(); if (emailData != null) businessCardInfo.Email = emailData.Value; var phoneData = BusinessCardInfos.Where(x => x.Name == "手机").ToList(); if (phoneData.Count > 0) { string phone = ""; foreach (var item in phoneData) { phone += item.Value + ","; } if (phone.Length > 0) { phone = phone.Substring(0, phone.Length - 1); } businessCardInfo.Phone = phone; } var telData = BusinessCardInfos.Where(x => x.Name == "电话").ToList(); if (telData.Count > 0) { string tel = ""; foreach (var item in telData) { tel += item.Value + ","; } if (tel.Length > 0) { tel = tel.Substring(0, tel.Length - 1); } businessCardInfo.Tel = tel; } } result.Data = businessCardInfo; break; } result.Code = 0; result.Msg = "成功"; return result; } catch (Exception ex) { result.Msg = "TencentOCRAPIError:" + ex.Message; return result; } } /// /// 图片转Base64 /// /// 图片的完整路径 /// public static string ImgToBase64(string ImageFileName) { try { Bitmap bmp = new Bitmap(ImageFileName); MemoryStream ms = new MemoryStream(); bmp.Save(ms, ImageFormat.Jpeg); byte[] arr = new byte[ms.Length]; ms.Position = 0; ms.Read(arr, 0, (int)ms.Length); ms.Close(); return Convert.ToBase64String(arr); } catch (Exception e) { return e.Message.ToString(); } } /// /// TencentOCR图片类型处理 /// PNG、JPG、JPEG、BMP /// /// /// public static bool ImageType(string type) { if (type.ToLower() == "png" || type.ToLower() == "jpg" || type.ToLower() == "jpeg" || type.ToLower() == "bmp") return true; return false; } private const double KBCount = 1024; private const double MBCount = KBCount * 1024; private const double GBCount = MBCount * 1024; private const double TBCount = GBCount * 1024; /// /// 得到适应的大小 /// /// /// string public static string GetAutoSizeString(double size, int roundCount) { if (KBCount > size) { return Math.Round(size, roundCount) + "B"; } else if (MBCount > size) { return Math.Round(size / KBCount, roundCount) + "KB"; } else if (GBCount > size) { return Math.Round(size / MBCount, roundCount) + "MB"; } else if (TBCount > size) { return Math.Round(size / GBCount, roundCount) + "GB"; } else { return Math.Round(size / TBCount, roundCount) + "TB"; } } } }