java读取图片上的数字(验证码)

需要说一下,这里说的方法对简单的图片读数字的正确率是非常高的。但是对于复杂的如字母、甚至汉字,正确率就不敢保证了。对于那些图片背景十分混乱,肉眼都得仔细分辨的,为了您的宝贵时间,就不要往下看了。


需要引入的jar包:

<!--图片识别-->
        <dependency>
            <groupId>net.java.dev.jna</groupId>
            <artifactId>jna</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.tess4j</groupId>
            <artifactId>tess4j</artifactId>
            <version>2.0.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jna</groupId>
                    <artifactId>jna</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


代码:

import com.camel.common.utils.HttpClient;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.LoadLibs;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;


import java.io.*;


/**
 * 测试类
 */
public class TestOne {
public static void main(String[] args) throws Exception {
downloadJPG(HttpClient.post("验证码url", "time=" + System.currentTimeMillis()),"1.jpg");
String code = getImgContent("1.jpg");
System.out.println("验证码 = " + code);
}


protected static void downloadJPG(HttpResponse httpResponse,String fileName) throws IOException {
InputStream input = httpResponse.getEntity().getContent();
OutputStream output = new FileOutputStream(new File(fileName));
IOUtils.copy(input, output);
if (output != null) {
output.close();
}
output.flush();
}

protected static String getImgContent(String imgUrl) {
String content = "";
File imageFile = new File(imgUrl);
//读取图片数字
ITesseract instance = new Tesseract();


File tessDataFolder = LoadLibs.extractTessResources("tessdata");
instance.setLanguage("eng");//英文库识别数字比较准确
instance.setDatapath(tessDataFolder.getAbsolutePath());


try {
content = instance.doOCR(imageFile).replace("\n", "");
System.out.println(content);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
return content;
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值