当前位置: 代码网 > it编程>编程语言>Java > SpringBoot集成Tess4J实现OCR的示例代码

SpringBoot集成Tess4J实现OCR的示例代码

2024年12月28日 Java 我要评论
1.什么是tess4j?tesseract是一个开源的光学字符识别(ocr)引擎,它可以将图像中的文字转换为计算机可读的文本。支持多种语言和书面语言,并且可以在命令行中执行。它是一个流行的开源ocr工

1.什么是tess4j?

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

tess4j api 提供的功能:

  • 直接识别支持的文件
  • 识别图片流
  • 识别图片的某块区域
  • 将识别结果保存为 text/ hocr/ pdf/ unlv/ box
  • 通过设置取词的等级,提取识别出来的文字
  • 获得每一个识别区域的具体坐标范围
  • 调整倾斜的图片
  • 裁剪图片
  • 调整图片分辨率
  • 从粘贴板获得图像
  • 克隆一个图像(目的:创建一份一模一样的图片,与原图在操作修改上,不相 互影响)
  • 图片转换为二进制、黑白图像、灰度图像
  • 反转图片颜色

2.环境准备

tesseract ocr库通过训练数据来学习不同语言和字体的特征,以便更好地识别图片中的文字。在安装tesseract ocr库时,通常会生成一个包含多个子文件夹的训练数据文件夹,其中每个子文件夹都包含了特定语言或字体的训练数据。

tess4j:
  datapath: d:/tmp

ps:这里我没有用官方github文档中给的地址,因为太慢了,找了一个下载比较快的,你们可以往下拉找到win64位的安装即可

3.代码工程

实验目的

实现图片上的文字识别

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;
   }
}

以上只是一些关键代码

4.测试

  • 启动spring boot应用
  • 传入一张带文字的图片
  • 可以看到返回识别后的结果

到此这篇关于springboot集成tess4j实现ocr的示例代码的文章就介绍到这了,更多相关springboot tess4j实现ocr内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com