本文給出了一種驗證碼識別方案,前提是咱們要有生成驗證碼的二進制碼。數組
咱們要識別的圖片文件:函數
將以上圖片保存到本地,並命名爲:1.jpgspa
識別該圖片的vbs源碼以下:code
Option Explicit '要識別的圖片名稱 Dim picName picName = "1.jpg" '數字對照表,識別圖片時就是把從圖片提取的與該數組比較,相同的即是該數字 Dim NumAry(9) NumAry(0)="00011000001111000110011011000011110000111100001111000011011001100011110000011000" NumAry(1)="00011000001110000111100000011000000110000001100000011000000110000001100001111110" NumAry(2)="00111100011001101100001100000011000001100000110000011000001100000110000011111111" NumAry(3)="01111100110001100000001100000110000111000000011000000011000000111100011001111100" NumAry(4)="00000110000011100001111000110110011001101100011011111111000001100000011000000110" NumAry(5)="11111110110000001100000011011100111001100000001100000011110000110110011000111100" NumAry(6)="00111100011001101100001011000000110111001110011011000011110000110110011000111100" NumAry(7)="11111111000000110000001100000110000011000001100000110000011000001100000011000000" NumAry(8)="00111100011001101100001101100110001111000110011011000011110000110110011000111100" NumAry(9)="00111100011001101100001111000011011001110011101100000011010000110110011000111100" Dim st,dataOff,imgW,imgH,imgWBytes,unitW,unitH Set st = Wscript.createobject("ADODB.Stream") st.Type = 1 st.Mode = 3 st.open() '加載圖片二進制流,並讀取圖片頭信息 st.LoadFromFile(picName) st.position = 10 '獲取數據偏移 dataOff = BinVal(st.read(4)) st.read(4) '圖片寬、高(象素) imgW = BinVal(st.read(4)) imgH = BinVal(st.read(4)) imgWBytes = imgW '每一個數字的寬、高(象素) unitW = 8 unitH = 10 MsgBox st.LoadFromFile(picName) MsgBox dataOff MsgBox imgW&vbCrLf&imgH Dim i,ii,tmp,validCode '循環獲取五個數字 For i=0 To 4 '獲取某數字的特徵,並與對照表進行比較,找到對應的則記錄,不然以*號標識 '3188是第一個數字的左上角的數據偏移,每向後一個則偏移增長 unitW+1 tmp = getBound(3188+(unitW+1)*i) For ii=0 To 9 If tmp = NumAry(ii) Then validCode = validCode & ii Exit For End If Next If Len(validCode) = i Then validCode = validCode & "*" Next '關閉 st.Close() Set st = Nothing '程序完成 MsgBox("驗證碼是:"&validCode) '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '獲取指定矩形區域的特徵碼 'bp:矩形左上角的偏移 unitW:矩形寬 unitH:矩形高 '按照矩形圖形從左到右、從上到下的方向進行提取,如該點二進制爲1則表示爲1,不然爲0 Function getBound(bp) Dim str,i,ii st.Position = bp For i=1 To unitH For ii=1 To unitW If AscB(st.Read(1)) = 1 Then str = str & "1" Else str = str & "0" End If Next st.Position = bp - i*imgWBytes Next getBound = str End Function '將2進制轉化爲數字 Function BinVal(bin) dim ret ret = 0 for i = lenb(bin) to 1 step -1 ret = ret *256 + ascb(midb(bin,i,1)) next BinVal=ret End Function Rem 將字符轉換成2進制數組的函數 function ASCIIToByteArray(sText) Dim objRS Dim lTemp Dim sTemp sTemp = "" For lTemp = 1 to LenB(sText) sTemp = sTemp & Right("00" & Hex(AscB(MidB(sText,lTemp,1))),2) Next set objRS = WScript.CreateObject("ADODB.Recordset") objRS.Fields.Append "Temp",204,LenB(sText)+1 objRS.Open objRS.AddNew objRS("Temp") = sTemp ' ADODB will convert here objRS.Update objRS.MoveFirst ASCIIToByteArray = objRS("Temp") ' A variant byte array is returned objRS.Close set objRS = Nothing end function
將以上vbs源碼另存爲:驗證碼圖片識別.vbs,並和1.jpg在同一目錄:圖片
雙擊「驗證碼圖片識別.vbs」,識別結果以下:ip