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

测试时使用的图片

相关推荐
深盾科技2 分钟前
融合C++与Python:兼顾开发效率与运行性能
java·c++·python
我待_JAVA_如初恋3 分钟前
idea创建MavenJavaWeb项目以后,包结构缺java
java·ide·intellij-idea
来深圳9 分钟前
leetcode 739. 每日温度
java·算法·leetcode
问道飞鱼13 分钟前
【Linux知识】Linux 虚拟机磁盘扩缩容操作指南(按文件系统分类)
linux·运维·服务器·磁盘扩缩容
CC大煊13 分钟前
【java】Druid数据库连接池完整配置指南:从入门到生产环境优化
java·数据库·springboot
JIngJaneIL27 分钟前
基于java+ vue交友系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·交友
苹果酱056734 分钟前
解决linux mysql命令 bash: mysql: command not found 的方法
java·vue.js·spring boot·mysql·课程设计
风雨飘逸38 分钟前
【shell&bash进阶系列】(二十一)向脚本传递参数(shift和getopts)
linux·运维·服务器·经验分享·bash
zly350043 分钟前
删除文件(rm 命令 删除目录)
linux·运维·服务器
fbllfbll44 分钟前
Alpine下部署Nginx+MAZANOKE在线批量压缩图片
服务器·nginx·pve·alpine·lxc容器·在线压缩图片·mazanoke