亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot集成Tess4J實現(xiàn)OCR的示例代碼

 更新時間:2024年12月27日 11:02:17   作者:HBLOG  
Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用,本文給大家介紹了SpringBoot集成Tess4J實現(xiàn)OCR的示例,需要的朋友可以參考下

1.什么是Tess4j?

  • Tesseract是一個開源的光學(xué)字符識別(OCR)引擎,它可以將圖像中的文字轉(zhuǎn)換為計算機(jī)可讀的文本。支持多種語言和書面語言,并且可以在命令行中執(zhí)行。它是一個流行的開源OCR工具,可以在許多不同的操作系統(tǒng)上運行。
  • Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調(diào)用。

Tess4J API 提供的功能:

  • 直接識別支持的文件
  • 識別圖片流
  • 識別圖片的某塊區(qū)域
  • 將識別結(jié)果保存為 TEXT/ HOCR/ PDF/ UNLV/ BOX
  • 通過設(shè)置取詞的等級,提取識別出來的文字
  • 獲得每一個識別區(qū)域的具體坐標(biāo)范圍
  • 調(diào)整傾斜的圖片
  • 裁剪圖片
  • 調(diào)整圖片分辨率
  • 從粘貼板獲得圖像
  • 克隆一個圖像(目的:創(chuàng)建一份一模一樣的圖片,與原圖在操作修改上,不相 互影響)
  • 圖片轉(zhuǎn)換為二進(jìn)制、黑白圖像、灰度圖像
  • 反轉(zhuǎn)圖片顏色

2.環(huán)境準(zhǔn)備

Tesseract OCR庫通過訓(xùn)練數(shù)據(jù)來學(xué)習(xí)不同語言和字體的特征,以便更好地識別圖片中的文字。在安裝Tesseract OCR庫時,通常會生成一個包含多個子文件夾的訓(xùn)練數(shù)據(jù)文件夾,其中每個子文件夾都包含了特定語言或字體的訓(xùn)練數(shù)據(jù)。

tess4j:
  datapath: D:/tmp

PS:這里我沒有用官方Github文檔中給的地址,因為太慢了,找了一個下載比較快的,你們可以往下拉找到win64位的安裝即可

3.代碼工程

實驗?zāi)康?/h3>

實現(xiàn)圖片上的文字識別

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.測試

  • 啟動Spring Boot應(yīng)用
  • 傳入一張帶文字的圖片
  • 可以看到返回識別后的結(jié)果

到此這篇關(guān)于SpringBoot集成Tess4J實現(xiàn)OCR的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot Tess4J實現(xiàn)OCR內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談一下JVM垃圾回收算法

    淺談一下JVM垃圾回收算法

    這篇文章主要介紹了一下JVM垃圾回收算法,Java有著自己一套的內(nèi)存管理機(jī)制,不需要開發(fā)者去手動釋放內(nèi)存,開發(fā)者只需要寫好代碼即可,運行過程中產(chǎn)生的垃圾都由JVM回收,需要的朋友可以參考下
    2023-04-04
  • springMVC前臺傳數(shù)組類型,后臺用list類型接收實例代碼

    springMVC前臺傳數(shù)組類型,后臺用list類型接收實例代碼

    這篇文章主要介紹了springMVC前臺傳數(shù)組類型,后臺用list類型接收實例代碼,具有一定借鑒價值,需要的朋友可以參考下。
    2017-12-12
  • spring實現(xiàn)動態(tài)切換、添加數(shù)據(jù)源及源碼分析

    spring實現(xiàn)動態(tài)切換、添加數(shù)據(jù)源及源碼分析

    這篇文章主要給大家介紹了關(guān)于spring實現(xiàn)動態(tài)切換、添加數(shù)據(jù)源及源碼分析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Java正則表達(dá)式匹配字符串并提取中間值的方法實例

    Java正則表達(dá)式匹配字符串并提取中間值的方法實例

    正則表達(dá)式常用于字符串處理、表單驗證等場合,實用高效,下面這篇文章主要給大家介紹了關(guān)于Java正則表達(dá)式匹配字符串并提取中間值的相關(guān)資料,需要的朋友可以參考下
    2022-06-06
  • SpringBoot集成Redisson實現(xiàn)消息隊列的示例代碼

    SpringBoot集成Redisson實現(xiàn)消息隊列的示例代碼

    本文介紹了如何在SpringBoot中通過集成Redisson來實現(xiàn)消息隊列的功能,包括RedisQueue、RedisQueueInit、RedisQueueListener、RedisQueueService等相關(guān)組件的實現(xiàn)和測試,感興趣的可以了解一下
    2024-10-10
  • 通俗易懂學(xué)習(xí)java并發(fā)工具類-Semaphore,Exchanger

    通俗易懂學(xué)習(xí)java并發(fā)工具類-Semaphore,Exchanger

    這篇文章主要介紹了java并發(fā)工具類-Semaphore,Exchanger,java并發(fā)工具類有很多,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,下面小編帶大家來一起學(xué)習(xí)一下吧
    2019-06-06
  • java中關(guān)于移位運算符的demo與總結(jié)(推薦)

    java中關(guān)于移位運算符的demo與總結(jié)(推薦)

    下面小編就為大家?guī)硪黄猨ava中關(guān)于移位運算符的demo與總結(jié)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Spring實現(xiàn)默認(rèn)標(biāo)簽解析流程

    Spring實現(xiàn)默認(rèn)標(biāo)簽解析流程

    這篇文章主要為大家詳細(xì)介紹了Spring實現(xiàn)默認(rèn)標(biāo)簽解析流程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式

    SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式

    這篇文章主要介紹了SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • java鎖synchronized面試常問總結(jié)

    java鎖synchronized面試常問總結(jié)

    這篇文章主要介紹了java鎖synchronized面試常問總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-12-12

最新評論