SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例代碼
1.什么是Tess4j?
Tesseract
是一個(gè)開(kāi)源的光學(xué)字符識(shí)別(OCR)引擎,它可以將圖像中的文字轉(zhuǎn)換為計(jì)算機(jī)可讀的文本。支持多種語(yǔ)言和書(shū)面語(yǔ)言,并且可以在命令行中執(zhí)行。它是一個(gè)流行的開(kāi)源OCR工具,可以在許多不同的操作系統(tǒng)上運(yùn)行。Tess4J
是一個(gè)基于Tesseract OCR引擎
的Java接口,可以用來(lái)識(shí)別圖像中的文本,說(shuō)白了,就是封裝了它的API,讓Java可以直接調(diào)用。
Tess4J API 提供的功能:
- 直接識(shí)別支持的文件
- 識(shí)別圖片流
- 識(shí)別圖片的某塊區(qū)域
- 將識(shí)別結(jié)果保存為 TEXT/ HOCR/ PDF/ UNLV/ BOX
- 通過(guò)設(shè)置取詞的等級(jí),提取識(shí)別出來(lái)的文字
- 獲得每一個(gè)識(shí)別區(qū)域的具體坐標(biāo)范圍
- 調(diào)整傾斜的圖片
- 裁剪圖片
- 調(diào)整圖片分辨率
- 從粘貼板獲得圖像
- 克隆一個(gè)圖像(目的:創(chuàng)建一份一模一樣的圖片,與原圖在操作修改上,不相 互影響)
- 圖片轉(zhuǎn)換為二進(jìn)制、黑白圖像、灰度圖像
- 反轉(zhuǎn)圖片顏色
2.環(huán)境準(zhǔn)備
- Tesseract-ocr安裝下載:Index of /tesseract
Tesseract OCR庫(kù)通過(guò)訓(xùn)練數(shù)據(jù)來(lái)學(xué)習(xí)不同語(yǔ)言和字體的特征,以便更好地識(shí)別圖片中的文字。在安裝Tesseract OCR庫(kù)時(shí),通常會(huì)生成一個(gè)包含多個(gè)子文件夾的訓(xùn)練數(shù)據(jù)文件夾,其中每個(gè)子文件夾都包含了特定語(yǔ)言或字體的訓(xùn)練數(shù)據(jù)。
tess4j: datapath: D:/tmp
PS:這里我沒(méi)有用官方Github文檔中給的地址,因?yàn)樘?,找了一個(gè)下載比較快的,你們可以往下拉找到win64位的安裝即可
3.代碼工程
實(shí)驗(yàn)?zāi)康?/h3>
實(shí)現(xiàn)圖片上的文字識(shí)別
實(shí)現(xiàn)圖片上的文字識(shí)別
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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>
controller
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 @AllArgsConstructor public 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); } }
service
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 @AllArgsConstructor public 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; @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; } }
以上只是一些關(guān)鍵代碼
4.測(cè)試
- 啟動(dòng)Spring Boot應(yīng)用
- 傳入一張帶文字的圖片
- 可以看到返回識(shí)別后的結(jié)果
到此這篇關(guān)于SpringBoot集成Tess4J實(shí)現(xiàn)OCR的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot Tess4J實(shí)現(xiàn)OCR內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springMVC前臺(tái)傳數(shù)組類型,后臺(tái)用list類型接收實(shí)例代碼
這篇文章主要介紹了springMVC前臺(tái)傳數(shù)組類型,后臺(tái)用list類型接收實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12spring實(shí)現(xiàn)動(dòng)態(tài)切換、添加數(shù)據(jù)源及源碼分析
這篇文章主要給大家介紹了關(guān)于spring實(shí)現(xiàn)動(dòng)態(tài)切換、添加數(shù)據(jù)源及源碼分析的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Java正則表達(dá)式匹配字符串并提取中間值的方法實(shí)例
正則表達(dá)式常用于字符串處理、表單驗(yàn)證等場(chǎng)合,實(shí)用高效,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式匹配字符串并提取中間值的相關(guān)資料,需要的朋友可以參考下2022-06-06SpringBoot集成Redisson實(shí)現(xiàn)消息隊(duì)列的示例代碼
本文介紹了如何在SpringBoot中通過(guò)集成Redisson來(lái)實(shí)現(xiàn)消息隊(duì)列的功能,包括RedisQueue、RedisQueueInit、RedisQueueListener、RedisQueueService等相關(guān)組件的實(shí)現(xiàn)和測(cè)試,感興趣的可以了解一下2024-10-10通俗易懂學(xué)習(xí)java并發(fā)工具類-Semaphore,Exchanger
這篇文章主要介紹了java并發(fā)工具類-Semaphore,Exchanger,java并發(fā)工具類有很多,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,下面小編帶大家來(lái)一起學(xué)習(xí)一下吧2019-06-06java中關(guān)于移位運(yùn)算符的demo與總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇java中關(guān)于移位運(yùn)算符的demo與總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05Spring實(shí)現(xiàn)默認(rèn)標(biāo)簽解析流程
這篇文章主要為大家詳細(xì)介紹了Spring實(shí)現(xiàn)默認(rèn)標(biāo)簽解析流程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式
這篇文章主要介紹了SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10java鎖synchronized面試常問(wèn)總結(jié)
這篇文章主要介紹了java鎖synchronized面試常問(wèn)總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12