这是一个使用VB6编写的自动化程序,通过WebBrowser控件实现对中国联通官网的自动充值功能,核心是验证码的图像识别处理。
验证码特征:中国联通官网验证码固定位置、固定大小、无干扰元素
识别原理:为每个数字建模,逐像素点对比,匹配度最高的数字即为结果
实现方式:WebBrowser控件 + 自动填表 + 自动点击
'################################处理自动填表################################
If URL = "http://pay.10010.com/payFeeOnline/payFeeOnlineInit.action" And Form1.Tag = "0" Then
'获取验证码图像,开始解析
Command1.Caption = "正在获取验证码,请稍侯..."
Dim Buff() As Byte
Inet1.URL = "http://ecard.10010.com/getImage.jsp"
Buff() = Inet1.OpenURL(, icByteArray)
If UBound(Buff()) < 100 Then GoTo error1
With Picture1
.Picture = PictureFromBits(Buff())
.PaintPicture .Picture, 0, 0, 700, 400, 0, 0, .ScaleWidth, .ScaleHeight
End With
DoEvents
'识别验证码
Call GetCode
Command1.Caption = "订单生成中..."
tyty = Split(Get_Data, "*") '充值号码,每次一个
If tyty(2) <> "1" Then tyty(1) = CStr(CSng(tyty(1)) * CSng(tyty(2))) '金额=金额*数量
WebBrowser1.Document.All("payFeeOnlineInfo.productNO").Value = tyty(0)
WebBrowser1.Document.All("payFeeOnlineInfo.productNOAgain").Value = tyty(0)
WebBrowser1.Document.All("payFeeOnlineInfo.payFee").Value = tyty(1)
WebBrowser1.Document.All("payFeeOnlineInfo.checkCode").Value = Text1.Text
'提交表单
WebBrowser1.Document.All("submit0").Click
Command1.Caption = "跳向确认订单!"
Form1.Tag = "1"
Exit Sub
error1:
Form1.Tag = "0"
End IfPrivate Sub GetCode() '识别========================= Command1.Caption = "正在识别验证码..." t1 = "" t2 = "" t3 = "" t4 = "" Dim ty(9) '数字0-9的二值化模型 ty(0) = "00001111111100011111111111011100000000111000000000011000000000011100000000001110000000111101111111111100" ty(1) = "00000000000010000000000001010000000011111111111111111011111111001110000000000111000000000010000000000000" ty(2) = "00000000001110100000011101110000001100110000001110011000001100001100000110000111111110000011111110000000" ty(3) = "00000000000110100001000001110000100000111000010000011000001000001100001111111111111101111101111100000000" ty(4) = "00000001110000000001111000000001101100000000110110000000110011000001110001111111111111111111111111111000" ty(5) = "00111100000111111110000001110001000000110000100000111000011000110100000111110010000011111001000000111000" ty(6) = "00001111111100011001000001011101100000101110110000011100010000001100001000001110000110001101000001111110" ty(7) = "00000000000011000000000111100000000011110000001111101000001111000100001100000010111100000001111100000000" ty(8) = "00111001111110111110111011110001110000110000010000011000001000001100001110011110000111001111111111111110" ty(9) = "00001100000110111111000001011100110000111000001000011000000100011100000111111010000011111101111111011100" Dim flag(10) '识别第一个数字 For i = 4 To 11 For j = 8 To 20 If Picture1.Point(i, j) < &H200000 Then t1 = t1 & "1" Else t1 = t1 & "0" End If Next j Next i Text1.Tag = 0 For M = 0 To 9 For N = 0 To Len(t1) If Mid(t1, N + 1, 1) = Mid(ty(M), N + 1, 1) Then flag(M) = flag(M) + 1 End If Next N If flag(M) > CSng(Text1.Tag) Then Text1.Tag = flag(M) ty1 = M End If Next M '识别第二个数字(代码结构类似,省略中间部分...) '... Text1.Text = ty1 & ty2 & ty3 & ty4 End Sub
图像获取:通过Inet控件下载验证码图片字节流
图像显示:使用Picture控件显示验证码图像
像素分析:通过Picture1.Point方法获取指定坐标点的颜色值
二值化处理:将彩色图像转换为黑白二值图像进行对比
模板匹配:预定义数字模板,通过像素匹配度识别数字
容错处理:识别错误时自动重新获取验证码
应用场景:该方案适用于验证码规则固定、无干扰元素的网站自动化处理,通过建立数字模板库和像素级对比,实现了较高的识别准确率。