SpringBoot + Tess4J 实现本地与远程图片的文字识别

42 篇文章 3 订阅

1 前言

1.1 概要

在本文中,我们将探讨如何在Spring Boot应用程序里集成Tess4J来实现OCR(光学字符识别),以识别出本地和远程图片中的文字。

我们将从添加依赖说起,然后创建服务类以实现OCR,最后展示如何处理用户上传的本地图片和远程图片URL进行文字识别。

1.2 背景

随着信息技术的不断进步,图片中的文字提取已经越来越多地应用于数据输入和自动化处理过程。Tess4J,作为Tesseract OCR引擎的Java JNA封装,提供了一个能力强大的接口来实现这一功能。

在Spring Boot中整合Tess4J,我们可以快速地在Java应用中优雅地实现文字识别。本文将在Spring Boot项目中实现这一功能。

2 代码实战

2.1 第1部分:环境搭建

在开始之前,请确保有以下环境配置:

  • JDK 1.8或更高版本
  • Maven
  • 最新版的Spring Boot
  • Tess4J版本4.x或更高

2.2 第2部分:添加依赖

在pom.xml中加入以下依赖,以便于使用Tess4J:

<dependencies>
    <dependency>
        <groupId>net.sourceforge.tess4j</groupId>
        <artifactId>tess4j</artifactId>
        <version>4.5.4</version>
    </dependency>
    <!-- 其他依赖 -->
</dependencies>

确保以上版本是最新的,或者是适配当前开发环境的版本。

2.3 第3部分:添加Tessdata语言库

百度网盘下载:

链接:https://pan.baidu.com/s/1rzOYZz3gfrbWmGRUolflqQ
提取码:em4z

在这里插入图片描述

2.4 第4部分:创建OCR服务类

@Service
public class OcrService {

    public String recognizeText(File imageFile) throws TesseractException {
        Tesseract tesseract = new Tesseract();
        
        // 设定训练文件的位置(如果是标准英文识别,此步可省略)
        tesseract.setDatapath("tessdata各语言集合包地址");
        tesseract.setLanguage("chi_sim");
        return tesseract.doOCR(imageFile);
    }

    public String recognizeTextFromUrl(String imageUrl) throws Exception {
        URL url = new URL(imageUrl);
        InputStream in = url.openStream();
        Files.copy(in, Paths.get("downloaded.jpg"), StandardCopyOption.REPLACE_EXISTING);

        File imageFile = new File("downloaded.jpg");
        return recognizeText(imageFile);
    }
}

在这段代码中,recognizeText(File imageFile)方法负责执行对本地文件的OCR任务,而recognizeTextFromUrl(String imageUrl)方法则先将远程图片下载到本地,然后再执行OCR。

2.5 第5部分:建立REST控制器

@RestController
@RequestMapping("/api/ocr")
public class OcrController {

    private final OcrService ocrService;

    // 使用构造器注入OcrService
    public OcrController(OcrService ocrService) {
        this.ocrService = ocrService;
    }

    @PostMapping("/upload")
    public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
        try {
            File convFile = new File(System.getProperty("java.io.tmpdir")+"/"+file.getOriginalFilename());
            file.transferTo(convFile);
            String result = ocrService.recognizeText(convFile);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("识别发生错误:" + e.getMessage());
        }
    }

    @GetMapping("/recognize-url")
    public ResponseEntity<String> recognizeFromUrl(@RequestParam("imageUrl") String imageUrl) {
        try {
            String result = ocrService.recognizeTextFromUrl(imageUrl);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.badRequest().body("从URL识别发生错误:" + e.getMessage());
        }
    }
}

在这个控制器中,我们创建了两个端点:/api/ocr/upload用于处理用户上传的本地图片,而/api/ocr/recognize-url则处理给定URL的远程图片。

3 测试

使用Postman直接请求:

在这里插入图片描述

4 小结

通过以上步骤,现在拥有了一个能够处理本地和远程图片文字识别的Spring Boot服务。在实践中,可能需要根据实际情况调整配置,例如在多语言环境中设置正确的语言包等。

尽管OCR技术仍然有提升空间,但通过Tess4J,可以取得非常不错的起点。

  • 24
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吴名氏.

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值