前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring和Ocr整合详解

Spring和Ocr整合详解

作者头像
品茗IT
发布2019-09-12 09:49:37
1.3K0
发布2019-09-12 09:49:37
举报
文章被收录于专栏:品茗IT品茗IT

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的识别度一般。然而开源易用。

**如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以<a

href="https://jq.qq.com/?_wv=1027&k=52sgH1J"

target="_blank">

加入我们的java学习圈,点击即可加入

</a>

,共同学习,节约学习时间,减少很多在学习中遇到的难题。**

开始搭建

依赖Jar包
代码语言:javascript
复制
<dependency>
	<dependency>
    <groupId>net.sourceforge.tess4j</groupId>
    <artifactId>tess4j</artifactId>
    <version>4.0.1</version>
</dependency>
Spring-ocr配置

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

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
tessdata.path=/tessdata
tessdata.language=eng

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

调用的service

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

TesseractService:

代码语言:javascript
复制
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组件化构建

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-05-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Spring和Ocr整合详解
    • 官方主页
      • 概述
        • 开始搭建
          • 依赖Jar包
          • Spring-ocr配置
          • 调用的service
        • 快速构建项目
        相关产品与服务
        文字识别
        文字识别(Optical Character Recognition,OCR)基于腾讯优图实验室的深度学习技术,将图片上的文字内容,智能识别成为可编辑的文本。OCR 支持身份证、名片等卡证类和票据类的印刷体识别,也支持运单等手写体识别,支持提供定制化服务,可以有效地代替人工录入信息。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档