首页 > 数据库 > 数据库开发技术 > Spring Boot集成Tess4J实现OCR
Tesseract是一个开源的光学字符识别(OCR)引擎,它可以将图像中的文字转换为计算机可读的文本。支持多种语言和书面语言,并且可以在命令行中执行。它是一个流行的开源OCR工具,可以在许多不同的操作系统上运行。
Tess4J是一个基于Tesseract OCR引擎的Java接口,可以用来识别图像中的文本,说白了,就是封装了它的API,让Java可以直接调用。
直接识别支持的文件
识别图片流
识别图片的某块区域
将识别结果保存为 TEXT/ HOCR/ PDF/ UNLV/ BOX
通过设置取词的等级,提取识别出来的文字
获得每一个识别区域的具体坐标范围
调整倾斜的图片
裁剪图片
调整图片分辨率
从粘贴板获得图像
克隆一个图像(目的:创建一份一模一样的图片,与原图在操作修改上,不相 互影响)
图片转换为二进制、黑白图像、灰度图像
反转图片颜色
Tesseract-ocr安装下载:https://digi.bib.uni-mannheim.de/tesseract/
Tesseract OCR库通过训练数据来学习不同语言和字体的特征,以便更好地识别图片中的文字。在安装Tesseract OCR库时,通常会生成一个包含多个子文件夹的训练数据文件夹,其中每个子文件夹都包含了特定语言或字体的训练数据。
tess4j:
datapath: D:/tmp
PS:这里我没有用官方Github文档中给的地址,因为太慢了,找了一个下载比较快的,你们可以往下拉找到win64位的安装即可
实现图片上的文字识别
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>Tess4j</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!-- tess4j --><dependency><groupId>net.sourceforge.tess4j</groupId><artifactId>tess4j</artifactId><version>4.5.4</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>
package com.et.tess4j.controller;import com.et.tess4j.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;import java.util.HashMap;import java.util.Map;@RestController@AllArgsConstructorpublic class HelloWorldController {@RequestMapping("/hello")public Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", "HelloWorld");return map;}private final OcrService ocrService;@PostMapping(value = "/recognize", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)public String recognizeImage(@RequestParam("file") MultipartFile file) throws TesseractException, IOException {return ocrService.recognizeText(file);}}
package com.et.tess4j.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@AllArgsConstructorpublic class OcrService {private final Tesseract tesseract;public String recognizeText(MultipartFile imageFile) throws TesseractException, IOException {InputStream sbs = new ByteArrayInputStream(imageFile.getBytes());BufferedImage bufferedImage = ImageIO.read(sbs);return tesseract.doOCR(bufferedImage);}}
config
package com.et.tess4j.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;public class TesseractOcrConfiguration {private String dataPath;public Tesseract tesseract() {Tesseract tesseract = new Tesseract();tesseract.setDatapath(dataPath);tesseract.setLanguage("chi_sim");return tesseract;}}
以上只是一些关键代码,所有代码请参见下面代码仓库
https://github.com/Harries/springboot-demo(tess4j)
启动Spring Boot应用
传入一张带文字的图片
可以看到返回识别后的结果

https://github.com/tesseract-ocr/tesseract
https://github.com/nguyenq/tess4j
原文作者:HBLOG;如有侵权,请联系管理员删除。