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文件)

测试时使用的图片

相关推荐
hehelm11 小时前
IO 多路复用 — Reactor
linux·服务器·网络·数据库·c++
cfm_291412 小时前
Spring监听器
java·spring boot·后端
SamDeepThinking12 小时前
Java面向对象在JVM里怎么实现
java·后端·面试
Sagittarius_A*12 小时前
Web 渗透实战:服务器系统识别、端口与中间件全量探测
服务器·网络安全·中间件·渗透测试
咸鱼翻身小阿橙13 小时前
多线程解析
java·开发语言
一枚码农出身的猎头13 小时前
岗位招聘:架构师,base湖南长沙,70-80w
java·架构
studytosky13 小时前
OpenClaw 入门与 Skill 开发
linux·服务器·ai编程
kaoa00013 小时前
Linux入门攻坚——82、kvm虚拟化-2
linux·运维·服务器
tachibana214 小时前
hot100 课程表(207)
java·数据结构·算法·leetcode
神仙别闹14 小时前
编写基于C++ Huffman 算法的无损压缩程序和解压程序
java·c++·算法