using NPOI.XWPF.UserModel; using OASystem.API.OAMethodLib.File; using OASystem.API.OAMethodLib.TencentCloudAPI; using OASystem.API.OAMethodLib.YouDaoAPI; using OASystem.Domain.Dtos.CRM; using OASystem.Domain.Dtos.Tencent; using OASystem.Domain.ViewModels.OCR; using OASystem.Domain.ViewModels.TencentOCR; using OASystem.Infrastructure.Repositories.CRM; using TencentCloud.Ocr.V20181119.Models; namespace OASystem.API.Controllers { /// /// TencentOCR 识别 /// [Route("api/[controller]/[action]")] //[ApiController] public class TencentOCRController : ControllerBase { private readonly VisaDeleClientRepository _visaDeleClientRepository; public TencentOCRController(VisaDeleClientRepository visaDeleClientRepository) { this._visaDeleClientRepository = visaDeleClientRepository; } #region web /// /// 通用印刷体 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetGeneralBasic(GeneralBasicOCRDto dto) { var validator = new GeneralBasicOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) { errors.AppendLine(valid.ErrorMessage); } return Ok(JsonView(false, errors.ToString())); } var gbData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto); if (gbData.Code != 0) { return Ok(JsonView(gbData.Msg)); } if (gbData.Data == null) { return Ok(JsonView(gbData.Msg)); } #region word下载 GeneralBasicOCRResponse res = gbData.Data as GeneralBasicOCRResponse; string fileName = "通用印刷体[CH]"; string content = $""; foreach (var view in res.TextDetections) { content += $"Content:{view.DetectedText} \n"; } if (dto.language == 1) { } else if (dto.language == 2) { fileName = "通用印刷体[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); #endregion return Ok(JsonView(true, "操作成功", new { dataSource = res, url = url })); } /// /// 身份证识别(国徽面/反面) /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetIDCard(IDCardOCRDto dto) { var validator = new IDCardOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var idCardData = await TencentOCRTools.GetOCR((int)TencentOCREnum.IDCard, dto); if (idCardData.Code != 0) { return Ok(JsonView(idCardData.Msg)); } if (idCardData.Data == null) { return Ok(JsonView(idCardData.Msg)); } IDCardOCRAndDownUrlView iDCardOCRView = new IDCardOCRAndDownUrlView() { }; if (dto.Language == 1) { bool cerdStatus = await _visaDeleClientRepository.SetCrmUpdPassIdCardOCR( new SetCrmUpdPassIdCardOCRDto { UserId = dto.UserId, ClientName = idCardData.Data.Name, Sex = idCardData.Data.Sex == "男" ? 0 : 1, CerdNo = idCardData.Data.IdNum, CerdAddress = idCardData.Data.Address, }); iDCardOCRView.Status = cerdStatus; iDCardOCRView.Name = idCardData.Data.Name; iDCardOCRView.Sex = idCardData.Data.Sex; iDCardOCRView.Nation = idCardData.Data.Nation; iDCardOCRView.Birth = idCardData.Data.Birth; iDCardOCRView.Address = idCardData.Data.Address; iDCardOCRView.IdNum = idCardData.Data.IdNum; iDCardOCRView.Authority = idCardData.Data.Authority; iDCardOCRView.ValidDate = idCardData.Data.ValidDate; } else if (dto.Language == 2) { iDCardOCRView.Name = !string.IsNullOrEmpty(idCardData.Data.Name) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Name) : ""; iDCardOCRView.Sex = !string.IsNullOrEmpty(idCardData.Data.Sex) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Sex) : ""; iDCardOCRView.Nation = !string.IsNullOrEmpty(idCardData.Data.Nation) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Nation) : ""; iDCardOCRView.Birth = !string.IsNullOrEmpty(idCardData.Data.Birth) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Birth) : ""; iDCardOCRView.Address = !string.IsNullOrEmpty(idCardData.Data.Address) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Address) : ""; iDCardOCRView.IdNum = idCardData.Data.IdNum; iDCardOCRView.Authority = !string.IsNullOrEmpty(idCardData.Data.Authority) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.Authority) : ""; iDCardOCRView.ValidDate = !string.IsNullOrEmpty(idCardData.Data.ValidDate) ? await YouDaoApiTools.GetOCR_ReTrans(idCardData.Data.ValidDate) : ""; } #region word生成 返回地址 Dictionary dic = new Dictionary(); dic.Add("Name", iDCardOCRView.Name); dic.Add("Sex", iDCardOCRView.Sex); dic.Add("Nation", iDCardOCRView.Nation); dic.Add("Birth", iDCardOCRView.Birth); dic.Add("Address", iDCardOCRView.Address); dic.Add("IdNum", iDCardOCRView.IdNum); dic.Add("Authority", iDCardOCRView.Authority); dic.Add("ValidDate", iDCardOCRView.ValidDate); string serverPath = ""; if (dto.Language == 1) { string fileNameCh = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[CH]" + DateTime.Now.ToString("yyyyMMddHHmmss")); serverPath = AsposeHelper.ExpertWordToModel("ocr_身份证(人像面).doc", "TencentOCR", fileNameCh, dic, null); } else if (dto.Language == 2) { string fileNameEn = string.Format("{0}.doc", dic["Name"].ToString() + "身份证(人像面)[EN]" + DateTime.Now.ToString("yyyyMMddHHmmss")); serverPath = AsposeHelper.ExpertWordToModel("(EN)ocr_身份证(人像面).doc", "TencentOCR", fileNameEn, dic, null); } iDCardOCRView.DownUrl = serverPath; #endregion return Ok(JsonView(iDCardOCRView)); } /// /// 户口簿识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetResidenceBookletOCR(ResidenceBookletOCRDto dto) { var validator = new ResidenceBookletOCRDtoFoalidator(); var validatorRes = validator.Validate(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var residenceBookData = await TencentOCRTools.GetOCR((int)TencentOCREnum.ResidenceBooklet, dto); if (residenceBookData.Code != 0) { return Ok(JsonView(residenceBookData.Msg)); } if (residenceBookData.Data == null) { return Ok(JsonView(residenceBookData.Msg)); } #region Feil Down ResidenceBookletOCRResponse res = residenceBookData.Data as ResidenceBookletOCRResponse; // 创建文档对象 XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); XWPFRun runText = p.CreateRun(); string fileName = "户口簿识别[CH]"; string content = $"{fileName}\n"; if (res != null) { content += $"户号:{res.HouseholdNumber ?? "Unknown"}\n"; content += $"姓名:{res.Name ?? "Unknown"}\n"; content += $"曾用名:{res.FormerName ?? "Unknown"}\n"; content += $"性别:{res.Sex ?? "Unknown"}\n"; content += $"出生地:{res.BirthPlace ?? "Unknown"}\n"; content += $"民族:{res.Nation ?? "Unknown"}\n"; content += $"籍贯:{res.NativePlace ?? "Unknown"}\n"; content += $"出生日期:{res.BirthDate ?? "Unknown"}\n"; content += $"公民身份证件编号:{res.IdCardNumber ?? "Unknown"}\n"; content += $"文化程度:{res.EducationDegree ?? "Unknown"}\n"; content += $"服务处所:{res.ServicePlace ?? "Unknown"}\n"; content += $"户别:{res.Household ?? "Unknown"}\n"; content += $"住址:{res.Address ?? "Unknown"}\n"; content += $"承办人签章文字:{res.Signature ?? "Unknown"}\n"; content += $"签发日期:{res.IssueDate ?? "Unknown"}\n"; content += $"户主页编号:{res.HomePageNumber ?? "Unknown"}\n"; content += $"户主姓名:{res.HouseholderName ?? "Unknown"}\n"; content += $"户主或与户主关系:{res.Relationship ?? "Unknown"}\n"; content += $"本市(县)其他住址:{res.OtherAddresses ?? "Unknown"}\n"; content += $"宗教信仰:{res.ReligiousBelief ?? "Unknown"}\n"; content += $"身高:{res.Height ?? "Unknown"}\n"; content += $"血型:{res.BloodType ?? "Unknown"}\n"; content += $"婚姻状况:{res.MaritalStatus ?? "Unknown"}\n"; content += $"兵役状况:{res.VeteranStatus ?? "Unknown"}\n"; content += $"职业:{res.Profession ?? "Unknown"}\n"; content += $"何时由何地迁来本市(县):{res.MoveToCityInformation ?? "Unknown"}\n"; content += $"何时由何地迁来本址:{res.MoveToSiteInformation ?? "Unknown"}\n"; content += $"登记日期:{res.RegistrationDate ?? "Unknown"}\n"; } if (dto.language == 1) { } else if (dto.language == 2) { fileName = "户口簿识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } setWrap(content, p, runText); runText.FontSize = 12; runText.SetFontFamily("宋体", FontCharRange.None); //设置宋体字体 //工作流写入,通过流的方式进行创建生成文件 MemoryStream stream = new MemoryStream(); doc.Write(stream); string FilePath = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR"; if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx"; string sDate = DateTime.Now.ToString("yyyy年MM月dd日-HH时mm分ss秒-ffff"); FilePath = FilePath + "/" + outputFile; FileStream Fs = new FileStream(FilePath, FileMode.OpenOrCreate); doc.Write(Fs); Fs.Close(); string url = $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}"; var data = new { dataSource = residenceBookData.Data, url = url }; #endregion return Ok(JsonView(data)); } /// /// npoi 创建 Word /// /// /// /// private string NPOICreateWord(string typeName, string content) { try { // 创建文档对象 XWPFDocument doc = new XWPFDocument(); XWPFParagraph p = doc.CreateParagraph(); XWPFRun runText = p.CreateRun(); string fileName = typeName; string content1 = $"{fileName}\n{content}"; setWrap(content1, p, runText); runText.FontSize = 12; runText.SetFontFamily("宋体", FontCharRange.None); //设置宋体字体 //工作流写入,通过流的方式进行创建生成文件 MemoryStream stream = new MemoryStream(); doc.Write(stream); string FilePath = $"{AppSettingsHelper.Get("WordBasePath")}/TencentOCR"; if (!Directory.Exists(FilePath)) { Directory.CreateDirectory(FilePath); } string outputFile = $"{fileName}{DateTime.Now.ToString("yyyyMMddHHmmss")}.docx"; string sDate = DateTime.Now.ToString("yyyy年MM月dd日-HH时mm分ss秒-ffff"); FilePath = FilePath + "/" + outputFile; FileStream Fs = new FileStream(FilePath, FileMode.OpenOrCreate); doc.Write(Fs); Fs.Close(); return $"{AppSettingsHelper.Get("WordBaseUrl")}Office/Word/TencentOCR/{outputFile}"; } catch (Exception ex) { return ex.Message; } } /// /// npoi 换行 /// /// /// /// private void setWrap(Object value, XWPFParagraph paragraph, XWPFRun run) { if (((String)value).IndexOf("\n") > 0) { //设置换行 String[] text = value.ToString().Split("\n"); for (int f = 0; f < text.Length; f++) { XWPFRun run1 = paragraph.CreateRun(); if (f == 0) { //此处不缩进因为word模板已经缩进了。 run1.SetText(text[f].Trim()); } else { run1.AddCarriageReturn();//硬回车 //注意:wps换行首行缩进是三个空格符,office要的话可以用 run.addTab();缩进或者四个空格符 run1.SetText(text[f].Trim()); } } } else { run.SetText((String)value); } } /// /// 营业执照识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetBizLicenseOCR(BizLicenseOCRDto dto) { var validator = new BizLicenseOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var residenceBookData = await TencentOCRTools.GetOCR((int)TencentOCREnum.BizLicense, dto); if (residenceBookData.Code != 0) { return Ok(JsonView(residenceBookData.Msg)); } if (residenceBookData.Data == null) { return Ok(JsonView(residenceBookData.Msg)); } BizLicenseOCRResponse dataInfo = residenceBookData.Data as BizLicenseOCRResponse; string fileName = "营业执照识别[CH]"; string content = ""; if (dataInfo != null) { content += $"统一社会信用代码(三合一之前为注册号):{dataInfo.RegNum}\n"; content += $"公司名称:{dataInfo.Name}\n"; content += $"注册资本:{dataInfo.Capital}\n"; content += $"法定代表人:{dataInfo.Person}\n"; content += $"地址:{dataInfo.Address} \n"; content += $"经营范围:{dataInfo.Business}\n"; content += $"主体类型:{dataInfo.Type} \n"; content += $"营业期限:{dataInfo.Period}\n"; content += $"组成形式:{dataInfo.ComposingForm}\n"; content += $"成立日期:{dataInfo.SetDate}\n"; } if (dto.language == 2) { fileName = "营业执照识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); var data = new { dataSource = residenceBookData.Data, url = url }; return Ok(JsonView(data)); } /// /// 组织结构识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetOrgCodeCertOCR(OrgCodeCertOCRDto dto) { var validator = new OrgCodeCertOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var occData = await TencentOCRTools.GetOCR((int)TencentOCREnum.OrgCodeCert, dto); if (occData.Code != 0) { return Ok(JsonView(occData.Msg)); } if (occData.Data == null) { return Ok(JsonView(occData.Msg)); } string fileName = "组织结构识别[CH]"; string content = ""; OrgCodeCertOCRResponse occInfo = occData.Data as OrgCodeCertOCRResponse; if (occInfo != null) { content += $"代码:{occInfo.OrgCode}\n"; content += $"机构名称:{occInfo.Name}\n"; content += $"地址:{occInfo.Address}\n"; content += $"有效期:{occInfo.ValidDate}\n"; } if (dto.language == 2) { fileName = "组织结构识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); var data = new { dataSource = occInfo, url = url }; return Ok(JsonView(data)); } /// /// 行驶证识别 /// 文档下载 /// CardSide=0 主页正面(有红色印章的一面),CardSide=1 行驶证副页正面(有号码号牌的一面),CardSide=2 行驶证主页正面和副页正面。 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetVehicleLicense(VehicleLicenseOCRDto dto) { var validator = new VehicleLicenseOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var vehicleLicenseData = TencentOCRTools.GetOCR((int)TencentOCREnum.VehicleLicense, dto); if (vehicleLicenseData.Result.Code != 0) { return Ok(JsonView(vehicleLicenseData.Result.Msg)); } if (vehicleLicenseData.Result.Data == null) { return Ok(JsonView(vehicleLicenseData.Result.Msg)); } #region word下载 VehicleLicenseOCRResponse vlData = vehicleLicenseData.Result.Data as VehicleLicenseOCRResponse; string url = ""; string fileName = ""; string content = ""; dynamic data = null; //Aspose.Words.License lic = new Aspose.Words.License(); //lic.SetLicense("Aspose.Wrods.lic");//破解版不用设置license if (dto.CardSide == 0) // 正面 { fileName = "行驶证识别-主页[CH]"; TextVehicleFront frontInfo = vlData.FrontInfo; if (frontInfo != null) { data = frontInfo; content += $"号牌号码:{frontInfo.PlateNo ?? "Unknown"}\n"; content += $"车辆类型:{frontInfo.VehicleType ?? "Unknown"}\n"; content += $"所有人:{frontInfo.Owner ?? "Unknown"}\n"; content += $"住址:{frontInfo.Address ?? "Unknown"}\n"; content += $"使用性质:{frontInfo.UseCharacter ?? "Unknown"}\n"; content += $"品牌型号:{frontInfo.Model ?? "Unknown"}\n"; content += $"车辆识别代号:{frontInfo.Vin ?? "Unknown"}\n"; content += $"发动机号码:{frontInfo.EngineNo ?? "Unknown"}\n"; content += $"注册日期:{frontInfo.RegisterDate ?? "Unknown"}\n"; content += $"发证日期:{frontInfo.IssueDate ?? "Unknown"}\n"; content += $"印章:{frontInfo.Seal ?? "Unknown"}\n"; } if (dto.language == 2) { fileName = "行驶证识别-主页[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } url = NPOICreateWord(fileName, content); } else if (dto.CardSide == 1) //反面 { TextVehicleBack backInfo = vlData.BackInfo; fileName = "行驶证识别-副页[CH]"; if (backInfo != null) { data = backInfo; content += $"号牌号码:{backInfo.PlateNo ?? "Unknown"}\n"; content += $"档案编号:{backInfo.FileNo ?? "Unknown"}\n"; content += $"核定人数:{backInfo.AllowNum ?? "Unknown"}\n"; content += $"总质量:{backInfo.TotalMass ?? "Unknown"}\n"; content += $"整备质量:{backInfo.CurbWeight ?? "Unknown"}\n"; content += $"核定载质量:{backInfo.LoadQuality ?? "Unknown"}\n"; content += $"外廓尺寸:{backInfo.ExternalSize ?? "Unknown"}\n"; content += $"备注:{backInfo.Marks ?? "Unknown"}\n"; content += $"检验记录:{backInfo.Record ?? "Unknown"}\n"; content += $"准牵引总质量:{backInfo.TotalQuasiMass ?? "Unknown"}\n"; } if (dto.language == 2) { fileName = "行驶证识别-副页[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } url = NPOICreateWord(fileName, content); } #endregion return Ok(JsonView(true, "操作成功", new { dataSource = data, url = url })); } /// /// 房产证识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetPropOwnerCert(PropOwnerCertOCRDto dto) { var validator = new PropOwnerCertOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var pcData = await TencentOCRTools.GetOCR((int)TencentOCREnum.PropOwnerCert, dto); if (pcData.Code != 0) { return Ok(JsonView(pcData.Msg)); } if (pcData.Data == null) { return Ok(JsonView(pcData.Msg)); } PropOwnerCertOCRResponse pocInfo = pcData.Data as PropOwnerCertOCRResponse; string fileName = "房产证识别[CH]"; string content = ""; if (pocInfo != null) { content += $"房地产权利人:{pocInfo.Owner}\n"; content += $"共有情况:{pocInfo.Possession}\n"; content += $"登记时间:{pocInfo.RegisterTime}\n"; content += $"规划用途:{pocInfo.Purpose}\n"; content += $"房屋性质:{pocInfo.Nature}\n"; content += $"房地坐落:{pocInfo.Location}\n"; } if (dto.language == 2) { fileName = "房产证识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); return Ok(JsonView(new { dataSource = pocInfo, url = url })); } /// /// 结婚证识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetMarriageLicense(GeneralBasicOCRDto dto) { var validator = new GeneralBasicOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var mlData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto); if (mlData.Code != 0) { return Ok(JsonView(mlData.Msg)); } if (mlData.Data == null) { return Ok(JsonView(mlData.Msg)); } string fileName = "结婚证识别[CH]"; string content = ""; GeneralBasicOCRResponse data = mlData.Data as GeneralBasicOCRResponse; List infos = data.TextDetections.ToList(); foreach (var item in infos) { content += $"Content:{item.DetectedText}\n"; } if (dto.language == 2) { fileName = "结婚证识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); return Ok(JsonView(new { dataSource = infos, url = url })); } /// /// 统一信用代码识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetUniformCreditCode(GeneralBasicOCRDto dto) { var validator = new GeneralBasicOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var gbData = await TencentOCRTools.GetOCR((int)TencentOCREnum.GeneralBasic, dto); if (gbData.Code != 0) { return Ok(JsonView(gbData.Msg)); } if (gbData.Data == null) { return Ok(JsonView(gbData.Msg)); } string fileName = "统一信用代码识别[CH]"; string content = ""; GeneralBasicOCRResponse data = gbData.Data as GeneralBasicOCRResponse; List infos = data.TextDetections.ToList(); foreach (var item in infos) { content += $"Content:{item.DetectedText}\n"; } if (dto.language == 2) { fileName = "统一信用代码识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); return Ok(JsonView(new { dataSource = infos, url = url })); } /// /// 护照识别(中国大陆地区护照) /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetPassport(PassportOCRDto dto) { var validator = new PassportOCRDtoFoalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var pData = await TencentOCRTools.GetOCR((int)TencentOCREnum.Passport, dto); if (pData.Code != 0) { return Ok(JsonView(pData.Msg)); } string url = ""; #region 文档下载 string fileName = "护照识别(中国大陆地区护照)[CH]"; string content = $""; PassportOCRResponse pInfo = pData.Data as PassportOCRResponse; if (pInfo != null) { content += $"国籍:{pInfo.Nationality ?? "Unknown"}\n"; content += $"国家码:{pInfo.Country ?? "Unknown"}\n"; content += $"护照号:{pInfo.PassportNo ?? "Unknown"}\n"; content += $"姓名:{pInfo.Name ?? "Unknown"}\n"; content += $"姓:{pInfo.FamilyName ?? "Unknown"}\n"; content += $"名:{pInfo.FirstName ?? "Unknown"}\n"; content += $"性别:{pInfo.Sex ?? "Unknown"}\n"; content += $"出生日期:{pInfo.BirthDate ?? "Unknown"}\n"; content += $"签发日期:{pInfo.IssueDate ?? "Unknown"}\n"; content += $"签发地点:{pInfo.IssuePlace ?? "Unknown"}\n"; content += $"有效期:{pInfo.ExpiryDate ?? "Unknown"}\n"; content += $"持证人签名:{pInfo.Signature ?? "Unknown"}\n"; content += $"最下方第一行 MRZ Code 序列:{pInfo.CodeSet ?? "Unknown"}\n"; content += $"最下方第二行 MRZ Code 序列:{pInfo.CodeCrc ?? "Unknown"}\n"; } if (dto.language == 2) { fileName = "护照识别(中国大陆地区护照)[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } url = NPOICreateWord(fileName, content); #endregion return Ok(JsonView(true, "操作成功", new { dataSource = pInfo, url = url })); } /// /// 护照识别(港澳台地区及境外护照) /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetMLIDPassport(MLIDPassportOCRDto dto) { var validator = new MLIDPassportOCRDtoFoavalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var mlidpData = await TencentOCRTools.GetOCR((int)TencentOCREnum.MLIDPassport, dto); if (mlidpData.Code != 0) { return Ok(JsonView(mlidpData.Msg)); } if (mlidpData.Data == null) { return Ok(JsonView(mlidpData.Msg)); } string url = ""; #region 文档下载 string fileName = "护照识别(港澳台地区及境外护照)[CH]"; string content = $""; MLIDPassportOCRResponse mlidInfo = mlidpData.Data as MLIDPassportOCRResponse; if (mlidInfo != null) { content += $"护照ID:{mlidInfo.ID ?? "Unknown"}\n"; content += $"姓名:{mlidInfo.Name ?? "Unknown"}\n"; content += $"出生日期:{mlidInfo.DateOfBirth ?? "Unknown"}\n"; content += $"性别(F女,M男):{mlidInfo.Sex ?? "Unknown"}\n"; content += $"有效期:{mlidInfo.DateOfExpiration ?? "Unknown"}\n"; content += $"发行国:{mlidInfo.IssuingCountry ?? "Unknown"}\n"; content += $"国家地区代码:{mlidInfo.Nationality ?? "Unknown"}\n"; } if (dto.language == 2) { fileName = "护照识别(港澳台地区及境外护照)[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } url = NPOICreateWord(fileName, content); #endregion return Ok(JsonView(true, "操作成功", new { dataSource = mlidInfo, url = url })); } /// /// 名片识别 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetBusinessCard(BusinessCardOCRDto dto) { var validator = new BusinessCardOCRDtoFoavalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) errors.AppendLine(valid.ErrorMessage); return Ok(JsonView(false, errors.ToString())); } var mlidpData = await TencentOCRTools.GetOCR((int)TencentOCREnum.BusinessCard, dto); if (mlidpData.Code != 0) { return Ok(JsonView(mlidpData.Msg)); } if (mlidpData.Data == null) { return Ok(JsonView(mlidpData.Msg)); } string url = ""; #region 文档下载 string fileName = "名片识别[CH]"; string content = $""; BusinessCardOCRResponse bcData = mlidpData.Data as BusinessCardOCRResponse; List bcInfos = new List(); bcInfos = bcData.BusinessCardInfos.ToList(); foreach (var view in bcInfos) { content += $"TypeName:{view?.Name ?? "Unknown"}\nContent:{view?.Value ?? "Unknown"}\n"; } if (dto.language == 2) { fileName = "名片识别[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } url = NPOICreateWord(fileName, content); #endregion //return Ok(JsonView(true, "操作成功", new { dataSource = bcData, url = url })); return Ok(JsonView(true, "操作成功", new { businessCardInfos = bcData.BusinessCardInfos, url = url })); } /// /// 智能结构化识别V2 /// 文档下载 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task GetSmartStructuralOCRV2(BusinessCardOCRDto dto) { var validator = new BusinessCardOCRDtoFoavalidator(); var validatorRes = await validator.ValidateAsync(dto); if (!validatorRes.IsValid) { var errors = new StringBuilder(); foreach (var valid in validatorRes.Errors) { errors.AppendLine(valid.ErrorMessage); } return Ok(JsonView(false, errors.ToString())); } var ssData = await TencentOCRTools.GetOCR((int)TencentOCREnum.SmartStructuralOCRV2, dto); if (ssData.Code != 0) { return Ok(JsonView(false, ssData.Msg)); } if (ssData.Data == null) { return Ok(JsonView(false, ssData.Msg)); } string fileName = "智能结构化识别V2[CH]"; List? datas = ssData.Data as List; string content = $""; foreach (var view in datas) { content += $"TypeName:{view.name}\nContent:{view.value}\n"; } if (dto.language == 2) { fileName = "智能结构化识别V2[EN]"; content = await YouDaoApiTools.GetOCR_ReTrans(content); } string url = NPOICreateWord(fileName, content); var data = new { dataSource = ssData.Data, url = url }; return Ok(JsonView(true, ssData.Msg, data)); } #endregion #region android ios /// /// 名片识别 /// 获取名片 移动端 返回数据 /// /// /// [HttpPost] [ProducesResponseType(typeof(JsonView), StatusCodes.Status200OK)] public async Task PostBusinessCardApp(BusinessCardOCRDto dto) { double strSize = 1024 * 1024 * 7; if (dto.picBase64.Length > strSize) { return Ok(JsonView("图片不能大于7M!")); } Logs(dto.picBase64); Logs("名片识别开始时间:" + DateTime.Now); try { var mlidpData = TencentOCRTools.PostOCRApp((int)TencentOCREnum.BusinessCard, dto); if (mlidpData.Code != 0) { return Ok(JsonView(mlidpData.Msg)); } if (mlidpData.Data == null) { return Ok(JsonView(mlidpData.Msg)); } Logs("名片识别结束时间:" + DateTime.Now); return Ok(JsonView(mlidpData.Data)); } catch (Exception ex) { return Ok(JsonView(false, ex.Message)); } } #endregion } }