1.rapidocr 使用
1.1依赖maven
<properties>
<hutool.version>5.8.18</hutool.version>
<lombok.version>1.18.20</lombok.version>
<rapidocr.version>0.0.7</rapidocr.version>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- rapidocr这个必须要引入 -->
<!-- 可前往maven中央仓库https://central.sonatype.com/artifact/io.github.mymonstercat/rapidocr/versions,查看版本 -->
<dependency>
<groupId>io.github.mymonstercat</groupId>
<artifactId>rapidocr</artifactId>
<version>${rapidocr.version}</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>${rapidocr.version}</version>
</dependency>
<dependency>
<groupId>io.github.mymonstercat</groupId>
<artifactId>rapidocr-ncnn-platform</artifactId>
<version>${rapidocr.version}</version>
</dependency>
</dependencies>
1.2 ocrUtils 工具类 以及使用
@Slf4j
public class OcrUtils {
@SneakyThrows
public static void main(String[] args) {
boolean isInput = false;
InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
String imagePath = "C:\\Users\\Administrator\\Desktop\\Snipaste_2024-08-12_18-12-35.png";
imagePath = "https://img.zcool.cn/community/0177c35548e8fb0000019ae93040ce.jpg@2o.jpg";
InputStream input = null;
String prx = imagePath.substring(imagePath.lastIndexOf("."), imagePath.length());
try {
input = new URL(imagePath).openStream();
isInput = true;
} catch (Exception e) {
e.printStackTrace();
}
if (true){
OcrResult ocrResult = ocr(input, prx, "temp/" ,null);
System.err.println(JSONUtil.toJsonStr(ocrResult));
System.err.println(ocrResult.getStrRes().trim());
return;
}
}
public static OcrResult ocr(InputStream input, String suffix, String ocrFileTempPath, Model model) {
InferenceEngine engine = InferenceEngine.getInstance(ObjectUtil.isNotEmpty(model) ? model : Model.ONNX_PPOCR_V3);
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
String pathname = ocrFileTempPath + format + "/" + UUID.randomUUID() + suffix;
log.info("ocr file init pathname:{}", pathname);
File file = new File(pathname);
try {
OutputStream output = FileUtil.getOutputStream(file);
IoUtil.copy(input, output);
String path = file.getPath();
OcrResult ocrResult = engine.runOcr(path);
log.info("ocr result:{}", JSONUtil.toJsonStr(ocrResult));
return ocrResult;
} finally {
FileUtil.del(file);
log.info("ocr file del file pathname:{}", pathname);
}
}
}