1.引入jar
<!-- 一般只需要引入一个,CPU端建议使用onnx,移动端建议使用ncnn -->
<!-- 可前往maven中央仓库https://repo1.maven.org/maven2/io/github/mymonstercat/,查看版本 -->
<dependency>
<groupId>io.github.mymonstercat</groupId>
<artifactId>rapidocr-onnx-platform</artifactId>
<version>0.0.7</version>
</dependency>
<dependency>
<groupId>io.github.mymonstercat</groupId>
<artifactId>rapidocr-onnx-linux-x86_64</artifactId>
<version>1.2.2</version>
</dependency>
直接上代码
String adress="文件实际地址";
File a= new File(adress);
if(!a.isFile()){
return ResponseVO.error("文件不存在");
}else {
log.info("ocr 文件存在");
}
try {
ParamConfig paramConfig = ParamConfig.getDefaultConfig();
paramConfig.setDoAngle(true);
paramConfig.setMostAngle(true);
InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
//填写图片路径 获取图片上的文本
OcrResult ocrResult = engine.runOcr(adress,paramConfig);
log.info(">>>>>"+ocrResult);
// 去除空字符
String orcStr=ocrResult.getStrRes().trim();
Map<String ,String> dataMap= new HashMap<>();
dataMap.put("mc","");
dataMap.put("zjhm","");
log.info(">>>>>"+orcStr);
if(ValidateUtil.noNull(orcStr)){
orcStr=orcStr.replaceAll("\\r?\\n", "");
if(orcStr.contains("营业执照")){
Pattern pattern = Pattern.compile("[0-9A-Z]{18}");
Matcher matcher = pattern.matcher(orcStr);
while (matcher.find()) {
String num=matcher.group(0);
if(!num.matches("\\d+")){
dataMap.put("zjhm",matcher.group(0));
}
}
if(orcStr.contains("统一社会信用代码营业执照")){ //0
if(orcStr.contains("注册资本") &&orcStr.contains("名称") &&orcStr.indexOf("注册资本") -orcStr.indexOf("名称") >0 &&orcStr.indexOf("注册资本") -orcStr.indexOf("名称") <31 ){
dataMap.put("mc",orcStr.substring(orcStr.indexOf("名称")+2,orcStr.indexOf("注册资本")));
}
if(orcStr.contains("成立日期") &&orcStr.contains("名称") &&orcStr.indexOf("成立日期")-orcStr.indexOf("名称") < 31&&orcStr.indexOf("成立日期")-orcStr.indexOf("名称") >0){
dataMap.put("mc",orcStr.substring(orcStr.indexOf("名称")+2,orcStr.indexOf("成立日期")));
}
if(orcStr.indexOf("称")<orcStr.indexOf("名") &&orcStr.indexOf("称")-orcStr.indexOf("名")<31){
dataMap.put("mc",orcStr.substring(orcStr.indexOf("称")+1,orcStr.indexOf("名")));
}
}else if(orcStr.indexOf("法定代表人")==0){ //90
if(orcStr.contains(")") && orcStr.indexOf(")")<orcStr.indexOf("营业执照")){
dataMap.put("mc",orcStr.substring(orcStr.indexOf(")")+1,orcStr.indexOf("营业执照")));
}
if(orcStr.contains(")") && orcStr.indexOf(")")<orcStr.indexOf("营业执照")){
dataMap.put("mc",orcStr.substring(orcStr.indexOf(")")+1,orcStr.indexOf("营业执照")));
}
} else if(orcStr.contains("营业执照统一社会信用代码")){ //180
if(orcStr.indexOf("称")<orcStr.indexOf("名")){
dataMap.put("mc",orcStr.substring(orcStr.indexOf("称")+1,orcStr.indexOf("名")));
}
}
}
if(orcStr.contains("公民身份号码")){
if(orcStr.contains("姓名")){
if(orcStr.indexOf("姓名")<orcStr.indexOf("性别")){ //0 90 270
dataMap.put("mc",orcStr.substring(orcStr.indexOf("姓名")+2,orcStr.indexOf("性别")).replace("公民身份号码",""));
}else if(orcStr.indexOf("姓名")>orcStr.indexOf("性别")) { //180
dataMap.put("mc",orcStr.substring(orcStr.indexOf("姓名")+2));
}
}
//获取身份证号
Pattern pattern = Pattern.compile("[0-9A-Z]{18}");
Matcher matcher = pattern.matcher(orcStr);
while (matcher.find()) {
dataMap.put("zjhm",matcher.group(0));
}
}
if(orcStr.contains("签发机关")||orcStr.contains("有效期限")||orcStr.contains("中华人民共和国")||orcStr.contains("居民身份证")){
orcStr=orcStr.replaceAll("中华人民共和国","").replace("益发机关","").replace("签发机关","").replace("有效期限","").replace("居民身份证","");
log.info(">>>>>"+orcStr);
String regex = "(\\d{4}\\.\\d{2}\\.\\d{2}-\\d{4}\\.\\d{2}\\.\\d{2})";
if(orcStr.contains("长期")){
regex = "(\\d{4}\\.\\d{2}\\.\\d{2}-长期)";
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(orcStr);
while (matcher.find()) {
dataMap.put("zjrq",matcher.group(0));
if(orcStr.indexOf(matcher.group(0))==0){
dataMap.put("qfjg",orcStr.substring(matcher.group(0).length()).trim());
}else{
dataMap.put("qfjg",orcStr.substring(0,orcStr.indexOf("局")+1).trim());
}
}
}
}
return ResponseVO.successData(dataMap);
}catch (Exception e){
log.info(">Exception>>>>"+e);
return ResponseVO.success();
} catch (Throwable e) {
// 捕获异常并处理
log.info(">Throwable>>>>"+e.getMessage());
return ResponseVO.success();
}