自动投票中验证码识别问题初探
现在很多网站投票、发帖都采取了验证码机制,如果验证码位置角度是固定的话,就可以采取先采样生成字模,然后进行对比,最后得到对应的验证码。这种方法基本上可以不用除噪,而且识别率达到百分之百准确。下面是我写的验证码识别代码:
Static Words As Byte
Dim i As Integer
Dim m As Integer
Dim n As Integer
Dim FNum As Integer
Dim ImgStr As String
Dim Kuan As Integer
Dim Gao As Integer
Dim lColor As Long
Dim MaNub As Integer
Dim DImgStr As String
Dim ZuiHouMa As String
Dim YZMa As Integer
Dim TopKong As Integer
Dim LeftKong As Integer
Dim RightKong As Integer
Dim CYL(0 To 15) As Integer '差异率
'验证图片宽度55,高度16,从第六个点(编号5)开始,到第50个点(编号49)结束,共五个字,5*9=45 上空4下空2
'验证图片宽度100 高度25 从26点开始,每字宽9,右空2,上空5,高13
MaNub = 5 '一幅图里有几个码
Kuan = 9
Gao = 13
TopKong = 5
LeftKong = 26
RightKong = 2
WordX = (Me.Left + Picture1.Left) / Screen.TwipsPerPixelX + LeftKong
WordY = (Me.Top + Picture1.Top) / Screen.TwipsPerPixelY + TopKong
'MsgBox WordX & WordY
'ls = SetPixel(GetDC(0), WordX, WordY, vbGreen)
WordX = LeftKong
WordY = TopKong
ZuiHouMa = ""
For i = 0 To MaNub - 1
ImgStr = ""
For m = 0 To Gao - 1
For n = 0 To Kuan - 1
lColor = GetPixel(Picture1.hdc, WordX + n, WordY + m)
If GetRedValue(lColor) > 100 And GetGreenValue(lColor) < 100 And GetBlueValue(lColor) < 100 Then
ImgStr = ImgStr & "1"
Else
ImgStr = ImgStr & "0"
End If
Next n
Next m
YZMa = 9
CYL(YZMa) = 100
For m = 0 To 9
CYL(m) = 0
For n = 0 To Kuan * Gao - 1
If (Mid(FontDat(m), n + 1, 1) = "1") And (Mid(ImgStr, n + 1, 1) = "0") Then CYL(m) = CYL(m) + 1
If (Mid(FontDat(m), n + 1, 1) = "0") And (Mid(ImgStr, n + 1, 1) = "1") Then CYL(m) = CYL(m) + 1
Next n
If CYL(m) < CYL(YZMa) Then YZMa = m
Next m
ZuiHouMa = ZuiHouMa & Trim(Hex(YZMa))
WordX = WordX + Kuan + RightKong
Next i
Text1.Text = ZuiHouMa
Dim i As Integer
Dim m As Integer
Dim n As Integer
Dim FNum As Integer
Dim ImgStr As String
Dim Kuan As Integer
Dim Gao As Integer
Dim lColor As Long
Dim MaNub As Integer
Dim DImgStr As String
Dim ZuiHouMa As String
Dim YZMa As Integer
Dim TopKong As Integer
Dim LeftKong As Integer
Dim RightKong As Integer
Dim CYL(0 To 15) As Integer '差异率
'验证图片宽度55,高度16,从第六个点(编号5)开始,到第50个点(编号49)结束,共五个字,5*9=45 上空4下空2
'验证图片宽度100 高度25 从26点开始,每字宽9,右空2,上空5,高13
MaNub = 5 '一幅图里有几个码
Kuan = 9
Gao = 13
TopKong = 5
LeftKong = 26
RightKong = 2
WordX = (Me.Left + Picture1.Left) / Screen.TwipsPerPixelX + LeftKong
WordY = (Me.Top + Picture1.Top) / Screen.TwipsPerPixelY + TopKong
'MsgBox WordX & WordY
'ls = SetPixel(GetDC(0), WordX, WordY, vbGreen)
WordX = LeftKong
WordY = TopKong
ZuiHouMa = ""
For i = 0 To MaNub - 1
ImgStr = ""
For m = 0 To Gao - 1
For n = 0 To Kuan - 1
lColor = GetPixel(Picture1.hdc, WordX + n, WordY + m)
If GetRedValue(lColor) > 100 And GetGreenValue(lColor) < 100 And GetBlueValue(lColor) < 100 Then
ImgStr = ImgStr & "1"
Else
ImgStr = ImgStr & "0"
End If
Next n
Next m
YZMa = 9
CYL(YZMa) = 100
For m = 0 To 9
CYL(m) = 0
For n = 0 To Kuan * Gao - 1
If (Mid(FontDat(m), n + 1, 1) = "1") And (Mid(ImgStr, n + 1, 1) = "0") Then CYL(m) = CYL(m) + 1
If (Mid(FontDat(m), n + 1, 1) = "0") And (Mid(ImgStr, n + 1, 1) = "1") Then CYL(m) = CYL(m) + 1
Next n
If CYL(m) < CYL(YZMa) Then YZMa = m
Next m
ZuiHouMa = ZuiHouMa & Trim(Hex(YZMa))
WordX = WordX + Kuan + RightKong
Next i
Text1.Text = ZuiHouMa
程序运行界面如下:
注:界面借用了网上下载的一个程序。。但识别内核是我自己写的。
DEMO程序如下:
http://my82163.51.net/readcode.rar