Spring Boot 集成 tess4j 实现图片辨认文本

打印 上一主题 下一主题

主题 1720|帖子 1720|积分 5170

tesseract是一个开源的光学字符辨认(OCR)引擎,它可以将图像中的文字转换为计算机可读的文本。支持多种语言和书面语言,并且可以在命令行中实行。它是一个流行的开源OCR工具,可以在许多不同的操作体系上运行。
Tess4J是一个基于Tesseract OCR引擎的Java接口,可以用来辨认图像中的文本,说白了,就是封装了它的API,让Java可以直接调用。

目次
一、安装 tesseract (OCR)
二、下载训练数据
三、创建spring boot 项目
  1、导入依靠
2、编写设置类
3、编写controller
5、编写service
5、运行调试


一、安装 tesseract (OCR)

  Index of /tesseract 找到符合本身电脑的安装

二、下载训练数据

https://raw.gitcode.com/gh_mirrors/te/tessdata/blobs/eeb66cfbd9c02b170a6aeeece673910793d8d8c4/chi_sim.traineddata
三、创建spring boot 项目

  1、导入依靠

  1.         <dependency>
  2.             <groupId>net.sourceforge.tess4j</groupId>
  3.             <artifactId>tess4j</artifactId>
  4.             <version>5.3.0</version>
  5.         </dependency>
复制代码
2、编写设置类

  1. package com.example.config;
  2. import net.sourceforge.tess4j.Tesseract;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration
  7. public class TesseractOcrConfig {
  8.     @Value("${tess4j.datapath}")
  9.     private String dataPath;
  10.     @Bean
  11.     public Tesseract tesseract() {
  12.         Tesseract tesseract = new Tesseract();
  13.         // 设置训练数据文件夹路径
  14.         tesseract.setDatapath(dataPath);
  15.         // 设置为中文简体
  16.         tesseract.setLanguage("chi_sim");
  17.         return tesseract;
  18.     }
  19. }
复制代码
3、编写controller

  1. package com.example.controller;
  2. import com.example.service.OcrService;
  3. import lombok.extern.slf4j.Slf4j;
  4. import net.sourceforge.tess4j.TesseractException;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.web.multipart.MultipartFile;
  10. import java.io.IOException;
  11. @RestController
  12. @RequestMapping("/api")
  13. @Slf4j
  14. public class OcrController {
  15.     private final OcrService ocrService;
  16.     public OcrController(OcrService ocrService) {
  17.         this.ocrService = ocrService;
  18.     }
  19.     @PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  20.     public String recognizeImage(MultipartFile file) throws TesseractException, IOException {
  21.         log.info(ocrService.recognizeText(file));
  22.         // 调用OcrService中的方法进行文字识别
  23.         return ocrService.recognizeText(file);
  24.     }
  25. }
复制代码
5、编写service

  1. package com.example.service.impl;
  2. import com.example.service.OcrService;
  3. import net.sourceforge.tess4j.Tesseract;
  4. import net.sourceforge.tess4j.TesseractException;
  5. import org.springframework.stereotype.Service;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import javax.imageio.ImageIO;
  8. import java.awt.image.BufferedImage;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. @Service
  13. public class OcrServiceImpl implements OcrService {
  14.     private final Tesseract tesseract;
  15.     public OcrServiceImpl(Tesseract tesseract) {
  16.         this.tesseract = tesseract;
  17.     }
  18.     /**
  19.      *
  20.      * @param imageFile 要识别的图片
  21.      * @return
  22.      */
  23.     @Override
  24.     public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException {
  25.         // 转换
  26.         InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());
  27.         BufferedImage bufferedImage = ImageIO.read(sbs);
  28.         // 对图片进行文字识别
  29.         return tesseract.doOCR(bufferedImage);
  30.     }
  31. }
复制代码
接口
  1. package com.example.service;
  2. import net.sourceforge.tess4j.TesseractException;
  3. import org.springframework.web.multipart.MultipartFile;
  4. import java.io.IOException;
  5. public interface OcrService {
  6.     public String recognizeText(MultipartFile imageFile) throws IOException, TesseractException;
  7. }
复制代码
5、运行调试


可以看到辨认率还是很棒的

注:图片颜色比力多的时间有有点辨认不清楚了以及一些带字体的文本 毕竟是免费的

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

泉缘泉

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表