前言

什么是Tess4j库

案例

1、引入依赖
<!-- tess4j -->
<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.5.4</version>
</dependency>
2、yml配置

这里,我特地把训练数据的目录路径配置在yml里,后续可以扩展到配置中心。

server:
  port: 8888

# 训练数据文件夹的路径
tess4j:
  datapath: D:/tessdata

Java也能做OCR!SpringBoot 整合 Tess4J 实现图片文字识别-LMLPHP

3、config配置类
package com.example.tesseractocr.config;

import net.sourceforge.tess4j.Tesseract;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @作者: 公众号【Java分享客栈】
 * @日期: 2023/10/12 22:58
 * @描述:
 */
@Configuration
public class TesseractOcrConfiguration {

   @Value("${tess4j.datapath}")
   private String dataPath;

   @Bean
   public Tesseract tesseract() {

      Tesseract tesseract = new Tesseract();
      // 设置训练数据文件夹路径
      tesseract.setDatapath(dataPath);
      // 设置为中文简体
      tesseract.setLanguage("chi_sim");
      return tesseract;
   }
}
4、service实现
package com.example.tesseractocr.service;

import lombok.AllArgsConstructor;
import net.sourceforge.tess4j.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

@Service
@AllArgsConstructor
public class OcrService {

    private final Tesseract tesseract;

   /**
    * 识别图片中的文字
    * @param imageFile 图片文件
    * @return 文字信息
    */
    public String recognizeText(MultipartFile imageFile) throws TesseractException, IOException {

        // 转换
        InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());
        BufferedImage bufferedImage = ImageIO.read(sbs);

        // 对图片进行文字识别
        return tesseract.doOCR(bufferedImage);
    }
}
5、新增rest接口
package com.example.tesseractocr.controller;

import com.example.tesseractocr.service.OcrService;
import lombok.AllArgsConstructor;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/api")
@RestController
@AllArgsConstructor
public class OcrController {
    private final OcrService ocrService;

    @PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String recognizeImage(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {

      // 调用OcrService中的方法进行文字识别
      return ocrService.recognizeText(file);
    }
}
6、测试效果

Java也能做OCR!SpringBoot 整合 Tess4J 实现图片文字识别-LMLPHP

Java也能做OCR!SpringBoot 整合 Tess4J 实现图片文字识别-LMLPHP

Java也能做OCR!SpringBoot 整合 Tess4J 实现图片文字识别-LMLPHP

Java也能做OCR!SpringBoot 整合 Tess4J 实现图片文字识别-LMLPHP

相关地址

总结


如果喜欢,请点赞+关注↓↓↓,持续分享干货哦!

10-16 22:03