Java验证码读取

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class ImgDecode{  
    public static byte[] readInputStream(InputStream in) throws Exception{  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        byte[] buffer = new byte[1024];  
        int len = 0;  
        while( (len=in.read(buffer)) != -1 ){  
            out.write(buffer, 0, len);  
        }  
        in.close();  
        return out.toByteArray();  
    } 
    
    public static void decodeImage(String imgpath){
    String path = imgpath.substring(0,imgpath.lastIndexOf("\\"));
    System.out.println("path="+path);
   
    String cmd = "cmd /c start .\\decode.bat "+"\""+imgpath+"\"";
    try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();

    }
    
    public static void main(String[] args) throws Exception {  
    URL url = new URL("http://url/checkcode/picCheckCode");
        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
        conn.setRequestMethod("GET");  
        conn.setConnectTimeout(5 * 1000);  
        InputStream in = conn.getInputStream();  
        byte[] data = readInputStream(in);  
      
        File imageFile = new File("abc.png");  
        FileOutputStream out = new FileOutputStream(imageFile);  
        out.write(data);  
        out.close();  
        System.out.println("imgpath="+imageFile.getAbsolutePath());
       decodeImage(imageFile.getAbsolutePath());
   
       try{
      Thread.sleep(1000);
       }catch(Exception e){
      e.printStackTrace();
       }
       InputStream in1 = new FileInputStream(new File(".\\1.txt"));
       byte[] b = new byte[1024];
       int len = -1;
       while((len=in1.read(b))!=-1){
      in1.read(b);
       }
       if(in!=null){
      try{
      in1.close();
      }catch(Exception e){
      e.printStackTrace();
      }
       }
       String out1 = new String(b);
       System.out.println(out1);
    }
}  

decode.bat:

@echo off
tesseract.exe %1 1 -1
)
exit

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值