使用Tessract-OCR创建验证码识别服务--Web环境

                                                 关于使用Tess4J创建验证码图片识别服务

 

     Tessract-OCR

    使用Tess4J最好先了解一下Tessract-OCR,Tessract-OCR是Google出的一款可以独立在Windows系统下运行的图片识别的软件,安装好Tessract后,配置好环境,即可在CMD命令行下使用。

    但是我们开发往往需要集成在代码中,比如这次我们是使用java代码来实现,这时候我们就要用到Tess4J了。

     使用Tess4J之前

    使用Tess4J之前最好看看官方文档的描述,因为现在网上许多关于Tess4J的使用的帖子是不完整的,甚至是错误的。

    Tesseract, Leptonica 32- and 64-bit DLLs, language data for English, and sample images are bundled with the program. Language data packs for Tesseract should be decompressed and placed into the tessdata folder. The Windows native libraries were built with VS2015 and therefore depend on the Visual C++ 2015 Redistributable Packages. Use Visual C++ 2017 Redistributable for Tess4J 4.x.  

                                                                                                                                                                                                                        --官方描述

    官方描述明确表示如果在Windows环境下使用,使用Tess4J 4.0以下的需要安装 Visual C++ 2015 Redistributable Packages,Tess4J 4.0以上的则需要安装 Visual C++ 2017 Redistributable。否则你会遇到TessAPI不能被初始化的异常。

    Tess4J在Web项目中的使用

    1.添加依赖

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

很简单,只有一个依赖

  2.安装Visual C++ 2015 Redistributable Packages

  我这里使用的Tess4J 3.1.0,所以需要安装Visual C++ 2015 Redistributable Packages

  3.编写程序

File imageFile = new File(picPath);
System.out.println("获得的图片路径:" + picPath);
Tesseract instance = new Tesseract();
instance.setDatapath("E:/tessdata");
instance.setLanguage("fontyp");
BufferedImage bi = ImageIO.read(imageFile);
// 灰度化
BufferedImage identifyImage = ImageHelper
        .convertImageToGrayscale(ImageHelper.getSubImage(bi, 0, 0, bi.getWidth(), bi.getHeight()));
// 二值化
identifyImage = ImageHelper.convertImageToBinary(identifyImage);
// 图片放大
identifyImage = ImageHelper.getScaledInstance(identifyImage, identifyImage.getWidth() * 4,
        identifyImage.getHeight() * 4);
String result = instance.doOCR(identifyImage);

注意:在Web项目中需要setDatapath,指定训练库目录,而且只能用绝对路径。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值