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 处理,为你的项目增添更多实用的功能。

更多推荐