Spring之路

Spring和Ocr整合详解

2019-04-03  本文已影响1人  逍遥天扬

Spring和Ocr整合详解

官方主页

Spring

Ocr tess4j

概述

Tess4J是对Tesseract OCR API.的Java JNA 封装。使java能够通过调用Tess4J的API来使用Tesseract OCR。支持的格式:TIFF,JPEG,GIF,PNG,BMP,JPEG,and PDF.

这里说整合Spring和Ocr有点勉强,因为Tess4J是脱离spring环境运行的。不过为方便适配到spring环境,这里就强行把它俩弄一块儿了。

tess4j的识别度一般。然而开源易用。

开始搭建

依赖Jar包

<dependency>
    <dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.0.1</version>
</dependency>

Spring-ocr配置

在spring的xml中,按照spring的规范,定义tesseractService,方便调用。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
                     http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-4.0.xsd ">

    <bean id="annotationPropertyConfigurerOcr"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:ocr.properties</value>
            </list>
        </property>
    </bean>

    <bean id="tesseractService" class="com.cff.springwork.ocr.service.TesseractService">
        <property name="tessdataPath" value="${tessdata.path}" />
        <property name="tessdataLang" value="${tessdata.language}" />
    </bean>
    
</beans>

这里的xml文件引入配置文件。

ocr.properties:

tessdata.path=/tessdata
tessdata.language=eng

tessdata.path指定了训练数据的路径,训练库比较大,https://github.com/tesseract-ocr/tessdata这里可以下载

调用的service

我们可以编写一个完整的service,方便以后使用。
TesseractService:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.ImageHelper;

public class TesseractService {
    private String tessdataPath;
    private String tessdataLang;

    public String ocr(String filePath) {
        try {
            File imageFile = new File(filePath);
            Tesseract instance = new Tesseract();
            // 使用classpath目录下的训练库
            String path = tessdataPath;
            instance.setLanguage(tessdataLang);// 英文库识别数字比较准确

            instance.setDatapath(path);
            String result = instance.doOCR(imageFile);
            return result;
        } catch (TesseractException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static BufferedImage change(File file) {

        // 读取图片字节数组
        BufferedImage textImage = null;
        try {
            InputStream in = new FileInputStream(file);
            BufferedImage image = ImageIO.read(in);
            textImage = ImageHelper
                    .convertImageToGrayscale(ImageHelper.getSubImage(image, 0, 0, image.getWidth(), image.getHeight())); // 对图片进行处理
            textImage = ImageHelper.getScaledInstance(image, image.getWidth() * 5, image.getHeight() * 5); // 将图片扩大5倍

        } catch (IOException e) {
            e.printStackTrace();
        }

        return textImage;
    }
}

快速构建项目

Spring组件化构建

上一篇 下一篇

猜你喜欢

热点阅读