1.//路径不能有中文只能数字或者英文
File imageFile = new File("D:\\1\\36157.jpg");
String temPath = imageFile.getAbsolutePath();
ParamConfig paramConfig = ParamConfig.getDefaultConfig();
paramConfig.setDoAngle(true);
paramConfig.setMostAngle(true);
InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V4);
// 开始识别
OcrResult ocrResult = engine.runOcr(temPath, paramConfig);
System.out.println("识别内容: " + ocrResult.getStrRes());
ocrResult.getStrRes()为识别内容,创建一个新集合并添加到集合里。通过\n去分割获取文字
pom包
<!-- rapidocr这个必须要引入 -->
<!-- 可前往maven中央仓库https://central.sonatype.com/artifact/io.github.mymonstercat/rapidocr/versions,查看版本 -->
<dependency>
<groupId>io.github.mymonstercat</groupId>
<artifactId>rapidocr</artifactId>
<version>0.0.7</version>
</dependency>
<!-- 一般只需要引入一个,CPU端建议使用onnx,移动端建议使用ncnn -->
<!-- 可前往maven中央仓库https://central.sonatype.com/artifact/io.github.mymonstercat/rapidocr-onnx-platform/versions,查看版本 -->
<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-ncnn-platform</artifactId>
<version>0.0.7</version>
</dependency>
2。//可以将图片从原路径拿出来放到D盘目录下然后识别图片中的文字。之后在删除(复制图片代码)
File result = new File(s);//需要复制到的路径,以及图片的新命名+格式
//获取s的文件名
String name = result.getName();
name = "D:/1/" + name;
FileInputStream input = new FileInputStream(s);//需要复制的原图的路径+图片名+ .png(这是该图片的格式)
FileOutputStream out = new FileOutputStream(name);
byte[] buffer = new byte[1024];//一个容量,相当于打水的桶,可以自定义大小
int hasRead = 0;
while ((hasRead = input.read(buffer)) > 0) {
out.write(buffer, 0, hasRead);//0:表示每次从0开始
}
System.out.println(result.getAbsolutePath());
input.close();//关闭
out.close();
3.用完之后记得删除复制路径的图片(allFilePathImages为图片集合list。)
for (String filePathImage : allFilePathImages) {
File file = new File(filePathImage);
if (file.exists()) {
file.delete();
}
}
4。递归调用获取所有图片
public static List<String> getAllFilePathsPdf(String directoryPath, List<String> filePaths) {
File directory = new File(directoryPath);
File[] files = directory.listFiles((dir, name) -> true);
// 获取所有文件和子目录
if (files != null) { for (File file : files) {
if (file.isFile()) {
String absolutePath = file.getAbsolutePath();
if (absolutePath.contains("退役军人人事档案目录.docx")) {
filePaths.add(absolutePath);
}
} else if (file.isDirectory()) {
// 如果需要递归获取子目录中的文件,可以在这里添加递归调用 getAllFilePaths(file.getAbsolutePath(), filePaths); /
/ 递归获取子目录中的文件路径 }
}
}
return filePaths;
}

2969

被折叠的 条评论
为什么被折叠?



