From 6a0009a3ade1905e289ff4bdd106057ee13aa289 Mon Sep 17 00:00:00 2001 From: hejinbo Date: Mon, 27 Nov 2023 17:28:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-common/pom.xml | 6 +- .../impl/CaseApplicationServiceImpl.java | 183 +++++++++++++----- .../ruoyi/wisdomarbitrate/utils/OCRUtils.java | 95 +++++++++ .../com/ruoyi/wisdomarbitrate/utils/Tset.java | 46 ++++- 4 files changed, 272 insertions(+), 58 deletions(-) create mode 100644 ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 2a11d03..830ba5f 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -174,7 +174,11 @@ documents4j-transformer-msoffice-word 1.0.3 - + + org.apache.pdfbox + pdfbox + 2.0.27 + diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java index a649ba8..c7298dc 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java @@ -40,6 +40,7 @@ import com.ruoyi.wisdomarbitrate.domain.*; import com.ruoyi.wisdomarbitrate.domain.vo.SendRoomNoMessageVO; import com.ruoyi.wisdomarbitrate.mapper.*; import com.ruoyi.wisdomarbitrate.service.ICaseApplicationService; +import com.ruoyi.wisdomarbitrate.utils.OCRUtils; import com.ruoyi.wisdomarbitrate.utils.SignAward; import com.ruoyi.wisdomarbitrate.utils.UnZipFileUtils; import com.tencentyun.TLSSigAPIv2; @@ -50,6 +51,7 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; +import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.poi.xwpf.usermodel.Document; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; @@ -980,10 +982,10 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { if (insertRow != 0 && CollectionUtil.isNotEmpty(caseAffiliates)) { caseAffiliates.forEach(caseAffiliate -> caseAffiliate.setCaseAppliLogId(caseApplication.getId())); // 插入案件日志人员相关表 - if(CollectionUtil.isNotEmpty(caseAttachList)) { + if (CollectionUtil.isNotEmpty(caseAttachList)) { List filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); // 插入日志附件表 - if(CollectionUtil.isNotEmpty(filterList)) { + if (CollectionUtil.isNotEmpty(filterList)) { for (CaseAttach caseAttach : filterList) { // 查询附件表 CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId()); @@ -992,7 +994,8 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { } } - } caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates); + } + caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates); } }); @@ -1098,21 +1101,21 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { } List caseAttachList = caseApplication.getCaseAttachList(); // 立案申请状态直接修改主表信息 - if (caseAttachList != null && caseAttachList.size() > 0 - && caseApplication.getCaseStatus()!=null && caseApplication.getCaseStatus().equals(CaseApplicationConstants.CASE_APPLICATION)) { - List filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); - if(CollectionUtil.isNotEmpty(filterList)){ - // 先删除2的附件在新增 + if (caseAttachList != null && caseAttachList.size() > 0 + && caseApplication.getCaseStatus() != null && caseApplication.getCaseStatus().equals(CaseApplicationConstants.CASE_APPLICATION)) { + List filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); + if (CollectionUtil.isNotEmpty(filterList)) { + // 先删除2的附件在新增 // caseAttachMapper.deleteByCasedIdAndType(caseApplication.getId(),2,0); // for (CaseAttach caseAttach : filterList) { // caseAttach.setCaseAppliId(caseApplication.getId()); // caseAttachMapper.save(caseAttach); // } - for (CaseAttach caseAttach : caseAttachList) { - caseAttach.setCaseAppliId(caseApplication.getId()); - caseAttachMapper.updateCaseAttach(caseAttach); - } + for (CaseAttach caseAttach : caseAttachList) { + caseAttach.setCaseAppliId(caseApplication.getId()); + caseAttachMapper.updateCaseAttach(caseAttach); + } } @@ -1133,23 +1136,23 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { try { caseApplication.setCaseAppliId(caseApplication.getId()); int insertRow = caseApplicationLogMapper.insert(caseApplication); - if (insertRow != 0 ) { - if( CollectionUtil.isNotEmpty(caseAffiliates)) { + if (insertRow != 0) { + if (CollectionUtil.isNotEmpty(caseAffiliates)) { caseAffiliates.forEach(caseAffiliate -> caseAffiliate.setCaseAppliLogId(caseApplication.getId())); // 插入案件日志人员相关表 caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates); } - if(CollectionUtil.isNotEmpty(caseAttachList)) { + if (CollectionUtil.isNotEmpty(caseAttachList)) { List filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); // 插入日志附件表 - if(CollectionUtil.isNotEmpty(filterList)) { - for (CaseAttach caseAttach : filterList) { - // 查询附件表 - CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId()); - attach.setCaseAppliLogId(caseApplication.getId()); - caseAttachLogMapper.save(attach); - } - } + if (CollectionUtil.isNotEmpty(filterList)) { + for (CaseAttach caseAttach : filterList) { + // 查询附件表 + CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId()); + attach.setCaseAppliLogId(caseApplication.getId()); + caseAttachLogMapper.save(attach); + } + } } } @@ -2806,17 +2809,17 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { if (CollectionUtil.isEmpty(reservedConferences)) { return reservedConferences; } - Map userIdMap = null; + Map userIdMap = null; List userIds = reservedConferences.stream().map(ReservedConference::getUserId).collect(Collectors.toList()); - if(CollectionUtil.isNotEmpty(userIds)){ + if (CollectionUtil.isNotEmpty(userIds)) { // 根据userids查询用户名 List userList = sysUserMapper.selectUserListByIds(userIds); - if(CollectionUtil.isNotEmpty(userList)){ - userIdMap = userList.stream().collect(Collectors.toMap(SysUser::getUserId,SysUser::getUserName)); + if (CollectionUtil.isNotEmpty(userList)) { + userIdMap = userList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName)); } } for (ReservedConference reservedConference : reservedConferences) { - if(null!=reservedConference.getUserId() && null!=userIdMap){ + if (null != reservedConference.getUserId() && null != userIdMap) { reservedConference.setUserName(userIdMap.get(reservedConference.getUserId())); } Date startTime = reservedConference.getScheduleStartTime(); @@ -2852,41 +2855,106 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { if (file.isEmpty()) { return AjaxResult.error("请选择要上传的文件"); } - //String targetPath = "/home/ruoyi/uploadPath/upload/unzipFile"; UUID uuid = UUID.randomUUID(); - String targetPath = "D:\\home\\unzip\\"+uuid+"\\"; + String targetPath = "\\home\\ruoyi\\uploadPath\\upload\\unzipFile\\"; + //String targetPath = "D:\\home\\unzip\\" + uuid + "\\"; File zipFile = null; InputStream ins = null; try { ins = file.getInputStream(); - String savePath = "D:\\develop\\java\\"; - String saveName = uuid+file.getOriginalFilename(); - zipFile = new File(savePath+saveName); + //上传的压缩包保存的路径 + //String savePath = "D:\\develop\\java\\"; + String savePath = "\\home\\ruoyi\\uploadPath\\upload\\zipFile\\"; + String saveName = uuid + file.getOriginalFilename(); + zipFile = new File(savePath + saveName); inputChangeToFile(ins, zipFile); } catch (IOException e) { e.printStackTrace(); } //解压缩上传的压缩包 - boolean unzipSuccess = UnZipFileUtils.unZipFile(zipFile, targetPath); - if (unzipSuccess ) { - //去掉后缀名,拿到解压后的文件夹路径 - String fileName = file.getOriginalFilename(); - String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf(".")); + boolean unzipSuccess = UnZipFileUtils.unZipFile(zipFile, targetPath); + if (unzipSuccess) { File directory = new File(targetPath); List andConvertPDF = findAndConvertPDF(directory); - if (andConvertPDF != null && andConvertPDF.size() > 0){ - // 返回找到的PDF文件路径 - return AjaxResult.success(andConvertPDF); - }else { + if (andConvertPDF != null && andConvertPDF.size() > 0) { + //遍历路径 + for (String pdfUrl : andConvertPDF) { + //获取文件的页数 + int fileNumPage = getFileNumPage(pdfUrl); + StringBuilder stringBuilder = new StringBuilder(); // 创建一个StringBuilder对象 + for (int i = 0; i < fileNumPage; i++) { + //对接腾讯云接口.识别里面的数据 + //暂时写死 + String pdfFilePath = "http://121.40.189.20:9000/API/uploadPath/upload/2023/11/14/ca2ca4697e5449ff9b9d23f95b221f58.pdf"; + String text = OCRUtils.pdfIdentifyText(pdfUrl, i + 1); + if (stringBuilder.length() > 0) { + stringBuilder.append(","); // 在已有内容的情况下添加逗号分隔符 + } + stringBuilder.append(text); // 拼接当前的字符串 + } + // 将字符串按逗号分隔符切割 + String[] fields = stringBuilder.toString().split(","); + JSONObject jsonObject = new JSONObject(); // 创建一个空的JSON对象 + for (String field : fields) { + // 对于每个字段,再按冒号分隔符拆分出键和值 + String[] keyValue = field.split(":"); + if (keyValue.length == 2) { // 对于合法的键值对,将其添加到JSON对象中 + jsonObject.put(keyValue[0], keyValue[1]); + } + } + //调用新增案件的接口 + CaseApplication caseApplication = new CaseApplication(); + + caseApplication.setApplicationOrganId(jsonObject.getString("统一社会信用代码")); + caseApplication.setCompLegalPerson(jsonObject.getString("法定代表人")); + caseApplication.setResidenAffiliAppli(jsonObject.getString("住所")); + caseApplication.setContactAddressAgent(jsonObject.getString("联系地址")); + caseApplication.setNameAgent(jsonObject.getString("委托代理人")); + caseApplication.setContactTelphoneAgent(jsonObject.getString("联系电话")); + caseApplication.setDebtorName(jsonObject.getString("被申请人")); + caseApplication.setDebtorIdentityNum(jsonObject.getString("居民身份证号码")); + caseApplication.setArbitratClaims(jsonObject.getString("仲裁请求")); + caseApplication.setCaseSubjectAmount(new BigDecimal(1)); + List caseAffiliates = new ArrayList<>(); + CaseAffiliate caseAffiliate = new CaseAffiliate(); + caseAffiliate.setIdentityType(1); + caseAffiliate.setName(jsonObject.getString("申请人")); + caseAffiliate.setIdentityNum(""); + caseAffiliate.setContactTelphone(jsonObject.getString("申请人联系电话")); + caseAffiliates.add(caseAffiliate); + CaseAffiliate caseAffiliate1 = new CaseAffiliate(); + caseAffiliate1.setIdentityType(2); + caseAffiliate1.setName(jsonObject.getString("被申请人")); + caseAffiliate1.setIdentityNum(""); + caseAffiliate1.setContactTelphone(jsonObject.getString("被申请人联系电话")); + caseAffiliates.add(caseAffiliate1); + caseApplication.setCaseAffiliates(caseAffiliates); + this.insertcaseApplication(caseApplication); + if (null != caseApplication.getId()) { + // 绑定案件与申请书 + File file1 = new File(pdfUrl); + CaseAttach caseAttach = new CaseAttach(); + caseAttach.setCaseAppliId(caseApplication.getId()); + String saveName = "/profile/upload/" + file1.getName(); + String savePath = "/home/ruoyi/uploadPath/upload"; + caseAttach.setAnnexPath(savePath); + caseAttach.setAnnexName(saveName); + caseAttach.setAnnexType(1); + caseAttachMapper.save(caseAttach); + } + } + return AjaxResult.success("导入成功"); + } else { // 没有找到符合条件的文件 return AjaxResult.error("未找到符合条件的文件"); } - }else { + } else { // 解压失败 return AjaxResult.error("解压失败"); } } + public static List findAndConvertPDF(File directory) { List pdfPaths = new ArrayList<>(); if (directory.isFile()) { @@ -2903,16 +2971,16 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { path = pdfPath; } } - if (path!= null) { + if (path != null) { pdfPaths.add(path); } } } else if (directory.isDirectory()) { searchAndConvertPDF(directory, pdfPaths); - }else { + } else { return null; } - return pdfPaths; + return pdfPaths; } public static boolean isPDF(File file) { @@ -2929,6 +2997,7 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { return ""; } } + public static void searchAndConvertPDF(File directory, List pdfPaths) { File[] files = directory.listFiles(); if (files != null) { @@ -2954,34 +3023,44 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { } } } + private static String convertToPDF(File file) { String wordFilePath = file.getAbsolutePath(); - String pdfSaveDirectory ="D:\\home\\unzip\\wordToPDF\\"; + // String pdfSaveDirectory = "D:\\home\\unzip\\wordToPDF\\"; + String pdfSaveDirectory = "\\home\\ruoyi\\uploadPath\\upload\\wordToPDF\\"; File directory = new File(pdfSaveDirectory); if (!directory.exists()) { directory.mkdirs(); } String name = file.getName(); String nameWithoutExtension = name.substring(0, name.lastIndexOf(".")); - String pdfFilePath =pdfSaveDirectory+nameWithoutExtension+".pdf"; + String pdfFilePath = pdfSaveDirectory + nameWithoutExtension + ".pdf"; File inputWord = new File(wordFilePath); File outputFile = new File(pdfFilePath); - try { + try { InputStream docxInputStream = new FileInputStream(inputWord); OutputStream outputStream = new FileOutputStream(outputFile); IConverter converter = LocalConverter.builder().build(); converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); docxInputStream.close(); outputStream.close(); - System.out.println("success"); - File file1= new File(pdfFilePath); - System.out.println("这是转化后的PDF文件路径"+file1); } catch (Exception e) { e.printStackTrace(); } return pdfFilePath; } + + private static int getFileNumPage(String pdfUrl) { + File pdfFile = new File(pdfUrl); + int pageCount = 0; + try (PDDocument document = PDDocument.load(pdfFile)) { + pageCount = document.getNumberOfPages(); + } catch (IOException e) { + e.printStackTrace(); + } + return pageCount; + } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java new file mode 100644 index 0000000..7b7c0e4 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/OCRUtils.java @@ -0,0 +1,95 @@ +package com.ruoyi.wisdomarbitrate.utils; + +import com.tencentcloudapi.bsca.v20210811.models.LicenseSummary; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import com.tencentcloudapi.ocr.v20181119.OcrClient; +import com.tencentcloudapi.ocr.v20181119.models.SmartStructuralOCRV2Request; +import com.tencentcloudapi.ocr.v20181119.models.SmartStructuralOCRV2Response; +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class OCRUtils { + //API的SecretId + private static final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp"; + //API的SecretKey + private static final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V"; + + public static String pdfIdentifyText(String ImageUrl, Integer PageNumber) { + try { + // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 + // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 + // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("ocr.tencentcloudapi.com"); + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 实例化要请求产品的client对象,clientProfile是可选的 + OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile); + // 实例化一个请求对象,每个接口都会对应一个request对象 + SmartStructuralOCRV2Request req = new SmartStructuralOCRV2Request(); + req.setImageUrl(ImageUrl); + req.setIsPdf(true); + req.setPdfPageNumber(PageNumber.longValue()); + String[] itemNames1 = {"申请人", "统一社会信用代码", "法定代表人", "住所", "联系地址", "委托代理人" + , "联系电话", "电子邮件", "被申请人", "居民身份证号码", "住所", "联系电话", "电子邮件", "仲裁请求" + , "事实和理由"}; + req.setItemNames(itemNames1); + // 返回的resp是一个SmartStructuralOCRV2Response的实例,与请求对象对应 + SmartStructuralOCRV2Response resp = client.SmartStructuralOCRV2(req); + // 输出json格式的字符串回包 + System.out.println(SmartStructuralOCRV2Response.toJsonString(resp)); + //解析数据 + String s = SmartStructuralOCRV2Response.toJsonString(resp); + // 解析JSON数据 + JSONObject jsonObject = new JSONObject(s); + JSONArray structuralList = jsonObject.getJSONArray("StructuralList"); + // 遍历StructuralList中的Groups,获取Key对应的AutoName和Value对应的AutoConten + StringBuilder stringBuilder = new StringBuilder(); // 创建一个StringBuilder对象 + for (int i = 0; i < structuralList.length(); i++) { + JSONArray groups = structuralList.getJSONObject(i).getJSONArray("Groups"); + for (int j = 0; j < groups.length(); j++) { + JSONArray lines = groups.getJSONObject(j).getJSONArray("Lines"); + for (int k = 0; k < lines.length(); k++) { + JSONObject line = lines.getJSONObject(k); + JSONObject key = line.getJSONObject("Key"); + JSONObject value = line.getJSONObject("Value"); + String autoName = key.getString("AutoName"); + String autoContent = value.getString("AutoContent"); + String text = autoName + ":" + autoContent; + if (stringBuilder.length() > 0) { + stringBuilder.append(","); // 在已有内容的情况下添加逗号分隔符 + } + stringBuilder.append(text); // 拼接当前的字符串 + } + } + } + return stringBuilder.toString(); // 获取最终的拼接结果 + } catch (TencentCloudSDKException e) { + System.out.println(e.toString()); + } + return null; + } + + public static void main(String[] args) { + String input = "申请人:招商银行,住所:上海嘉定区,联系地址:上海徐汇区,法定代表人:招商法人,委托代理人:黄海龙,被申请人:白贵勇,住所:陕西,联系地址:陕西,仲裁请求:仲裁委主任依据《2022年版仲裁规则》第三十一条第(四)项、第三十二条的规定指定白贵勇仲裁员为仲裁庭的仲裁员。本案由白贵勇仲裁员成立仲裁庭进行审理。,事实和理由:(一)申请人的仲裁请求及事实和理由,申请人:债务纠纷"; + String[] fields = input.split(","); + JSONObject jsonObject = new JSONObject(); // 创建一个空的JSON对象 + for (String field : fields) { + // 对于每个字段,再按冒号分隔符拆分出键和值 + String[] keyValue = field.split(":"); + if (keyValue.length == 2) { // 对于合法的键值对,将其添加到JSON对象中 + jsonObject.put(keyValue[0], keyValue[1]); + } + } + System.out.println(jsonObject.toString()); + } +} diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java index 7f3a6b6..cb75215 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java @@ -1,5 +1,6 @@ package com.ruoyi.wisdomarbitrate.utils; + import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter; import com.documents4j.job.LocalConverter; @@ -10,9 +11,16 @@ import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.ocr.v20181119.OcrClient; import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse; +import com.tencentcloudapi.ocr.v20181119.models.SmartStructuralOCRV2Request; +import com.tencentcloudapi.ocr.v20181119.models.SmartStructuralOCRV2Response; +import org.apache.pdfbox.pdmodel.PDDocument; +import org.json.JSONArray; +import org.json.JSONObject; import java.io.*; +import java.util.ArrayList; +import java.util.List; public class Tset { public static void main(String[] args) { @@ -21,7 +29,8 @@ public class Tset { //API的SecretKey final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V"; - String pdfFilePath = "D:\\data\\1. 仲裁申请书-陈博.pdf"; + String pdfFilePath = "http://121.40.189.20:8000/API/uploadPath/upload/1.pdf"; + // String pdfFilePath = "http://121.40.189.20:9000/API/uploadPath/upload/2023/11/14/ca2ca4697e5449ff9b9d23f95b221f58.pdf"; try{ // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 @@ -37,16 +46,43 @@ public class Tset { // 实例化要请求产品的client对象,clientProfile是可选的 OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile); // 实例化一个请求对象,每个接口都会对应一个request对象 - GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest(); + SmartStructuralOCRV2Request req = new SmartStructuralOCRV2Request(); req.setImageUrl(pdfFilePath); req.setIsPdf(true); req.setPdfPageNumber(1L); - // 返回的resp是一个GeneralAccurateOCRResponse的实例,与请求对象对应 - GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req); + String[] itemNames1 = {"申请人", "住所"}; + req.setItemNames(itemNames1); + // 返回的resp是一个SmartStructuralOCRV2Response的实例,与请求对象对应 + SmartStructuralOCRV2Response resp = client.SmartStructuralOCRV2(req); // 输出json格式的字符串回包 - System.out.println(GeneralAccurateOCRResponse.toJsonString(resp)); + System.out.println(SmartStructuralOCRV2Response.toJsonString(resp)); + //解析数据 + String s = SmartStructuralOCRV2Response.toJsonString(resp); + // 解析JSON数据 + JSONObject jsonObject = new JSONObject(s); + JSONArray structuralList = jsonObject.getJSONArray("StructuralList"); + // 遍历StructuralList中的Groups,获取Key对应的AutoName和Value对应的AutoContent + List strings = new ArrayList<>(); + for (int i = 0; i < structuralList.length(); i++) { + JSONArray groups = structuralList.getJSONObject(i).getJSONArray("Groups"); + for (int j = 0; j < groups.length(); j++) { + JSONArray lines = groups.getJSONObject(j).getJSONArray("Lines"); + for (int k = 0; k < lines.length(); k++) { + JSONObject line = lines.getJSONObject(k); + JSONObject key = line.getJSONObject("Key"); + JSONObject value = line.getJSONObject("Value"); + String autoName = key.getString("AutoName"); + String autoContent = value.getString("AutoContent"); + String text = autoName +":"+ autoContent; + strings.add(text); + } + } + } + System.out.println(strings); + } catch (TencentCloudSDKException e) { System.out.println(e.toString()); } + } } \ No newline at end of file -- 2.54.0