【神技解锁】Spring Boot + Tess4J:一图胜千言,瞬间变文字,颠覆你的视觉体验!

简介: 【8月更文挑战第29天】本文详细介绍了如何在 Spring Boot 项目中集成 Tess4J,实现高效本地与远程图片的光学字符识别(OCR)处理。通过具体步骤展示了如何添加依赖、配置 OCR 引擎、创建图片处理服务及控制器,并提供了测试方法。这不仅适用于文本识别场景,还可扩展至其他图像处理任务,为项目增添实用功能。

Spring Boot 结合 Tess4J 可以实现高效的本地与远程图片处理功能。Tess4J 是一个 Java 封装的 Tesseract OCR 引擎,它提供了方便的接口来进行光学字符识别(OCR)。本文将通过一个具体的技术博客的形式,详细介绍如何在 Spring Boot 项目中集成 Tess4J,并实现对本地及远程图片的OCR处理。

首先,我们需要在项目中引入必要的依赖。对于 Spring Boot 项目,我们可以在 pom.xml 文件中添加以下依赖项:

<dependencies>
    <!-- Spring Boot Starter Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Tess4J 依赖 -->
    <dependency>
        <groupId>net.sourceforge.tess4j</groupId>
        <artifactId>tess4j</artifactId>
        <version>4.5.6</version>
    </dependency>

    <!-- Commons IO 用于处理文件流 -->
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.11.0</version>
    </dependency>

    <!-- Optional: 使用 Spring Boot DevTools 加快开发迭代 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <!-- Test Dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

接下来,我们需要准备 Tesseract OCR 引擎所需的语言包。可以从 Tesseract OCR 官网 下载对应的语言包,然后将其放置在一个合适的位置,比如项目的 resources 目录下。

配置 OCR 引擎

在 Spring Boot 中,我们可以通过创建一个配置类来初始化 Tesseract OCR 引擎:

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
public class OcrConfig {
   

    @Bean
    public Tesseract tesseract() {
   
        Tesseract instance = new Tesseract();
        instance.setLanguage("eng"); // 设置语言包,这里使用英语
        instance.setDatapath("src/main/resources/tessdata"); // 设置语言包路径
        return instance;
    }
}

图片处理服务

接下来,我们创建一个服务类来处理图片:

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Service
public class ImageOcrService {
   

    private final Tesseract tesseract;
    private final ResourceLoader resourceLoader;

    @Autowired
    public ImageOcrService(Tesseract tesseract, ResourceLoader resourceLoader) {
   
        this.tesseract = tesseract;
        this.resourceLoader = resourceLoader;
    }

    /**
     * 从本地文件路径读取图片并进行 OCR 处理
     * @param imagePath 图片文件路径
     * @return OCR 结果文本
     */
    public String ocrLocalImage(String imagePath) throws IOException, TesseractException {
   
        Resource imageResource = resourceLoader.getResource(imagePath);
        BufferedImage image = ImageIO.read(imageResource.getInputStream());
        return tesseract.doOCR(image);
    }

    /**
     * 从远程 URL 获取图片并进行 OCR 处理
     * @param imageUrl 图片 URL
     * @return OCR 结果文本
     */
    public String ocrRemoteImage(String imageUrl) throws IOException, TesseractException {
   
        byte[] imageData = IOUtils.toByteArray(new ByteArrayInputStream(IOUtils.toByteArray(imageUrl)));
        BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageData));
        return tesseract.doOCR(image);
    }
}

控制器

最后,我们需要创建一个控制器来接收 HTTP 请求,并调用服务层的方法来处理图片:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

@RestController
public class OcrController {
   

    private final ImageOcrService imageOcrService;

    @Autowired
    public OcrController(ImageOcrService imageOcrService) {
   
        this.imageOcrService = imageOcrService;
    }

    @GetMapping("/ocr/local")
    public ResponseEntity<String> ocrLocalImage(@RequestParam("path") String imagePath) throws IOException, TesseractException {
   
        String result = imageOcrService.ocrLocalImage(imagePath);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/ocr/remote")
    public ResponseEntity<String> ocrRemoteImage(@RequestParam("url") String imageUrl) throws IOException, TesseractException {
   
        String result = imageOcrService.ocrRemoteImage(imageUrl);
        return ResponseEntity.ok(result);
    }
}

测试

为了测试我们的服务是否正常工作,我们可以运行 Spring Boot 应用,并使用 Postman 或类似的工具发送 GET 请求到 /ocr/local/ocr/remote 端点。例如:

  • 本地图片处理:http://localhost:8080/ocr/local?path=file:/path/to/image.jpg
  • 远程图片处理:http://localhost:8080/ocr/remote?url=https://example.com/path/to/image.jpg

总结

通过以上步骤,我们成功地在 Spring Boot 项目中集成了 Tess4J,并实现了本地和远程图片的 OCR 处理功能。这种方法不仅可以应用于文本识别场景,还可以扩展到其他图像处理任务中。希望本文能够帮助你更好地理解如何在 Spring Boot 中使用 Tess4J 进行 OCR 处理,为你的项目增添更多实用的功能。

相关文章
|
文字识别 Java API
SpringBoot+Tess4j实现牛逼的OCR识别工具
SpringBoot+Tess4j实现牛逼的OCR识别工具
1603 0
SpringBoot+Tess4j实现牛逼的OCR识别工具
|
22天前
|
机器学习/深度学习 文字识别 前端开发
基于 Spring Boot 3.3 + OCR 实现图片转文字功能
【8月更文挑战第30天】在当今数字化信息时代,图像中的文字信息越来越重要。无论是文档扫描、名片识别,还是车辆牌照识别,OCR(Optical Character Recognition,光学字符识别)技术都发挥着关键作用。本文将围绕如何使用Spring Boot 3.3结合OCR技术,实现图片转文字的功能,分享工作学习中的技术干货。
58 2
|
2月前
|
机器学习/深度学习 人工智能 文字识别
文本,文字扫描01,OCR文本识别技术展示,一个安卓App,一个简单的设计,文字识别可以应用于人工智能,机器学习,车牌识别,身份证识别,银行卡识别,PaddleOCR+SpringBoot+Andr
文本,文字扫描01,OCR文本识别技术展示,一个安卓App,一个简单的设计,文字识别可以应用于人工智能,机器学习,车牌识别,身份证识别,银行卡识别,PaddleOCR+SpringBoot+Andr
|
2月前
|
文字识别 Java
文本,文字识别07,SpringBoot服务开发-入参和返回值,编写接口的时候,要注意识别的文字返回的是多行,因此必须是List集合,Bean层,及实体类的搭建
文本,文字识别07,SpringBoot服务开发-入参和返回值,编写接口的时候,要注意识别的文字返回的是多行,因此必须是List集合,Bean层,及实体类的搭建
|
4月前
|
XML 文字识别 Java
SpringBoot + Tess4J 实现本地与远程图片处理
【4月更文挑战第30天】Spring Boot 是一个流行的 Java 框架,可以方便地搭建各种类型的应用。Tess4J 是一个基于 Tesseract OCR 的 Java 接口库,用于识别图像中的文本。本文将介绍如何结合这两个工具,创建一个应用程序,能够处理本地和远程图像,提取其中的文本。
149 1
|
Java Spring 容器
SpringBoot整合JavaMail 可以发送单纯的文字邮件 也可发送有html文件或者发送压缩包多文件邮件
SpringBoot整合JavaMail 可以发送单纯的文字邮件 也可发送有html文件或者发送压缩包多文件邮件
220 0
SpringBoot整合JavaMail 可以发送单纯的文字邮件 也可发送有html文件或者发送压缩包多文件邮件
|
存储 NoSQL 算法
SpringBoot+Redis 搞定搜索栏热搜、不雅文字过滤功能
首先配置好redis数据源等等基础 代码实现过滤不雅文字功能
|
存储 NoSQL 算法
SpringBoot+Redis 搜索栏热搜、不雅文字过滤功能
SpringBoot+Redis 搜索栏热搜、不雅文字过滤功能
211 0
|
Java
springboot jar 方式获取 资源图片 文字等
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40254498/article/details/79722217 InputStream stream = getClass().
1279 0
|
30天前
|
缓存 Java Maven
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决
Java本地高性能缓存实践问题之SpringBoot中引入Caffeine作为缓存库的问题如何解决