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

测试时使用的图片

相关推荐
wuminyu1 小时前
Kafka中sendfile与mmap实现机制解析
java·linux·c语言·jvm·c++
前端世界2 小时前
Linux服务器实战:NTP时间同步、SELinux权限与rsyslog日志管理,一次搞懂三大运维问题
linux·运维·服务器
Android系统攻城狮2 小时前
Linux Gstreamer深度解析之gst_audio_converter_new调用流程与实战(二十)
linux·运维·服务器·gstreamer音视频·音视频进阶
考虑考虑2 小时前
Java实现hmacsha256加密算法
java·后端·java ee
captain3763 小时前
▲网络原理(2)-TCP
java·服务器·网络·tcp/ip·java-ee
MetaLite3 小时前
SpringBoot接口分层规范-外网网关内部服务与参数边界
java·数据库·spring boot
Co_Hui3 小时前
Java 线程状态
java
土司大王3 小时前
LeetCode hot100——35.搜索插入位置:Java 二分模板、左闭右开区间与插入点分析
java·算法·leetcode
言乐64 小时前
Python加速器4跨境网络加速器
运维·服务器·开发语言·网络·python
土司大王4 小时前
LeetCode hot100——34.在排序数组中查找元素的第一个和最后一个位置:Java 二分模板、边界分析
java·算法·leetcode