Sun
是选择你爱的人还是爱你的人?
posts - 8,comments - 14,trackbacks - 0
以前公司业务人员手动操作,一天也只能获取1000来条数据,通过程序获得了非常大的改观。程序目的是自动验证登录并查询获取相关信息,对验证码上每个字显示的宽度有要求,需要事先预设好,因为需要将验证码图片上的字分别裁剪单独分析。当然一般一个网站的验证码宽度都相对固定。




  1package com.xiaohang.readcode;
  2
  3import java.awt.*;
  4import java.awt.image.BufferedImage;
  5import java.io.*;
  6import java.net.URL;
  7import javax.imageio.*;
  8
  9public class ImageCode {
 10    BufferedImage imd; // 待识别图像
 11
 12    private int iw, ih; // 图像宽和高
 13
 14    //public final static String path = "E:\\codeimages\\1.bmp";
 15
 16    public  static void main(String []args){
 17        
 18        try{
 19            
 20            ImageCode app = new ImageCode();
 21            
 22            //String s = app.getImageNum("http://*****.cjn.cn/gcode.act");
 23            String s = app.getImageNum("E:\\codeimages\\1.bmp");
 24            System.out.println("result is : " + s);
 25            
 26        }
catch(Exception ex)
 27        {
 28            ex.printStackTrace();
 29        }

 30    }

 31    
 32    public String getImageNum(String file) {
 33
 34        StringBuffer sb = new StringBuffer("");
 35        try {
 36        //    imd = ImageIO.read(new URL(file)); // 用ImageIO的静态方法读取图像
 37            imd = ImageIO.read(new File(file));
 38            BufferedImage newim[] = new BufferedImage[4];
 39            int[] x = new int[4];
 40            // 将图像分成4块,因为要处理的文件都有四个数字
 41            newim[0= imd.getSubimage(501015);
 42            newim[1= imd.getSubimage(1801015);
 43            newim[2= imd.getSubimage(3101015);
 44            newim[3= imd.getSubimage(4401015);
 45        
 46                 for (int k = 0; k < 4; k++{
 47                x[k] = 0;
 48            //    ImageIO.write(newim[k], "JPEG", new File("E:\\codeimages\\"+k+".jpeg"));
 49                this.iw = newim[k].getWidth(null);
 50                this.ih = newim[k].getHeight(null);
 51                int[] pix = new int[iw * ih];
 52                // 二进制图像,将像数读取出来,转换成0、1图像数组
 53            
 54                for (int i = 0; i < ih; i++{
 55                    for (int j = 0; j < iw; j++{
 56                        pix[i * (iw) + j] = newim[k].getRGB(j, i);
 57                        String code = String.valueOf(pix[i * (iw) + j]);
 58                        if (code.length() >= 8)
 59                            pix[i * (iw) + j] = 1;
 60                        else
 61                            pix[i * (iw) + j] = 0;
 62
 63                        x[k] = x[k] + pix[i * (iw) + j];
 64                    }

 65                }

 66
 67                // 得到匹配的数字
 68                int r = this.getMatchNum(pix);
 69                sb.append(r);
 70            //    System.out.println("x=" + x[k]);
 71            }

 72
 73        }
 catch (Exception ex) {
 74            ex.printStackTrace();
 75        }

 76        return sb.toString();
 77    }

 78    
 79    static int[][] value = {
 80             // 0
 81                   {
 82                    0000000000,
 83                    0000000000,
 84                    0000110000,
 85                    0001111000,
 86                    0011001100,
 87                    0011001100,
 88                    0011001100,
 89                    0011001100,
 90                    0011001100,
 91                    0011001100,
 92                    0011001100,
 93                    0001111000,
 94                    0000110000,
 95                    0000000000,
 96                    0000000000
 97                    }
,
 98            // 1
 99                  
100                    0000000000,
101                    0000000000,
102                    0000111000,
103                    0000111000,
104                    0000011000,
105                    0000011000,
106                    0000011000,
107                    0000011000,
108                    0000011000,
109                    0000011000,
110                    0000011000,
111                    0000011000,
112                    0001111100,
113                    0001111100,
114                    0000000000
115                    }
,
116            // 2
117                    
118                        0000000000,
119                        0000000000,
120                        0001110000,
121                        0011111000,
122                        0011001100,
123                        0000001100,
124                        0000001100,
125                        0000011000,
126                        0000110000,
127                        0001100000,
128                        0011000000,
129                        0011111100,
130                        0111111100,
131                        0000000000,
132                        0000000000
133                    }
,
134                    
135                    
136            // 3
137                    
138                        0000000000,
139                        0000000000,
140                        0001111000,
141                        0011111100,
142                        0010001100,
143                        0000011000,
144                        0000110000,
145                        0001111000,
146                        0000111100,
147                        0000001100,
148                        0000001100,
149                        0011111000,
150                        0011110000,
151                        0000000000,
152                        0000000000
153                    }
,
154            // 4
155                    
156                        0000000000,
157                        0000000000,
158                        0000001000,
159                        0000011000,
160                        0000111000,
161                        0000111000,
162                        0001111000,
163                        0011011000,
164                        0111111100,
165                        0111111100,
166                        0000011000,
167                        0000011000,
168                        0000011000,
169                        0000000000,
170                        0000000000
171                    }
,
172            // 5
173                    
174                        0000000000,
175                        0000000000,
176                        0001111100,
177                        0001111100,
178                        0001100000,
179                        0011110000,
180                        0010111000,
181                        0000001100,
182                        0000001100,
183                        0000001100,
184                        0000001100,
185                        0011111000,
186                        0011110000,
187                        0000000000,
188                        0000000000
189                    }
,
190            // 6
191                    
192                        0000000000,
193                        0000000000,
194                        0001111100,
195                        0011111100,
196                        0011000000,
197                        0011000000,
198                        0011110000,
199                        0011111000,
200                        0011001100,
201                        0011001100,
202                        0011001100,
203                        0001111000,
204                        0000110000,
205                        0000000000,
206                        0000000000
207                    }
,
208            // 7
209                    
210                        0000000000,
211                        0000000000,
212                        0011111100,
213                        0111111100,
214                        0100001100,
215                        0000011000,
216                        0000011000,
217                        0000011000,
218                        0000110000,
219                        0000110000,
220                        0000110000,
221                        0001100000,
222                        0001100000,
223                        0001100000,
224                        0000000000
225                    }
,
226            // 8
227                    
228                        0000000000,
229                        0000000000,
230                        0001111000,
231                        0011111100,
232                        0011001100,
233                        0011001100,
234                        0001111000,
235                        0000110000,
236                        0001111000,
237                        0011001100,
238                        0011001100,
239                        0011111100,
240                        0001111000,
241                        0000000000,
242                        0000000000
243                        }
,
244            // 9
245                        
246                            0000000000,
247                            0000000000,
248                            0000110000,
249                            0001001000,
250                            0010000100,
251                            0010000100,
252                            0010000100,
253                            0001001100,
254                            0000110100,
255                            0000001000,
256                            0000001000,
257                            0000110000,
258                            0011000000,
259                            0000000000,
260                            0000000000
261                            }
 }
;
262    
263    // 图像像素相减去最小绝对值得到最接近结果
264    public int getMatchNum(int[] pix){
265        
266        int result = -1;
267        int temp = 100;
268        int x;
269        for(int k = 0 ; k < 10 ; k++){
270            
271            x = 0 ; 
272            for(int i = 0 ; i < 150 ; i++){   // lenght=120
273                x = x+Math.abs(pix[i] - value[k][i]);    // 取绝对值            
274            }

275            
276        if(x < temp){
277            temp = x;
278            result = k;
279        }

280        }

281        return result;
282    }

283    
284
285}

286

原创文章,转载请注明出处。
http://www.blogjava.net/beijing2008/articles/285556.html



posted on 2009-07-05 15:28 .Sun 阅读(846) 评论(1)  编辑  收藏

FeedBack:
# re: 读取图片验证码
2009-08-19 16:35 | 陌路人
受益匪浅。
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: