asp版扭曲图形验证码
<%
Class CAMXAM_COM_RndCodeBmp
Private TextDataLib,TextData,BmpData,BmpHead,BmpWidth,BmpHeight,BmpSize,TextDataLength,SessionString
End Sub
Private Sub Class_Terminate()
End Sub
Public Function SetTextInfo(TextString)
ReDim TextDataLib(0)
TextDataLib(0) = TextString
End Function
Public Function AddTextData(TextDataString)
TextDataLength = UBound(TextDataLib) + 1
ReDim Preserve TextDataLib(TextDataLength)
TextDataLib(TextDataLength) = TextDataString
End Function
Private Function Getbinary(IntNumber)
Dim IntBin0,IntBin1,IntBin2,IntBin0_,IntBin1_,IntBin2_
IntBin0 = Int(IntNumber / 16777216)
IntBin0_= IntNumber Mod 16777216
IntBin1 = Int(IntBin0_ / 65536)
IntBin1_= IntBin0_ Mod 65536
IntBin2 = Int(IntBin1_ / 256)
IntBin2_= IntBin1_ Mod 256
Getbinary = ChrB(IntBin2_) & ChrB(IntBin2) & ChrB(IntBin1) & ChrB(IntBin0)
End Function
Private Function GetColadRstring(ColadRstring)
Dim ColorArray,CALength
ColorArray = Split(ColadRstring,",")
CALength = UBound(ColorArray)
GetColadRstring = ColorArray(GetRnd(0,CALength))
End Function
Private Function GetRnd(LowNumber,UpNumber)
Randomize Timer
GetRnd = Int((UpNumber - LowNumber + 1) * Rnd + LowNumber)
End Function
Private Function FileHeader()
Response.Expires = -1
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"
Response.ContentType = "Image/BMP"
End Function
Private Function SetBmpHead(BmpSize,Width,Height)
BmpHead = ChrB(66) & ChrB(77)
BmpHead = BmpHead & Getbinary(BmpSize+54)
BmpHead = BmpHead & Getbinary(0)
BmpHead = BmpHead & Getbinary(54)
BmpHead = BmpHead & Getbinary(40)
BmpHead = BmpHead & Getbinary(Width)
BmpHead = BmpHead & Getbinary(Height)
BmpHead = BmpHead & ChrB(1) & ChrB(0)
BmpHead = BmpHead & ChrB(24) & ChrB(0)
BmpHead = BmpHead & Getbinary(0)
BmpHead = BmpHead & Getbinary(BmpSize)
BmpHead = BmpHead & Getbinary(65536)
BmpHead = BmpHead & Getbinary(65536)
BmpHead = BmpHead & Getbinary(16777216)
BmpHead = BmpHead & Getbinary(16777216)
End Function
Private Function GetRndTextData()
ReDim TextData(TextLength-1)
Dim IText
For IText = 0 To TextLength-1 Step +1
TextData(IText) = GetColadRstring(BackColor) & " " & GetColadRstring(TextColor) & " " & ChangeShape(TextDataLib(GetRnd(1,TextDataLength)))
SessionString = SessionString & GetTextData(IText)(4)
Next
End Function
Private Function GetTextData(IText)
Dim TextArray,ArrayLength,ITextData
TextArray = Split(TextData(IText)," ")
ArrayLength = UBound(TextArray)
Dim TextDataArray(5)
TextDataArray(0) = TextArray(0)
TextDataArray(1) = TextArray(1)
TextDataArray(2) = Len(TextArray(3))
TextDataArray(3) = ArrayLength - 2
TextDataArray(4) = TextArray(2)
For ITextData = 3 To ArrayLength Step +1
TextDataArray(5) = TextDataArray(5) & " " & TextArray(ITextData)
Next
GetTextData = TextDataArray
End Function
Private Function GetColor(ColadRstring)
GetColor = ChrB("&H" & Mid(ColadRstring,6,2)) & ChrB("&H" & Mid(ColadRstring,4,2)) & ChrB("&H" & Mid(ColadRstring,2,2))
End Function
Private Function BmpColor(ColorA,ColorB)
Dim BmpColor_(1)
BmpColor_(0) = GetColor(ColorA)
BmpColor_(1) = GetColor(ColorB)
BmpColor = BmpColor_
End Function
Private Function SetBmpData()
Dim TextInfo,FontData,I,J,K
GetRndTextData()
TextInfo = GetTextData(0)
BmpHeight = TextInfo(3)
BmpWidth = TextInfo(2) * TextLength
BmpSize = BmpHeight * BmpWidth * 3
For I = BmpHeight To 1 Step -1
For J = 0 To TextLength -1 Step +1
TextInfo = GetTextData(J)
FontData = Split(TextInfo(5)," ")(I)
For K = 1 To TextInfo(2) Step +1
If GetRnd(1,99) < YawpCount Then
BmpData = BmpData & GetColor(GetColadRstring(YawpColor))
Else
BmpData = BmpData & BmpColor(TextInfo(0),TextInfo(1))(Mid(FontData,K,1))
End If
Next
Next
Next
End Function
Public Function Write()
FileHeader()
SetBmpData()
SetBmpHead BmpSize,BmpWidth,BmpHeight
Response.BinaryWrite BmpHead
Response.BinaryWrite BmpData
Session(SessionName) = SessionString
End Function
Public Function ChangeShape(psString)
Randomize Timer
sTemp =Split(psString, " ")
Dim nMaxWidth, nMaxHeight : nMaxWidth =16 : nMaxHeight =20
Dim nWidth, nHeight : nWidth =Len(sTemp(1)) : nHeight =ubound(sTemp)
Dim nTemp, nTemp1, sTemp, sReturn, nTopStart, nCurLeft, nAspect
nTopStart =Int((nHeight - 1 + 1) * Rnd + 1)
nAspect =Int((2 - 1 + 1) * Rnd + 1)
nCurLeft =1
Dim sFilter
sFilter ="aJLkKfFbdDhTBpP6eQ7"
If Instr(sFilter, sTemp(0)) =0 Then
For nTemp =nTopStart To ubound(sTemp)
If nCurLeft >7 Then
nCurLeft =5
For nTemp1 =nTemp to ubound(sTemp)
If nCurLeft <1 Then Exit For
sTemp(nTemp1) =Fill("0", int(nCurLeft /2) ) & sTemp(nTemp1)
nCurLeft =nCurLeft -1
Next
Exit For
End If
sTemp(nTemp) =Fill("0", int(nCurLeft /2) ) & sTemp(nTemp)
nCurLeft =nCurLeft +1
Next
Else
For nTemp =1 to ubound(sTemp)
If nAspect =1 Then
sTemp(nTemp) =Fill("0", int(nCurLeft /2) ) & sTemp(nTemp)
Else
sTemp(nTemp) =sTemp(nTemp) & Fill("0", int(nCurLeft /2) )
If Len(sTemp(nTemp)) <nMaxWidth Then sTemp(nTemp) =Fill("0", nMaxWidth - Len(sTemp(nTemp))) & sTemp(nTemp)
End If
nCurLeft =nCurLeft +1
Next
End If
For nTemp =1 to ubound(sTemp)
If Len(sTemp(nTemp)) <nMaxWidth Then sTemp(nTemp) =sTemp(nTemp) & Fill("0", nMaxWidth - Len(sTemp(nTemp)))
If sReturn <>"" Then sReturn =sReturn &" "
sReturn =sReturn & sTemp(nTemp)
Next
nTopStart =Int(((nMaxHeight -nHeight) - 1 + 1) * Rnd + 1)
For nTemp =1 to nTopStart
sReturn =Fill("0", nMaxWidth) &" "& sReturn
Next
For nTemp =0 to (nMaxHeight -nHeight -nTopStart) -1
sReturn =sReturn &" "& Fill("0", nMaxWidth)
Next
ChangeShape =sTemp(0) &" "& sReturn
End Function
Public Function Fill(psChar, pnWidth)
Dim nTemp, sTemp
For nTemp =1 To pnWidth
sTemp =sTemp & psChar
Next
Fill =sTemp
End Function
End Class
Set Bmp = New CAMXAM_COM_RndCodeBmp
Bmp.SetTextInfo "点阵来源黑睿工作室
Bmp.AddTextData "2 00111100 01111110 11100111 11000011 00000011 00000011 00000110 00001100 00011000 00110000 01100000 11111111 11111111"
Bmp.AddTextData "3 00111000 01111110 11000110 00000110 00000110 00011100 00011110 00000011 00000011 11000011 11000011 01111110 00111100"
Bmp.AddTextData "4 000001100 000011100 000011100 000111100 001101100 001101100 011001100 110001100 111111111 111111111 000001100 000001100 000001100"
Bmp.AddTextData "5 01111110 01111110 01100000 11000000 11011100 11111110 11000011 00000011 00000011 11000011 11000011 01111110 00111100"
Bmp.AddTextData "6 00111100 01111110 01100011 11000000 11000000 11011100 11111110 11100011 11000011 11000011 01100011 01111110 00111100"
Bmp.AddTextData "7 11111111 11111111 00000010 00000110 00001100 00001100 00011000 00011000 00011000 00011000 00110000 00110000 00110000"
Bmp.AddTextData "8 00111100 01111110 11000011 11000011 11000011 01111110 01111110 11000011 11000011 11000011 11000011 01111110 00111100"
Bmp.AddTextData "A 0011110000 0000110000 0001111000 0001111000 0011001100 0011001100 0011111100 0110000110 0110000110 1111001111"
Bmp.AddTextData "b 11000000 11000000 11000000 11011100 11111110 11100111 11000011 11000011 11000011 11000011 11100111 11111110 11011100"
Bmp.AddTextData "B 11111110 01100011 01100011 01100011 01111110 01100011 01100011 01100011 01100011 11111110"
Bmp.AddTextData "c 0011110 0111111 1110011 1100000 1100000 1100000 1100000 1110011 0111111 0011110"
Bmp.AddTextData "C 00111111 01100111 11000011 11000011 11000000 11000000 11000000 11000000 01100011 00111110"
Bmp.AddTextData "d 00000011 00000011 00000011 00111011 01111111 11100111 11000011 11000011 11000011 11000011 11100111 01111111 00111011"
Bmp.AddTextData "e 00111100 01111110 11100110 11000011 11111111 11111111 11000000 11100011 01111110 00111100"
Bmp.AddTextData "E 11111111 01100011 01100000 01101100 01111100 01101100 01100000 01100000 01100011 11111111"
Bmp.AddTextData "f 0001111 0011111 0011000 0011000 0011000 1111110 1111110 0011000 0011000 0011000 0011000 0011000 0011000 0011000 0111000"
Bmp.AddTextData "F 11111111 01100011 01100000 01101100 01111100 01101100 01100000 01100000 01100000 01100000"
Bmp.AddTextData "G 001111110 011001110 110000110 110000110 110000000 110011111 110000110 110000110 011000110 001111100"
Bmp.AddTextData "h 11000000 11000000 11000000 11011110 11111111 11100011 11000011 11000011 11000011 11000011 11000011 11000011 11000011"
Bmp.AddTextData "H 1111001111 0110000110 0110000110 0110000110 0111111110 0110000110 0110000110 0110000110 0110000110 1111001111"
Bmp.AddTextData "J 000111111 000001100 000001100 000001100 000001100 000001100 000001100 110001100 110001100 011111000"
Bmp.AddTextData "k 11000000 11000000 11000000 11000111 11001110 11011100 11110000 11111000 11011000 11001100 11001100 11000110 11000111"
Bmp.AddTextData "K 111111110 011001100 011011000 011011000 011110000 011110000 011011000 011001100 011000110 111100111"
Bmp.AddTextData "L 11111000 01100000 01100000 01100000 01100000 01100000 01100000 01100000 01100011 11111111"
Bmp.AddTextData "m 110111001110 111111011111 111001110011 110001100011 110001100011 110001100011 110001100011 110001100011 110001100011 110001100011"
Bmp.AddTextData "M 1110000111 0110000110 0111001110 0111001110 0111111110 0111111110 0110110110 0110110110 0110000110 1111001111"
Bmp.AddTextData "n 11011110 11111111 11100011 11000011 11000011 11000011 11000011 11000011 11000011 11000011"
Bmp.AddTextData "N 1110011111 0110000110 0111000110 0111100110 0111100110 0110110110 0110011110 0110011110 0110001110 1111100110"
Bmp.AddTextData "p 11011100 11111110 11100111 11000011 11000011 11000011 11000011 11100111 11111110 11011100 11000000 11000000 11000000 11000000"
Bmp.AddTextData "P 11111110 01100011 01100011 01100011 01100011 01111110 01100000 01100000 01100000 01100000"
Bmp.AddTextData "R 111111100 011000110 011000110 011000110 011000110 011111100 011011000 011001100 011000110 111100111"
Bmp.AddTextData "s 01111110 11111110 11000000 11100000 01111100 00011110 00000011 10000011 11111111 01111100"
Bmp.AddTextData "S 01111111 11000111 11000011 11000000 01111000 00001110 00000011 11000011 11100011 11111110"
Bmp.AddTextData "t 00110000 00110000 11111111 11111111 00110000 00110000 00110000 00110000 00110000 00110000 00111111 00011111"
Bmp.AddTextData "T 11111111 10011001 00011000 00011000 00011000 00011000 00011000 00011000 00011000 00011000"
Bmp.AddTextData "u 11000011 11000011 11000011 11000011 11000011 11000011 11000011 11000111 11111111 01111011"
Bmp.AddTextData "U 1111001111 0110000110 0110000110 0110000110 0110000110 0110000110 0110000110 0110000110 0111001110 0001111000"
Bmp.AddTextData "v 110000011 011000011 011000110 011000110 001100110 001100100 000110100 000111100 000111000 000011000"
Bmp.AddTextData "V 1111001111 0110000110 0110000110 0011001100 0011001100 0011001100 0001111000 0001111000 0000110000 0000110000"
Bmp.AddTextData "w 11000000001 11000110011 11001110011 01001010010 01001010010 01101011010 01110011110 01110001110 00110001100 00110001100"
Bmp.AddTextData "W 1110000111 1100000011 1100110011 1100110011 0111111110 0111111110 0111111110 0011001100 0011001100 0011001100"
Bmp.AddTextData "x 111000011 011000110 001101100 001111100 000111000 000111000 001111100 001001100 011000110 110000111"
Bmp.AddTextData "X 1111001111 0110000110 0011001100 0001111000 0000110000 0000110000 0001111000 0011001100 0110000110 1111001111"
Bmp.AddTextData "y 1100000011 0110000010 0110000110 0011000100 0011001100 0001101000 0001111000 0001111000 0000110000 0000110000 0001100000 0111100000 0111000000"
Bmp.AddTextData "Y 1111001111 0110000110 0011001100 0011001100 0001111000 0000110000 0000110000 0000110000 0000110000 0000110000"
Bmp.AddTextData "z 11111111 11111111 00000011 00000110 00001100 00011000 00110000 01100000 11111111 11111111"
Bmp.AddTextData "Z 11111111 11000011 00000110 00001100 00011000 00011000 00110000 01100000 11000011 11111111"
Bmp.TextLength = 4'验证码长度
Bmp.BackColor = "#FFFFFF"'背景颜色,多个颜色用"," 分隔
Bmp.TextColor = "#FF0000,#FF9900,#33CCFF,#99CC00,#FF99FF,#9933FF,#0099FF" '文字颜色,多个颜色用逗号"," 分隔
Bmp.YawpColor = Bmp.TextColor '噪波点颜色,多个颜色使用"," 分隔
Bmp.YawpCount = 2 '噪波点比例
Bmp.Write()
%>
下面做一点介绍:
nMaxWidth =16 : nMaxHeight =20这是每个字符的最大宽度和最大高度,
Bmp.AddTextData "2 00111100 01111110 11100111 11000011 00000011 00000011 00000110 00001100 00011000 00110000 01100000 11111111 11111111"
可以很容易看出来,这就是字符的点阵信息,0表示空,1表示一个点,第1位的 2 表示这个点阵字符串表示哪个字符,字符点阵信息的每一行用空格分隔开来,试着把上面的点阵中的空格替换成换行符,
00111100
01111110
11100111
11000011
00000011
00000011
00000110
00001100
00011000
00110000
01100000
11111111
11111111
看起来不是很明显,呵呵,再把 0 替换成 空格 看看什么样。
111111
111 111
11 11
怎么样?看的很清楚了吧。
这个验证码不要求每个字符的点阵信息都同宽同高,因为我加的变形函数会按最大宽、最大高自动进行填补,所以自己去定制字符点阵信息的时候,可以随心所欲。
另外值得注意的一点是,这里面的最大宽 nMaxWidth 不要使用奇数,最好使用偶数。
11
11
11
11
11
11
11
11111111
11111111