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

测试时使用的图片

相关推荐
a努力。1 分钟前
Redis Java 开发系列#2 数据结构
java·数据结构·redis
遇见火星32 分钟前
Linux性能调优:理解CPU中的平均负载和使用率
linux·运维·服务器·cpu
a努力。1 小时前
腾讯Java面试被问:String、StringBuffer、StringBuilder区别
java·开发语言·后端·面试·职场和发展·架构
Vic101012 小时前
解决 Spring Security 在异步线程中用户信息丢失的问题
java·前端·spring
QD_IT伟2 小时前
SpringBoot项目整合Tlog 数据链路的规范加强
java·spring boot·后端
源码获取_wx:Fegn08952 小时前
基于springboot + vue二手交易管理系统
java·vue.js·spring boot·后端·spring·课程设计
Zsh-cs2 小时前
Spring
java·数据库·spring
爬山算法2 小时前
Springboot请求和响应相关注解及使用场景
java·spring boot·后端
程序员水自流2 小时前
MySQL InnoDB存储引擎详细介绍之事务
java·数据库·mysql·oracle
请为小H留灯2 小时前
Java实际开发@常用注解(附实战场景)
java·后端·个人开发