博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个翻译自VB6.0的验证码识别代码
阅读量:4982 次
发布时间:2019-06-12

本文共 3778 字,大约阅读时间需要 12 分钟。

2011062708415095.jpg
原先是一个
VB6.0
验证码识别源码,修改成了
VB2008下的。代码很初级,仅供参考。
验证码识别的基本思路:获取图片→二值化→去噪→分割匹配→识别
ContractedBlock.gif
ExpandedBlockStart.gif
获取验证码图片并显示在PictureBox上
 
1
Dim
WR
As
WebRequest
=
WebRequest.Create(TxtUrl.Text)
2
Dim
RESP
As
WebResponse
=
WR.GetResponse
3
Dim
BMP
As
New
Bitmap(RESP.GetResponseStream)
4
PictureBox1.Image
=
BMP
ContractedBlock.gif
ExpandedBlockStart.gif
二值化
 
1
Public
Sub
Binarization()
'
二值化
2
Dim
NumS
As
String
=
""
3
'
Dim pix(0, 0) As Boolean
4
ReDim
pix(PictureBox1.Width
-
1
, PictureBox1.Height
-
1
)
5
6
Dim
iy
As
Integer
7
Dim
ix
As
Integer
8
9
Dim
BMP
As
Bitmap
=
New
Bitmap(PictureBox1.Image)
10
11
Dim
Pixel
As
Color
12
13
CG
=
TxtG.Text : CR
=
TxtR.Text : CB
=
TxtB.Text
14
For
iy
=
0
To
BMP.Height
-
1
15
For
ix
=
0
To
BMP.Width
-
1
16
Pixel
=
BMP.GetPixel(ix, iy)
17
If
Pixel.G
<>
CG
Or
Pixel.R
<>
CR
Or
Pixel.B
<>
CB
Then
18
pix(ix, iy)
=
False
19
Else
20
pix(ix, iy)
=
True
21
End
If
22
Next
23
Next
24
25
Dim
str
As
String
=
""
26
For
iy
=
0
To
pix.GetUpperBound(
1
)
'
UBound(pix(, ), 2)
27
For
ix
=
0
To
pix.GetUpperBound(
0
)
'
UBound(pix(), 1)
28
If
pix(ix, iy)
Then
29
str
=
str
&
"
"
30
Else
31
str
=
str
&
"
"
32
End
If
33
Next
34
str
=
str
&
vbCrLf
35
Next
36
37
TxtChar.Text
=
str
38
End Sub
ContractedBlock.gif
ExpandedBlockStart.gif
分割验证码
 
1
Public
Sub
SplitCheckCode()
'
分割验证码
2
Dim
i, j, k, p
As
Integer
3
Dim
bL, bR
As
Boolean
4
'
第一步, 找出纵向分割界线
5
vCode(
0
).Left
=
0
6
For
i
=
0
To
Pix.GetUpperBound(
0
)
7
For
j
=
0
To
Pix.GetUpperBound(
1
)
8
If
Pix(i, j)
=
False
Then
Exit
For
'
检验是否整列均为■
9
Next
10
If
j
=
Pix.GetUpperBound(
1
)
+
1
Then
11
If
i
>
Pix.GetLowerBound(
0
)
Then
12
For
k
=
0
To
Pix.GetUpperBound(
1
)
13
If
Pix(i
-
1
, k)
=
False
Then
Exit
For
'
检验是否整列均为■
14
Next
15
If
k
<
Pix.GetUpperBound(
1
)
+
1
Then
bL
=
True
16
End
If
17
If
i
<
Pix.GetUpperBound(
0
)
Then
18
For
k
=
0
To
Pix.GetUpperBound(
1
)
19
If
Pix(i
+
1
, k)
=
False
Then
Exit
For
'
检验是否整列均为■
20
Next
21
If
k
<
Pix.GetUpperBound(
1
)
+
1
Then
bR
=
True
22
End
If
23
End
If
24
If
(bL
Or
bR)
Then
25
p
=
p
+
1
: bL
=
False
: bR
=
False
26
Select
Case
p
27
Case
1
: vCode(
0
).Right
=
i
-
1
28
Case
2
: vCode(
1
).Left
=
i
+
1
29
Case
3
: vCode(
1
).Right
=
i
-
1
30
Case
4
: vCode(
2
).Left
=
i
+
1
31
Case
5
: vCode(
2
).Right
=
i
-
1
32
Case
6
: vCode(
3
).Left
=
i
+
1
33
Case
7
: vCode(
3
).Right
=
i
-
1
34
End
Select
35
End
If
36
Next
37
For
k
=
0
To
3
38
vCode(k).Top
=
0
: vCode(k).Bottom
=
9
: vCode(k).CodeChar
=
""
39
Next
40
'
第二步, 将所有字符转为特征码
41
For
k
=
0
To
3
42
For
i
=
vCode(k).Left
To
vCode(k).Right
43
For
j
=
vCode(k).Top
To
vCode(k).Bottom
44
If
Pix(i, j)
Then
p
=
0
Else
p
=
1
45
vCode(k).CodeChar
=
vCode(k).CodeChar
&
CStr
(p)
46
Next
47
Next
48
Next
49
End Sub
ContractedBlock.gif
ExpandedBlockStart.gif
识别验证码
 
1
Public
Sub
SpotCheckCode()
'
识别验证码
2
Dim
BaseCode(
9
)
As
String
3
BaseCode(
0
)
=
"
011111111010000000011000000001100000000110000000010111111110
"
4
BaseCode(
1
)
=
"
01000000010100000001111111111100000000010000000001
"
5
BaseCode(
2
)
=
"
010000001110000001011000001001100001000110001000010111000001
"
6
BaseCode(
3
)
=
"
010000001010000000011000100001100010000110001000010111011110
"
7
BaseCode(
4
)
=
"
000001100000001010000011001000010000100111111111110000001001
"
8
BaseCode(
5
)
=
"
111110001010001000011000100001100010000110001000011000011110
"
9
BaseCode(
6
)
=
"
001111111001000100011000100001100010000110001000010000011110
"
10
BaseCode(
7
)
=
"
110000000010000000111000001100100011000010110000001100000000
"
11
BaseCode(
8
)
=
"
011101111010001000011000100001100010000110001000010111011110
"
12
BaseCode(
9
)
=
"
011110000010000100011000010001100001000110001000100111111100
"
13
14
Dim
i, k
As
Integer
15
Dim
s
As
String
16
For
k
=
0
To
3
17
s
=
"
$
"
18
For
i
=
0
To
9
19
If
vCode(k).CodeChar
=
BaseCode(i)
Then
20
s
=
CStr
(i) :
Exit
For
21
End
If
22
Next
23
TxtResult.Text
=
TxtResult.Text
&
s
24
Next
25
End Sub

转载于:https://www.cnblogs.com/spikef/archive/2011/06/27/2091067.html

你可能感兴趣的文章