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

测试时使用的图片

相关推荐
特立独行的猫A3 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
一起_java3 小时前
技术文章-基于Dify-Agent的智能问答系统设计与实现
java·spring boot·后端·agent·dify
Flynn Liang4 小时前
I/O 密集型高并发实战:Java 服务 10 维优化,从线程打满到 10 万 QPS
java·spring boot·性能优化·高并发·系统设计·虚拟线程·io密集型
吃我一窝jio5 小时前
Python与Go线程模型深度对比
java·开发语言·python
这个昵称不容易6 小时前
SpringBoot 购物推荐网站的设计与实现:120 个 Java 文件、147 个接口、13 张表,附真实运行截图
java·springboot·毕设
文丑颜不良啊6 小时前
LeetCode3041~3060题目解析
java·leetcode
科研邪修法6 小时前
中英混排OCR评估:从字符输出到字段验收
ocr
文丑颜不良啊7 小时前
LeetCode3101~3120题目解析
java·leetcode
Resean02237 小时前
SpringMVC 6+源码分析(十一)视图渲染与异步请求(收尾篇)
java·spring·springmvc
靠沿8 小时前
MySQL数据库进阶——存储过程和触发器
java·数据库·mysql