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

测试时使用的图片

相关推荐
念恒123064 小时前
传输层协议TCP
服务器·网络·tcp/ip
东坡白菜4 小时前
windows下使用Trae启动java项目遇到的乱码问题
java
SQL-First布道者5 小时前
⚡Spring JDBC 完整体系 · 第 10 集 · 手搓 `BaseCondition`
java·spring boot·后端·spring·mybatis·spring jdbc
珍珠先生5 小时前
第9章 JDK 11 新特性:var 与便捷新 API
java
比兔代理5 小时前
代理IP池性能瓶颈在哪?并发、带宽与IP质量的平衡优化策略
服务器·网络·tcp/ip·安全
hoho_125 小时前
麒麟V10升级nginx最新版本到1.31.4
java·服务器·nginx
冬夜戏雪5 小时前
笔试的Scanner in Scanner out
java·开发语言
BlueAsia_Lab5 小时前
Wi‑Fi 认证:企业办理需要准备哪些资料?
运维·服务器·网络
南城以南溫暖如初1475 小时前
社交裂变商城系统设计与实战开发指南
java·spring boot·mysql·vue·mybatis
Zane1994125 小时前
HashSet、TreeSet、LinkedHashSet:底层都是“套壳“
java·开发语言