Merge branch 'hjb' of SH-Arbitrate/Arbitrate-Backend into dev

This commit was merged in pull request #243.
This commit is contained in:
2023-11-27 17:32:03 +08:00
committed by Gitea
4 changed files with 272 additions and 58 deletions
+5 -1
View File
@@ -174,7 +174,11 @@
<artifactId>documents4j-transformer-msoffice-word</artifactId> <artifactId>documents4j-transformer-msoffice-word</artifactId>
<version>1.0.3</version> <version>1.0.3</version>
</dependency> </dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
@@ -40,6 +40,7 @@ import com.ruoyi.wisdomarbitrate.domain.*;
import com.ruoyi.wisdomarbitrate.domain.vo.SendRoomNoMessageVO; import com.ruoyi.wisdomarbitrate.domain.vo.SendRoomNoMessageVO;
import com.ruoyi.wisdomarbitrate.mapper.*; import com.ruoyi.wisdomarbitrate.mapper.*;
import com.ruoyi.wisdomarbitrate.service.ICaseApplicationService; import com.ruoyi.wisdomarbitrate.service.ICaseApplicationService;
import com.ruoyi.wisdomarbitrate.utils.OCRUtils;
import com.ruoyi.wisdomarbitrate.utils.SignAward; import com.ruoyi.wisdomarbitrate.utils.SignAward;
import com.ruoyi.wisdomarbitrate.utils.UnZipFileUtils; import com.ruoyi.wisdomarbitrate.utils.UnZipFileUtils;
import com.tencentyun.TLSSigAPIv2; 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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; 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.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -980,10 +982,10 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
if (insertRow != 0 && CollectionUtil.isNotEmpty(caseAffiliates)) { if (insertRow != 0 && CollectionUtil.isNotEmpty(caseAffiliates)) {
caseAffiliates.forEach(caseAffiliate -> caseAffiliate.setCaseAppliLogId(caseApplication.getId())); caseAffiliates.forEach(caseAffiliate -> caseAffiliate.setCaseAppliLogId(caseApplication.getId()));
// 插入案件日志人员相关表 // 插入案件日志人员相关表
if(CollectionUtil.isNotEmpty(caseAttachList)) { if (CollectionUtil.isNotEmpty(caseAttachList)) {
List<CaseAttach> filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); List<CaseAttach> filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList());
// 插入日志附件表 // 插入日志附件表
if(CollectionUtil.isNotEmpty(filterList)) { if (CollectionUtil.isNotEmpty(filterList)) {
for (CaseAttach caseAttach : filterList) { for (CaseAttach caseAttach : filterList) {
// 查询附件表 // 查询附件表
CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId()); CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId());
@@ -992,7 +994,8 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
} }
} }
} caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates); }
caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates);
} }
}); });
@@ -1099,9 +1102,9 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
List<CaseAttach> caseAttachList = caseApplication.getCaseAttachList(); List<CaseAttach> caseAttachList = caseApplication.getCaseAttachList();
// 立案申请状态直接修改主表信息 // 立案申请状态直接修改主表信息
if (caseAttachList != null && caseAttachList.size() > 0 if (caseAttachList != null && caseAttachList.size() > 0
&& caseApplication.getCaseStatus()!=null && caseApplication.getCaseStatus().equals(CaseApplicationConstants.CASE_APPLICATION)) { && caseApplication.getCaseStatus() != null && caseApplication.getCaseStatus().equals(CaseApplicationConstants.CASE_APPLICATION)) {
List<CaseAttach> filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); List<CaseAttach> filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList());
if(CollectionUtil.isNotEmpty(filterList)){ if (CollectionUtil.isNotEmpty(filterList)) {
// 先删除2的附件在新增 // 先删除2的附件在新增
// caseAttachMapper.deleteByCasedIdAndType(caseApplication.getId(),2,0); // caseAttachMapper.deleteByCasedIdAndType(caseApplication.getId(),2,0);
// for (CaseAttach caseAttach : filterList) { // for (CaseAttach caseAttach : filterList) {
@@ -1133,16 +1136,16 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
try { try {
caseApplication.setCaseAppliId(caseApplication.getId()); caseApplication.setCaseAppliId(caseApplication.getId());
int insertRow = caseApplicationLogMapper.insert(caseApplication); int insertRow = caseApplicationLogMapper.insert(caseApplication);
if (insertRow != 0 ) { if (insertRow != 0) {
if( CollectionUtil.isNotEmpty(caseAffiliates)) { if (CollectionUtil.isNotEmpty(caseAffiliates)) {
caseAffiliates.forEach(caseAffiliate -> caseAffiliate.setCaseAppliLogId(caseApplication.getId())); caseAffiliates.forEach(caseAffiliate -> caseAffiliate.setCaseAppliLogId(caseApplication.getId()));
// 插入案件日志人员相关表 // 插入案件日志人员相关表
caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates); caseAffiliateLogMapper.batchCaseAffiliate(caseAffiliates);
} }
if(CollectionUtil.isNotEmpty(caseAttachList)) { if (CollectionUtil.isNotEmpty(caseAttachList)) {
List<CaseAttach> filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList()); List<CaseAttach> filterList = caseAttachList.stream().filter(c -> c.getAnnexType().equals(2)).collect(Collectors.toList());
// 插入日志附件表 // 插入日志附件表
if(CollectionUtil.isNotEmpty(filterList)) { if (CollectionUtil.isNotEmpty(filterList)) {
for (CaseAttach caseAttach : filterList) { for (CaseAttach caseAttach : filterList) {
// 查询附件表 // 查询附件表
CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId()); CaseAttach attach = caseAttachMapper.queryAnnexById(caseAttach.getAnnexId());
@@ -2806,17 +2809,17 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
if (CollectionUtil.isEmpty(reservedConferences)) { if (CollectionUtil.isEmpty(reservedConferences)) {
return reservedConferences; return reservedConferences;
} }
Map<Long,String> userIdMap = null; Map<Long, String> userIdMap = null;
List<Long> userIds = reservedConferences.stream().map(ReservedConference::getUserId).collect(Collectors.toList()); List<Long> userIds = reservedConferences.stream().map(ReservedConference::getUserId).collect(Collectors.toList());
if(CollectionUtil.isNotEmpty(userIds)){ if (CollectionUtil.isNotEmpty(userIds)) {
// 根据userids查询用户名 // 根据userids查询用户名
List<SysUser> userList = sysUserMapper.selectUserListByIds(userIds); List<SysUser> userList = sysUserMapper.selectUserListByIds(userIds);
if(CollectionUtil.isNotEmpty(userList)){ if (CollectionUtil.isNotEmpty(userList)) {
userIdMap = userList.stream().collect(Collectors.toMap(SysUser::getUserId,SysUser::getUserName)); userIdMap = userList.stream().collect(Collectors.toMap(SysUser::getUserId, SysUser::getUserName));
} }
} }
for (ReservedConference reservedConference : reservedConferences) { for (ReservedConference reservedConference : reservedConferences) {
if(null!=reservedConference.getUserId() && null!=userIdMap){ if (null != reservedConference.getUserId() && null != userIdMap) {
reservedConference.setUserName(userIdMap.get(reservedConference.getUserId())); reservedConference.setUserName(userIdMap.get(reservedConference.getUserId()));
} }
Date startTime = reservedConference.getScheduleStartTime(); Date startTime = reservedConference.getScheduleStartTime();
@@ -2852,41 +2855,106 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
if (file.isEmpty()) { if (file.isEmpty()) {
return AjaxResult.error("请选择要上传的文件"); return AjaxResult.error("请选择要上传的文件");
} }
//String targetPath = "/home/ruoyi/uploadPath/upload/unzipFile";
UUID uuid = UUID.randomUUID(); 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; File zipFile = null;
InputStream ins = null; InputStream ins = null;
try { try {
ins = file.getInputStream(); ins = file.getInputStream();
String savePath = "D:\\develop\\java\\"; //上传的压缩包保存的路径
String saveName = uuid+file.getOriginalFilename(); //String savePath = "D:\\develop\\java\\";
zipFile = new File(savePath+saveName); String savePath = "\\home\\ruoyi\\uploadPath\\upload\\zipFile\\";
String saveName = uuid + file.getOriginalFilename();
zipFile = new File(savePath + saveName);
inputChangeToFile(ins, zipFile); inputChangeToFile(ins, zipFile);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
//解压缩上传的压缩包 //解压缩上传的压缩包
boolean unzipSuccess = UnZipFileUtils.unZipFile(zipFile, targetPath); boolean unzipSuccess = UnZipFileUtils.unZipFile(zipFile, targetPath);
if (unzipSuccess ) { if (unzipSuccess) {
//去掉后缀名,拿到解压后的文件夹路径
String fileName = file.getOriginalFilename();
String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf("."));
File directory = new File(targetPath); File directory = new File(targetPath);
List<String> andConvertPDF = findAndConvertPDF(directory); List<String> andConvertPDF = findAndConvertPDF(directory);
if (andConvertPDF != null && andConvertPDF.size() > 0){ if (andConvertPDF != null && andConvertPDF.size() > 0) {
// 返回找到的PDF文件路径 //遍历路径
return AjaxResult.success(andConvertPDF); for (String pdfUrl : andConvertPDF) {
}else { //获取文件的页数
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<CaseAffiliate> 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("未找到符合条件的文件"); return AjaxResult.error("未找到符合条件的文件");
} }
}else { } else {
// 解压失败 // 解压失败
return AjaxResult.error("解压失败"); return AjaxResult.error("解压失败");
} }
} }
public static List<String> findAndConvertPDF(File directory) { public static List<String> findAndConvertPDF(File directory) {
List<String> pdfPaths = new ArrayList<>(); List<String> pdfPaths = new ArrayList<>();
if (directory.isFile()) { if (directory.isFile()) {
@@ -2903,13 +2971,13 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
path = pdfPath; path = pdfPath;
} }
} }
if (path!= null) { if (path != null) {
pdfPaths.add(path); pdfPaths.add(path);
} }
} }
} else if (directory.isDirectory()) { } else if (directory.isDirectory()) {
searchAndConvertPDF(directory, pdfPaths); searchAndConvertPDF(directory, pdfPaths);
}else { } else {
return null; return null;
} }
return pdfPaths; return pdfPaths;
@@ -2929,6 +2997,7 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
return ""; return "";
} }
} }
public static void searchAndConvertPDF(File directory, List<String> pdfPaths) { public static void searchAndConvertPDF(File directory, List<String> pdfPaths) {
File[] files = directory.listFiles(); File[] files = directory.listFiles();
if (files != null) { if (files != null) {
@@ -2954,16 +3023,18 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
} }
} }
} }
private static String convertToPDF(File file) { private static String convertToPDF(File file) {
String wordFilePath = file.getAbsolutePath(); 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); File directory = new File(pdfSaveDirectory);
if (!directory.exists()) { if (!directory.exists()) {
directory.mkdirs(); directory.mkdirs();
} }
String name = file.getName(); String name = file.getName();
String nameWithoutExtension = name.substring(0, name.lastIndexOf(".")); String nameWithoutExtension = name.substring(0, name.lastIndexOf("."));
String pdfFilePath =pdfSaveDirectory+nameWithoutExtension+".pdf"; String pdfFilePath = pdfSaveDirectory + nameWithoutExtension + ".pdf";
File inputWord = new File(wordFilePath); File inputWord = new File(wordFilePath);
File outputFile = new File(pdfFilePath); File outputFile = new File(pdfFilePath);
try { try {
@@ -2973,15 +3044,23 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
docxInputStream.close(); docxInputStream.close();
outputStream.close(); outputStream.close();
System.out.println("success");
File file1= new File(pdfFilePath);
System.out.println("这是转化后的PDF文件路径"+file1);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return pdfFilePath; 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;
}
} }
@@ -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());
}
}
@@ -1,5 +1,6 @@
package com.ruoyi.wisdomarbitrate.utils; package com.ruoyi.wisdomarbitrate.utils;
import com.documents4j.api.DocumentType; import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter; import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter; 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.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest; import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse; 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.io.*;
import java.util.ArrayList;
import java.util.List;
public class Tset { public class Tset {
public static void main(String[] args) { public static void main(String[] args) {
@@ -21,7 +29,8 @@ public class Tset {
//API的SecretKey //API的SecretKey
final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V"; 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{ try{
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
@@ -37,16 +46,43 @@ public class Tset {
// 实例化要请求产品的client对象,clientProfile是可选的 // 实例化要请求产品的client对象,clientProfile是可选的
OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile); OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象 // 实例化一个请求对象,每个接口都会对应一个request对象
GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest(); SmartStructuralOCRV2Request req = new SmartStructuralOCRV2Request();
req.setImageUrl(pdfFilePath); req.setImageUrl(pdfFilePath);
req.setIsPdf(true); req.setIsPdf(true);
req.setPdfPageNumber(1L); req.setPdfPageNumber(1L);
// 返回的resp是一个GeneralAccurateOCRResponse的实例,与请求对象对应 String[] itemNames1 = {"申请人", "住所"};
GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req); req.setItemNames(itemNames1);
// 返回的resp是一个SmartStructuralOCRV2Response的实例,与请求对象对应
SmartStructuralOCRV2Response resp = client.SmartStructuralOCRV2(req);
// 输出json格式的字符串回包 // 输出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<String> 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) { } catch (TencentCloudSDKException e) {
System.out.println(e.toString()); System.out.println(e.toString());
} }
} }
} }