123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- 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
- {
- /// <summary>
- /// 腾讯OCR Tools
- /// </summary>
- public static class TencentOCRTools
- {
- /// <summary>
- /// 初始化 腾讯云账户密钥对 SecretId,SecretKey
- /// </summary>
- static Credential cred = new Credential
- {
- SecretId = "AKIDoQUHLBELagatzoScfdMpMddMnz8apqIc",
- SecretKey = "hKiGbhkX5NWWXu3zDWwAATVK3e5sWuBB"
- };
- /// <summary>
- /// ocr识别 Web
- /// </summary>
- /// <param name="ocrEnum">转换类型</param>
- /// <param name="data">请求数据源</param>
- /// <returns></returns>
- public static async Task<Result> 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<StructuralItem> structuralItems = ssRep.StructuralItems.ToList();
- if (structuralItems.Count() < 1)
- {
- return new Result() { Code = -1, Msg = "智能结构化识别V2:未识别出可用信息" };
- }
- List<ocrGeneralView> _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;
- }
- }
- /// <summary>
- /// ocr识别 android ios
- /// </summary>
- /// <param name="ocrEnum">转换类型</param>
- /// <param name="data">请求数据源</param>
- /// <returns></returns>
- 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<BusinessCardInfo> BusinessCardInfos = new List<BusinessCardInfo>();
- 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;
- }
- }
- /// <summary>
- /// 图片转Base64
- /// </summary>
- /// <param name="ImageFileName">图片的完整路径</param>
- /// <returns></returns>
- 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();
- }
- }
- /// <summary>
- /// TencentOCR图片类型处理
- /// PNG、JPG、JPEG、BMP
- /// </summary>
- /// <param name="type"></param>
- /// <returns></returns>
- 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;
- /// <summary>
- /// 得到适应的大小
- /// </summary>
- /// <param name="path"></param>
- /// <returns>string</returns>
- 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";
- }
- }
- }
- }
|