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

测试时使用的图片

相关推荐
EkihzniY11 分钟前
AI+OCR:解锁数字化新视界
人工智能·ocr
青青草原技术员灰太狼24 分钟前
Nginx的https搭建
linux·服务器·网络
小坏讲微服务26 分钟前
SpringBoot4.0整合knife4j 在线文档完整使用
java·spring cloud·在线文档·knife4j·文档·接口文档·swagger-ui
8***Z8936 分钟前
springboot 异步操作
java·spring boot·mybatis
i***132440 分钟前
Spring BOOT 启动参数
java·spring boot·后端
坚持不懈的大白43 分钟前
后端:SpringMVC
java
IT_Octopus44 分钟前
(旧)Spring Securit 实现JWT token认证(多平台登录&部分鉴权)
java·后端·spring
kk哥88991 小时前
Spring详解
java·后端·spring
S***26751 小时前
Spring Cloud Gateway 整合Spring Security
java·后端·spring
Tao____1 小时前
开源物联网平台
java·物联网·mqtt·开源·设备对接