java实现ocr功能(Tesseract OCR)

1、pom文件中引入依赖

复制代码
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.5.4</version>
</dependency>

2、下载语言库文件(不要放到resources下,可以放到项目所在目录下,在博主的主页资源菜单下可下载,也可自行在网上找资源下载)

参考目录结构(也可以放其他位置,只要代码能找到就行):

3、代码:

java 复制代码
@PostMapping("/ocr2")
@ApiOperationSupport(order = 3)
@ApiOperation(value = "识别图像2", notes = "上传图像")
public R<String> ocr2(@RequestBody MultipartFile file) throws Exception {
    Tesseract instance = new Tesseract();
    // 设置Tesseract的语言库路径
    ClassPathResource classPathResource = new ClassPathResource("/tessdata");
    String tessDataPath = classPathResource.getPath();
    instance.setDatapath(tessDataPath);
    instance.setLanguage("chi_sim");
    String ocrResult = "";
    File tfile = File.createTempFile("tempfile", file.getOriginalFilename());
    file.transferTo(tfile);
    ocrResult = instance.doOCR(tfile);
    return R.data(ocrResult);
}

4、测试接口(只能上传jpg文件)

测试时使用的图片

相关推荐
顶点多余7 分钟前
自定义协议、序列化、反序列化实现
java·linux·开发语言·c++·tcp/ip
小新同学^O^15 分钟前
简单学习 --> SpringAOP
java·学习·spring·aop
风味蘑菇干17 分钟前
使用接口定义规范,实现类完成具体逻辑。
java·开发语言
Zephyr_021 分钟前
java数据结构
java·数据结构
2401_8332693026 分钟前
Java多线程:从入门到进阶
java·开发语言
故事还在继续吗36 分钟前
高性能网络
服务器·网络·c/c++
NE_STOP36 分钟前
Redis--Redis分布式系统的原理与实操
java
认真的薛薛37 分钟前
阿里云: A记录 & CNAME
服务器·前端·阿里云
步十人38 分钟前
【FastAPI】ORM-02.使用 ORM 高效处理数据库逻辑
服务器·数据库·fastapi
Forget_855039 分钟前
RHEL——Kubernetes容器编排平台(二)
java·开发语言