以前公司业务人员手动操作,一天也只能获取1000来条数据,通过程序获得了非常大的改观。程序目的是自动验证登录并查询获取相关信息,对验证码上每个字显示的宽度有要求,需要事先预设好,因为需要将验证码图片上的字分别裁剪单独分析。当然一般一个网站的验证码宽度都相对固定。
1
package com.xiaohang.readcode;
2
3
import java.awt.*;
4
import java.awt.image.BufferedImage;
5
import java.io.*;
6
import java.net.URL;
7
import javax.imageio.*;
8
9
public 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(5, 0, 10, 15);
42
newim[1] = imd.getSubimage(18, 0, 10, 15);
43
newim[2] = imd.getSubimage(31, 0, 10, 15);
44
newim[3] = imd.getSubimage(44, 0, 10, 15);
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
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
83
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
84
0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
85
0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
86
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
87
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
88
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
89
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
90
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
91
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
92
0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
93
0, 0, 0, 1, 1, 1, 1, 0, 0, 0,
94
0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
95
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96
0, 0, 0, 0, 0, 0, 0, 0, 0, 0
97
},
98
// 1
99
{
100
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
102
0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
103
0, 0, 0, 0, 1, 1, 1, 0, 0, 0,
104
0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
105
0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
106
0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
107
0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
108
0, 0, 0, 0, 0, 1, 1, 0, 0, 0,
109
0, 0, 0, 0, 0, 1, 1, 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0, 0, 0, 0, 0, 0, 0
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 阅读(848)
评论(1) 编辑 收藏