`
caozuiba
  • 浏览: 904283 次
文章分类
社区版块
存档分类
最新评论

数字验证码.asp

 
阅读更多

<%
'验证时用Cstr(Session("getcode"))和用户输入值进行比较

Const nMaxSaturation = 100 ' 最大色彩饱和度
Const nBlankNoisyDotOdds = 0.2 ' 空白处噪点率
Const nColorNoisyDotOdds = 0.1 ' 有色处噪点率
Const nCharCount = 4 ' 产生的字符个数
Const nPixelWidth = 20 ' 单个字符位图的宽度
Const nPixelHeight = 20 ' 单个字符位图的高度
Const nColorHue = 220 ' 显示验证码的色调(-1表示随机色调, -2表示灰度色调)
Const nAngleRandom = 10 ' 角度随机量
Const nLengthRandom = 10 ' 长度随机量(百分比)
Const cCharSet = "0123456789"
' 构成验证码的字符集
' 如果扩充了下边的字母矢量库,则可以相应扩充这个字符集

Dim Buf(), DigtalStr
Dim Lines(), LineCount
Dim CursorX, CursorY, DirX, DirY

Randomize
Call CreatValidCode("GetCode")

Sub CDGen_Reset()
' 复位矢量笔和环境变量
LineCount = 0
CursorX = 0
CursorY = 0
' 初始的光笔方向是垂直向下
DirX = 0
DirY = 1
End Sub

Sub CDGen_Clear()
' 清空位图阵列
Dim i, j
ReDim Buf(nPixelHeight - 1, nCharCount * nPixelWidth - 1)

For j = 0 To nPixelHeight - 1
For i = 0 To nCharCount * nPixelWidth - 1
Buf(j, i) = 0
Next
Next
End Sub

Sub CDGen_PSet(X, Y)
' 在位图阵列上画点
Buf(Y, X) = 1
End Sub

Sub CDGen_Line(X1, Y1, X2, Y2)
' 在位图阵列上画线
Dim DX, DY, DeltaT, i

DX = X2 - X1
DY = Y2 - Y1
If Abs(DX) > Abs(DY) Then DeltaT = Abs(DX) Else DeltaT = Abs(DY)
For i = 0 To DeltaT
CDGen_PSet X1 + DX * i / DeltaT, Y1 + DY * i / DeltaT
Next
End Sub

Sub CDGen_FowardDraw(nLength)
' 按当前光笔方向绘制指定长度并移动光笔,正数表示从左向右/从上向下绘制,负数表示从右向左/从下向上绘制
nLength = Sgn(nLength) * Abs(nLength) * (1 - nLengthRandom / 100 + Rnd * nLenghtRandom * 2 / 100)
ReDim Preserve Lines(3, LineCount)
Lines(0, LineCount) = CursorX
Lines(1, LineCount) = CursorY
CursorX = CursorX + DirX * nLength
CursorY = CursorY + DirY * nLength
Lines(2, LineCount) = CursorX
Lines(3, LineCount) = CursorY
LineCount = LineCount + 1
End Sub

Sub CDGen_SetDirection(nAngle)
' 按指定角度设定画笔方向, 正数表示相对当前方向顺时针改变方向,负数表示相对当前方向逆时针改变方向
Dim DX, DY

nAngle = Sgn(nAngle) * (Abs(nAngle) - nAngleRandom + Rnd * nAngleRandom * 2) / 180 * 3.1415926
DX = DirX
DY = DirY
DirX = DX * Cos(nAngle) - DY * Sin(nAngle)
DirY = DX * Sin(nAngle) + DY * Cos(nAngle)
End Sub

Sub CDGen_MoveToMiddle(nActionIndex, nPercent)
' 将画笔光标移动到指定动作的中间点上,nPercent为中间位置的百分比
Dim DeltaX, DeltaY

DeltaX = Lines(2, nActionIndex) - Lines(0, nActionIndex)
DeltaY = Lines(3, nActionIndex) - Lines(1, nActionIndex)
CursorX = Lines(0, nActionIndex) + Sgn(DeltaX) * Abs(DeltaX) * nPercent / 100
CursorY = Lines(1, nActionIndex) + Sgn(DeltaY) * Abs(DeltaY) * nPercent / 100
End Sub

Sub CDGen_MoveCursor(nActionIndex)
' 将画笔光标移动到指定动作的起始点上
CursorX = Lines(0, nActionIndex)
CursorY = Lines(1, nActionIndex)
End Sub

Sub CDGen_Close(nActionIndex)
' 将当前光笔位置与指定动作的起始点闭合并移动光笔
ReDim Preserve Lines(3, LineCount)
Lines(0, LineCount) = CursorX
Lines(1, LineCount) = CursorY
CursorX = Lines(0, nActionIndex)
CursorY = Lines(1, nActionIndex)
Lines(2, LineCount) = CursorX
Lines(3, LineCount) = CursorY
LineCount = LineCount + 1
End Sub

Sub CDGen_CloseToMiddle(nActionIndex, nPercent)
' 将当前光笔位置与指定动作的中间点闭合并移动光笔,nPercent为中间位置的百分比
Dim DeltaX, DeltaY

ReDim Preserve Lines(3, LineCount)
Lines(0, LineCount) = CursorX
Lines(1, LineCount) = CursorY
DeltaX = Lines(2, nActionIndex) - Lines(0, nActionIndex)
DeltaY = Lines(3, nActionIndex) - Lines(1, nActionIndex)
CursorX = Lines(0, nActionIndex) + Sgn(DeltaX) * Abs(DeltaX) * nPercent / 100
CursorY = Lines(1, nActionIndex) + Sgn(DeltaY) * Abs(DeltaY) * nPercent / 100
Lines(2, LineCount) = CursorX
Lines(3, LineCount) = CursorY
LineCount = LineCount + 1
End Sub

Sub CDGen_Flush(X0, Y0)
' 按照当前的画笔动作序列绘制位图点阵
Dim MaxX, MinX, MaxY, MinY
Dim DeltaX, DeltaY, StepX, StepY, OffsetX, OffsetY
Dim i

MaxX = MinX = MaxY = MinY = 0
For i = 0 To LineCount - 1
If MaxX < Lines(0, i) Then MaxX = Lines(0, i)
If MaxX < Lines(2, i) Then MaxX = Lines(2, i)
If MinX > Lines(0, i) Then MinX = Lines(0, i)
If MinX > Lines(2, i) Then MinX = Lines(2, i)
If MaxY < Lines(1, i) Then MaxY = Lines(1, i)
If MaxY < Lines(3, i) Then MaxY = Lines(3, i)
If MinY > Lines(1, i) Then MinY = Lines(1, i)
If MinY > Lines(3, i) Then MinY = Lines(3, i)
Next
DeltaX = MaxX - MinX
DeltaY = MaxY - MinY
If DeltaX = 0 Then DeltaX = 1
If DeltaY = 0 Then DeltaY = 1
MaxX = MinX
MaxY = MinY
If DeltaX > DeltaY Then
StepX = (nPixelWidth - 2) / DeltaX
StepY = (nPixelHeight - 2) / DeltaX
OffsetX = 0
OffsetY = (DeltaX - DeltaY) / 2
Else
StepX = (nPixelWidth - 2) / DeltaY
StepY = (nPixelHeight - 2) / DeltaY
OffsetX = (DeltaY - DeltaX) / 2
OffsetY = 0
End If
For i = 0 To LineCount - 1
Lines(0, i) = Round((Lines(0, i) - MaxX + OffsetX) * StepX, 0)
If Lines(0, i) < 0 Then Lines(0, i) = 0
If Lines(0, i) >= nPixelWidth - 2 Then Lines(0, i) = nPixelWidth - 3
Lines(1, i) = Round((Lines(1, i) - MaxY + OffsetY) * StepY, 0)
If Lines(1, i) < 0 Then Lines(1, i) = 0
If Lines(1, i) >= nPixelHeight - 2 Then Lines(1, i) = nPixelHeight - 3
Lines(2, i) = Round((Lines(2, i) - MinX + OffsetX) * StepX, 0)
If Lines(2, i) < 0 Then Lines(2, i) = 0
If Lines(2, i) >= nPixelWidth - 2 Then Lines(2, i) = nPixelWidth - 3
Lines(3, i) = Round((Lines(3, i) - MinY + OffsetY) * StepY, 0)
If Lines(3, i) < 0 Then Lines(3, i) = 0
If Lines(3, i) >= nPixelHeight - 2 Then Lines(3, i) = nPixelHeight - 3
CDGen_Line Lines(0, i) + X0 + 1, Lines(1, i) + Y0 + 1, Lines(2, i) + X0 + 1, Lines(3, i) + Y0 + 1
Next
End Sub

Sub CDGen_Char(cChar, X0, Y0)
' 在指定坐标处生成指定字符的位图阵列
CDGen_Reset
Select Case cChar
Case "0"
CDGen_SetDirection -60 ' 逆时针60度(相对于垂直线)
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 逆时针60度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 1.5 ' 绘制1.5个单位
CDGen_SetDirection -60 ' 逆时针60度
CDGen_FowardDraw 0.7 ' 绘制0.7个单位
CDGen_SetDirection -60 ' 顺时针120度
CDGen_FowardDraw 0.7 ' 绘制0.7个单位
CDGen_Close 0 ' 封闭当前笔与第0笔(0开始)
Case "1"
CDGen_SetDirection -90 ' 逆时针90度(相对于垂直线)
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
CDGen_MoveToMiddle 0, 50 ' 移动画笔的位置到第0笔(0开始)的50%处
CDGen_SetDirection 90 ' 逆时针90度
CDGen_FowardDraw -1.4 ' 反方向绘制1.4个单位
CDGen_SetDirection 30 ' 逆时针30度
CDGen_FowardDraw 0.4 ' 绘制0.4个单位
Case "2"
CDGen_SetDirection 45 ' 顺时针45度(相对于垂直线)
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -120 ' 逆时针120度
CDGen_FowardDraw 0.4 ' 绘制0.4个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.6 ' 绘制0.6个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 1.6 ' 绘制1.6个单位
CDGen_SetDirection -135 ' 逆时针135度
CDGen_FowardDraw 1.0 ' 绘制1.0个单位
Case "3"
CDGen_SetDirection -90 ' 逆时针90度(相对于垂直线)
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection 135 ' 顺时针135度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection -120 ' 逆时针120度
CDGen_FowardDraw 0.6 ' 绘制0.6个单位
CDGen_SetDirection 80 ' 顺时针80度
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
Case "4"
CDGen_SetDirection 20 ' 顺时针20度(相对于垂直线)
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection -110 ' 逆时针110度
CDGen_FowardDraw 1.2 ' 绘制1.2个单位
CDGen_MoveToMiddle 1, 60 ' 移动画笔的位置到第1笔(0开始)的60%处
CDGen_SetDirection 90 ' 顺时针90度
CDGen_FowardDraw 0.7 ' 绘制0.7个单位
CDGen_MoveCursor 2 ' 移动画笔到第2笔(0开始)的开始处
CDGen_FowardDraw -1.5 ' 反方向绘制1.5个单位
Case "5"
CDGen_SetDirection 90 ' 顺时针90度(相对于垂直线)
CDGen_FowardDraw 1.0 ' 绘制1.0个单位
CDGen_SetDirection -90 ' 逆时针90度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection -90 ' 逆时针90度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection 30 ' 顺时针30度
CDGen_FowardDraw 0.4 ' 绘制0.4个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.4 ' 绘制0.4个单位
CDGen_SetDirection 30 ' 顺时针30度
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
Case "6"
CDGen_SetDirection -60 ' 逆时针60度(相对于垂直线)
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 逆时针60度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 1.5 ' 绘制1.5个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 0.7 ' 绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
CDGen_CloseToMiddle 2, 50 ' 将当前画笔位置与第2笔(0开始)的50%处封闭
Case "7"
CDGen_SetDirection 180 ' 顺时针180度(相对于垂直线)
CDGen_FowardDraw 0.3 ' 绘制0.3个单位
CDGen_SetDirection 90 ' 顺时针90度
CDGen_FowardDraw 0.9 ' 绘制0.9个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 1.3 ' 绘制1.3个单位
Case "8"
CDGen_SetDirection -60 ' 逆时针60度(相对于垂直线)
CDGen_FowardDraw -0.8 ' 反方向绘制0.8个单位
CDGen_SetDirection -60 ' 逆时针60度
CDGen_FowardDraw -0.8 ' 反方向绘制0.8个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection 110 ' 顺时针110度
CDGen_FowardDraw -1.5 ' 反方向绘制1.5个单位
CDGen_SetDirection -110 ' 逆时针110度
CDGen_FowardDraw 0.9 ' 绘制0.9个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.8 ' 绘制0.8个单位
CDGen_SetDirection 60 ' 顺时针60度
CDGen_FowardDraw 0.9 ' 绘制0.9个单位
CDGen_SetDirection 70 ' 顺时针70度
CDGen_FowardDraw 1.5 ' 绘制1.5个单位
CDGen_Close 0 ' 封闭当前笔与第0笔(0开始)
Case "9"
CDGen_SetDirection 120 ' 逆时针60度(相对于垂直线)
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 逆时针60度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 顺时针120度
CDGen_FowardDraw -1.5 ' 绘制1.5个单位
CDGen_SetDirection -60 ' 顺时针120度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 顺时针120度
CDGen_FowardDraw -0.7 ' 绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 顺时针120度
CDGen_FowardDraw 0.5 ' 绘制0.5个单位
CDGen_CloseToMiddle 2, 50 ' 将当前画笔位置与第2笔(0开始)的50%处封闭
' 以下为字母的矢量动作,有兴趣的可以继续
Case "A"
CDGen_SetDirection -(Rnd * 20 + 150) ' 逆时针150-170度(相对于垂直线)
CDGen_FowardDraw Rnd * 0.2 + 1.1 ' 绘制1.1-1.3个单位
CDGen_SetDirection Rnd * 20 + 140 ' 顺时针140-160度
CDGen_FowardDraw Rnd * 0.2 + 1.1 ' 绘制1.1-1.3个单位
CDGen_MoveToMiddle 0, 30 ' 移动画笔的位置到第1笔(0开始)的30%处
CDGen_CloseToMiddle 1, 70 ' 将当前画笔位置与第1笔(0开始)的70%处封闭
Case "B"
CDGen_SetDirection -(Rnd * 20 + 50) ' 逆时针50-70度(相对于垂直线)
CDGen_FowardDraw Rnd * 0.4 + 0.8 ' 绘制0.8-1.2个单位
CDGen_SetDirection Rnd * 20 + 110 ' 顺时针110-130度
CDGen_FowardDraw Rnd * 0.2 + 0.6 ' 绘制0.6-0.8个单位
CDGen_SetDirection -(Rnd * 20 + 110) ' 逆时针110-130度
CDGen_FowardDraw Rnd * 0.2 + 0.6 ' 绘制0.6-0.8个单位
CDGen_SetDirection Rnd * 20 + 110 ' 顺时针110-130度
CDGen_FowardDraw Rnd * 0.4 + 0.8 ' 绘制0.8-1.2个单位
CDGen_Close 0 ' 封闭当前笔与第1笔(0开始)
Case "C"
CDGen_SetDirection -60 ' 逆时针60度(相对于垂直线)
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection -60 ' 逆时针60度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 1.5 ' 绘制1.5个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw -0.7 ' 反方向绘制0.7个单位
CDGen_SetDirection 120 ' 顺时针120度
CDGen_FowardDraw 0.7 ' 绘制0.7个单位
End Select
CDGen_Flush X0, Y0
End Sub

Sub CDGen_Blur()
' 对产生的位图进行柔化处理
Dim i, j

For j = 1 To nPixelHeight - 2
For i = 1 To nCharCount * nPixelWidth - 2
If Buf(j, i) = 0 Then
If ((Buf(j, i - 1) Or Buf(j + 1, i)) And 1) <> 0 Then
' 如果当前点是空白点,且上下左右四个点中有一个点是有色点,则该点做柔化处理
Buf(j, i) = 2
End If
End If
Next
Next
End Sub

Sub CDGen_NoisyDot()
' 对产生的位图进行噪点处理
Dim i, j, NoisyDot, CurDot

For j = 0 To nPixelHeight - 1
For i = 0 To nCharCount * nPixelWidth - 1
If Buf(j, i) <> 0 Then
NoisyDot = Int(Rnd * Rnd * nMaxSaturation)
Select Case nColorNoisyDotOdds
Case 0
CurDot = nMaxSaturation
Case 1
CurDot = 0
Case Else
CurDot = NoisyDot
End Select
If Rnd < nColorNoisyDotOdds Then Buf(j, i) = CurDot Else Buf(j,i) = nMaxSaturation
Else
NoisyDot = Int(Rnd * nMaxSaturation)
Select Case nBlankNoisyDotOdds
Case 0
CurDot = 0
Case 1
CurDot = nMaxSaturation
Case Else
CurDot = NoisyDot
End Select
If Rnd < nBlankNoisyDotOdds Then Buf(j, i) = CurDot Else Buf(j,i) = 0
End If
Next
Next
End Sub

Sub CDGen()
' 生成位图阵列
Dim i, Ch
DigtalStr = ""
CDGen_Clear
For i = 0 To nCharCount - 1
Ch = Mid(cCharSet, Int(Rnd * Len(cCharSet)) + 1, 1)
DigtalStr = DigtalStr + Ch
CDGen_Char Ch, i * nPixelWidth, 0
Next
CDGen_Blur
CDGen_NoisyDot
End Sub

Function HSBToRGB(vH, vS, vB)
' 将颜色值由HSB转换为RGB
Dim aRGB(3), RGB1st, RGB2nd, RGB3rd
Dim nH, nS, nB
Dim lH, nF, nP, nQ, nT

vH = (vH Mod 360)
If vS > 100 Then
vS = 100
ElseIf vS < 0 Then
vS = 0
End If
If vB > 100 Then
vB = 100
ElseIf vB < 0 Then
vB = 0
End If
If vS > 0 Then
nH = vH / 60
nS = vS / 100
nB = vB / 100
lH = Int(nH)
nF = nH - lH
nP = nB * (1 - nS)
nQ = nB * (1 - nS * nF)
nT = nB * (1 - nS * (1 - nF))
Select Case lH
Case 0
aRGB(0) = nB * 255
aRGB(1) = nT * 255
aRGB(2) = nP * 255
Case 1
aRGB(0) = nQ * 255
aRGB(1) = nB * 255
aRGB(2) = nP * 255
Case 2
aRGB(0) = nP * 255
aRGB(1) = nB * 255
aRGB(2) = nT * 255
Case 3
aRGB(0) = nP * 255
aRGB(1) = nQ * 255
aRGB(2) = nB * 255
Case 4
aRGB(0) = nT * 255
aRGB(1) = nP * 255
aRGB(2) = nB * 255
Case 5
aRGB(0) = nB * 255
aRGB(1) = nP * 255
aRGB(2) = nQ * 255
End Select
Else
aRGB(0) = (vB * 255) / 100
aRGB(1) = aRGB(0)
aRGB(2) = aRGB(0)
End If
HSBToRGB = ChrB(Round(aRGB(2), 0)) & ChrB(Round(aRGB(1), 0)) & ChrB(Round(aRGB(0), 0))
End Function

Sub CreatValidCode(pSN)
Dim i, j, CurColorHue
' 禁止缓存
Response.Expires = -9999
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-ctrol", "no-cache"
Response.ContentType = "image/bmp"

Call CDGen
Session(pSN) = DigtalStr '记录入Session
Dim PicWidth, PicHeight, FileSize, PicDataSize
PicWidth = nCharCount * nPixelWidth
PicHeight = nPixelHeight
PicDataSize = PicWidth * PicHeight * 3
FileSize = PicDataSize + 54

' 输出BMP文件信息头
Response.BinaryWrite ChrB(66) & ChrB(77) & _
ChrB(FileSize Mod 256) & ChrB((FileSize / 256) Mod 256) & ChrB((FileSize / 256 /256) Mod 256) & ChrB(FileSize / 256 / 256 / 256) & _
ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & _
ChrB(54) & ChrB(0) & ChrB(0) & ChrB(0)

' 输出BMP位图信息头
Response.BinaryWrite ChrB(40) & ChrB(0) & ChrB(0) & ChrB(0) & _
ChrB(PicWidth Mod 256) & ChrB((PicWidth / 256) Mod 256) & ChrB((PicWidth / 256 /256) Mod 256) & ChrB(PicWidth / 256 / 256 / 256) & _
ChrB(PicHeight Mod 256) & ChrB((PicHeight / 256) Mod 256) & ChrB((PicHeight / 256/ 256) Mod 256) & ChrB(PicHeight / 256 / 256 / 256) & _
ChrB(1) & ChrB(0) & _
ChrB(24) & ChrB(0) & _
ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & _
ChrB(PicDataSize Mod 256) & ChrB((PicDataSize / 256) Mod 256) & ChrB((PicDataSize/ 256 / 256) Mod 256) & ChrB(PicDataSize / 256 / 256 / 256) & _
ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & _
ChrB(18) & ChrB(11) & ChrB(0) & ChrB(0) & _
ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0) & _
ChrB(0) & ChrB(0) & ChrB(0) & ChrB(0)

' 逐点输出位图阵列
If nColorHue = -1 Then
CurColorHue = Int(Rnd * 360)
ElseIf nColorHue = -2 Then
CurColorHue = 0
Else
CurColorHue = nColorHue
End If
For j = 0 To nPixelHeight - 1
For i = 0 To Len(DigtalStr) * nPixelWidth - 1
If nColorHue = -2 Then
Response.BinaryWrite HSBToRGB(CurColorHue, 0, 100 -Buf(nPixelHeight - 1 - j, i))
Else
Response.BinaryWrite HSBToRGB(CurColorHue, Buf(nPixelHeight - 1 -j, i), 100)
End If
Next
Next
End Sub
'调用方法:<img src="数字验证码.asp">
%>

///////////////////////////////////////////////////////////////////////////////////////////////////////////

gif图片验证码

----------------------------

<%
'--------------------------------------
'Fly38.com - VerifyCode Class
' gif 验证码生成类
' 原作者:NetRube
'--------------------------------------
Class Fly38_VerifyCode
Public GlobalColorTable(), LocalColorTable()
Public TransparentColorIndex, UseTransparency
Public GIF89a
Public Comment

Private FGroundColorIndex, BGroundColorIndex
Private Image
Private GlobalColorTableSize, GlobalColorTableFlag, LocalColorTableSize, LocalColorTableFlag
Private Width_, Height_
Private LeftPosition, TopPosition
Private Bits, ColorResolution, CodeSize
Private PixelAspectRatio
Private SortFlag, InterlaceFlag
Private Seperator, GraphicControl, EndOfImage
Private Reserved

Private Font
Private Letter(19)

Private Sub Class_Initialize()
Image = ""

GIF89a = False

ReDim GlobalColorTable(256)
GlobalColorTableSize = 7
GlobalColorTableFlag = True

GlobalColorTable(2) = RGB(255, 0, 0)
GlobalColorTable(3) = RGB(0, 255, 0)
GlobalColorTable(4) = RGB(0, 0, 255)
GlobalColorTable(5) = RGB(255, 255, 0)
GlobalColorTable(6) = RGB(0, 255, 255)
GlobalColorTable(7) = RGB(255, 0, 255)

ReDim LocalColorTable(0)
LocalColorTableSize = 0
LocalColorTableFlag = False

ColorResolution = 7
Bits = 7
CodeSize = 7

BGroundColorIndex = 0
FGroundColorIndex = 1
TransparentColorIndex = 0
UseTransparency = False

LeftPosition = 0
TopPosition = 0
Width_ = 20
Height_ = 20

Clear

PixelAspectRatio = 0
SortFlag = False
InterlaceFlag = False
Seperator = Asc(",")
GraphicControl = Asc("!")
EndOfImage = Asc(";")

Comment = ""

Reserved = 0

Set Font = Server.CreateObject("Scripting.Dictionary")

Letter(0) = "00000000000000"
Letter(1) = "00001111100000"
Letter(2) = "00011111110000"
Letter(3) = "00111000111000"
Letter(4) = "00110000011100"
Letter(5) = "01110000001100"
Letter(6) = "01100000001110"
Letter(7) = "01100000001110"
Letter(8) = "11100000001110"
Letter(9) = "11000000001110"
Letter(10) = "11000000001110"
Letter(11) = "11100000001110"
Letter(12) = "11100000001100"
Letter(13) = "11100000001100"
Letter(14) = "01100000001100"
Letter(15) = "01110000011100"
Letter(15) = "00111000011000"
Letter(16) = "00011111110000"
Letter(17) = "00001111100000"
Letter(18) = "00000000000000"
Font.Add "0", Letter

Letter(0) = "00000000000000"
Letter(1) = "00000001110000"
Letter(2) = "00000001110000"
Letter(3) = "00000011100000"
Letter(4) = "00000011000000"
Letter(5) = "00000011000000"
Letter(6) = "00000011000000"
Letter(7) = "00000111000000"
Letter(8) = "00000111000000"
Letter(9) = "00000111000000"
Letter(10) = "00000110000000"
Letter(11) = "00000110000000"
Letter(12) = "00000110000000"
Letter(13) = "00000110000000"
Letter(14) = "00000110000000"
Letter(15) = "00000110000000"
Letter(15) = "00000110000000"
Letter(16) = "00000110000000"
Letter(17) = "00000010000000"
Letter(18) = "00000000000000"
Font.Add "1", Letter

Letter(0) = "00000000000000"
Letter(1) = "00001111110000"
Letter(2) = "00011111111000"
Letter(3) = "00111000011100"
Letter(4) = "01110000011100"
Letter(5) = "01110000011000"
Letter(6) = "01100000011000"
Letter(7) = "00000000111000"
Letter(8) = "00000001110000"
Letter(9) = "00000001110000"
Letter(10) = "00000011000000"
Letter(11) = "00000111000000"
Letter(12) = "00001110000000"
Letter(13) = "00011000000000"
Letter(14) = "00011000000000"
Letter(15) = "00110000011100"
Letter(16) = "01101111111100"
Letter(17) = "01111111111110"
Letter(18) = "01111100000000"
Letter(19) = "00000000000000"
Font.Add "2", Letter

Letter(0) = "00000000000000"
Letter(1) = "00001111111000"
Letter(2) = "00111111111000"
Letter(3) = "01110000111100"
Letter(4) = "01100000011000"
Letter(5) = "01000000111000"
Letter(6) = "00000000111000"
Letter(7) = "00000001110000"
Letter(8) = "00000011000000"
Letter(9) = "00000111110000"
Letter(10) = "00000100111000"
Letter(11) = "00000000011100"
Letter(12) = "00000000011100"
Letter(13) = "00000000011100"
Letter(14) = "00000000011100"
Letter(15) = "00000000011000"
Letter(16) = "11100000111000"
Letter(17) = "11111111110000"
Letter(18) = "01111111100000"
Letter(19) = "00000000000000"
Font.Add "3", Letter

Letter(0) = "00000000000000"
Letter(1) = "00000000111000"
Letter(2) = "00000001111000"
Letter(3) = "00000011100000"
Letter(4) = "00000111011100"
Letter(5) = "00001110011100"
Letter(6) = "00001100011000"
Letter(7) = "00011000111000"
Letter(8) = "00111000110000"
Letter(9) = "01110000110000"
Letter(10) = "01100000110000"
Letter(11) = "01100000110000"
Letter(12) = "11000111111110"
Letter(13) = "11111111111100"
Letter(14) = "11111111100000"
Letter(15) = "11100001100000"
Letter(16) = "00000001110000"
Letter(17) = "00000000110000"
Letter(18) = "00000000110000"
Letter(19) = "00000000100000"
Font.Add "4", Letter

Letter(0) = "00000000000000"
Letter(1) = "00001100000100"
Letter(2) = "00011111111110"
Letter(3) = "00011111111100"
Letter(4) = "00011110000000"
Letter(5) = "00011000000000"
Letter(6) = "00111000000000"
Letter(7) = "00111000000000"
Letter(8) = "00111111110000"
Letter(9) = "00111111111000"
Letter(10) = "00000000011000"
Letter(11) = "00000000011000"
Letter(12) = "00000000011000"
Letter(13) = "00000000011000"
Letter(14) = "00000000011000"
Letter(15) = "00000000011000"
Letter(16) = "00000001111000"
Letter(17) = "01111111110000"
Letter(18) = "00111111000000"
Letter(19) = "00000000100000"
Font.Add "5", Letter

Letter(0) = "00000000000000"
Letter(1) = "00000011110000"
Letter(2) = "00000111100000"
Letter(3) = "00001110000000"
Letter(4) = "00011100000000"
Letter(5) = "00111000000000"
Letter(6) = "00110000000000"
Letter(7) = "00110000000000"
Letter(8) = "01111111110000"
Letter(9) = "01111111111000"
Letter(10) = "01110000011100"
Letter(11) = "01100000001100"
Letter(12) = "01100000001100"
Letter(13) = "01100000001100"
Letter(14) = "01100000001100"
Letter(15) = "01110000011100"
Letter(16) = "00110000011100"
Letter(17) = "00111111111000"
Letter(18) = "00011111110000"
Letter(19) = "00000000000000"
Font.Add "6", Letter

Letter(0) = "00000000000000"
Letter(1) = "00100111111110"
Letter(2) = "01111111111100"
Letter(3) = "01111110011100"
Letter(4) = "00000000011000"
Letter(5) = "00000000111000"
Letter(6) = "00000000110000"
Letter(7) = "00000000110000"
Letter(8) = "00000000110000"
Letter(9) = "00000001110000"
Letter(10) = "00000001100000"
Letter(11) = "00000001100000"
Letter(12) = "00000001100000"
Letter(13) = "00000001100000"
Letter(14) = "00000011100000"
Letter(15) = "00000011100000"
Letter(16) = "00000011100000"
Letter(17) = "00000001000000"
Letter(18) = "00000001000000"
Letter(19) = "00000000000000"
Font.Add "7", Letter

Letter(0) = "00000000000000"
Letter(1) = "00001111110000"
Letter(2) = "00011111111000"
Letter(3) = "00111000011000"
Letter(4) = "00110000011000"
Letter(5) = "01110000011100"
Letter(6) = "01110000011000"
Letter(7) = "00110000011000"
Letter(8) = "00111101111000"
Letter(9) = "00011111111000"
Letter(10) = "00111000111100"
Letter(11) = "01110000001100"
Letter(12) = "01110000001100"
Letter(13) = "01100000001110"
Letter(14) = "01100000001100"
Letter(15) = "01100000001100"
Letter(16) = "01110000011100"
Letter(17) = "00111111111100"
Letter(18) = "00011111110000"
Letter(19) = "00000000000000"
Font.Add "8", Letter

Letter(0) = "00000000000000"
Letter(1) = "00011111110000"
Letter(2) = "00111111111000"
Letter(3) = "01110000111000"
Letter(4) = "01110000011100"
Letter(5) = "01100000001100"
Letter(6) = "01100000001100"
Letter(7) = "01100000001100"
Letter(8) = "01100000001100"
Letter(9) = "01110000011100"
Letter(10) = "00111111111100"
Letter(11) = "00011111111100"
Letter(12) = "00000000011000"
Letter(13) = "00000000011000"
Letter(14) = "00000000111000"
Letter(15) = "00000001110000"
Letter(16) = "00000011100000"
Letter(17) = "00000111000000"
Letter(18) = "00011110000000"
Letter(19) = "00000000000000"
Font.Add "9", Letter
End Sub
Private Sub Class_Terminate()
Font.RemoveAll
Set Font = Nothing
End Sub

Public Property Get Width()
Width = Width_
End Property

Public Property Get Height()
Height = Height_
End Property

Public Property Get Version()
Version = "NetRube VerifyCode Class 1.0 Build 20041225"
End Property

Public Property Let BGroundColor(ByVal Color)
GlobalColorTable(0) = MakeColor(Color)
BGroundColorIndex = 0
End Property

Public Property Let FGroundColor(ByVal Color)
GlobalColorTable(1) = MakeColor(Color)
FGroundColorIndex = 1
End Property

Public Property Get Pixel(ByVal PX, ByVal PY)
If (PX > 0 And PX <= Width_) And (PY > 0 And PY <= Height_) Then
Pixel = AscB(MidB(Image, (Width_ * (PY - 1)) + PX, 1))
Else
Pixel = 0
End If
End Property

Public Property Let Pixel(ByVal PX, ByVal PY, PValue)
Dim Offset

PX = Int(PX)
PY = Int(PY)
PValue = Int(PValue)

Offset = Width_ * (PY - 1)

If (PX > 0 And PX <= Width_) And (PY > 0 And PY <= Height_) Then
Image = LeftB(Image, Offset + (PX - 1)) & ChrB(PValue) & RightB(Image, LenB(Image) - (Offset + PX))
End If
End Property

Public Sub Clear()
Image = String(Width_ * (Height_ + 1) / 2, ChrB(BGroundColorIndex) & ChrB(BGroundColorIndex))
End Sub

Public Sub Resize(ByVal NewWidth, ByVal NewHeight, RPreserve)
Dim OldImage, OldWidth, OldHeight
Dim CopyWidth, CopyHeight
Dim X, Y

If RPreserve Then
OldImage = Image
OldWidth = Width_
OldHeight = Height_
End If

Width_ = NewWidth
Height_ = NewHeight

Clear

If RPreserve Then
If NewWidth > OldWidth Then CopyWidth = OldWidth Else CopyWidth = NewWidth
If NewHeight > OldHeight Then CopyHeight = OldHeight Else CopyHeight = NewHeight

Width_ = NewWidth
Height_ = NewHeight

For Y = 1 To CopyHeight
For X = 1 To CopyWidth
Pixel(X, Y) = AscB(MidB(OldImage, (OldWidth * (Y - 1)) + X, 1))
Next
Next
End If
End Sub

Private Function ShiftLeft(SLValue, SLBits)
ShiftLeft = SLValue * (2 ^ SLBits)
End Function

Private Function ShiftRight(SRValue, SRBits)
ShiftRight = Int(SRValue / (2 ^ SRBits))
End Function

Private Function Low(LValue)
Low = LValue And &HFF
End Function

Private Function High(HValue)
High = ShiftRight(HValue, 8)
End Function

Private Function Blue(BValue)
Blue = Low(ShiftRight(BValue, 16))
End Function

Private Function Green(GValue)
Green = Low(ShiftRight(GValue, 8))
End Function

Private Function Red(RValue)
Red = Low(RValue)
End Function

Private Function MakeColor(MCValue)
MakeColor = CLng("&H" & Right(MCValue, 2) & Mid(MCValue, 4, 2) & Mid(MCValue, 2, 2))
End Function

Private Function GetWord(GWValue)
GetWord = ShiftLeft(AscB(RightB(GWValue, 1)), 8) Or AscB(LeftB(GWValue, 1))
End Function

Private Function MakeWord(MWValue)
MakeWord = ChrB(Low(MWValue)) & ChrB(High(MWValue))
End Function

Private Function MakeByte(MBValue)
MakeByte = ChrB(Low(MBValue))
End Function

Private Function UncompressedData()
Dim ClearCode, ChunkMax, EndOfStream
Dim UDData, UD, U

UncompressedData = ""

ClearCode = 2 ^ Bits
ChunkMax = 2 ^ Bits - 2
EndOfStream = ClearCode + 1

UDData = ""

For U = 1 To LenB(Image) Step ChunkMax
UDData = UDData & MidB(Image, U, ChunkMax) & ChrB(ClearCode)
Next

For U = 1 To LenB(UDData) Step &HFF
UD = MidB(UDData, U, &HFF)
UncompressedData = UncompressedData & MakeByte(LenB(UD)) & UD
Next

UncompressedData = UncompressedData & MakeByte(&H00)
UncompressedData = UncompressedData & MakeByte(EndOfStream)
End Function

Private Function GetGColorTable()
Dim GGCT

GetGColorTable = ""

For GGCT = 0 To UBound(GlobalColorTable) - 1
GetGColorTable = GetGColorTable & MakeByte(Red(GlobalColorTable(GGCT)))
GetGColorTable = GetGColorTable & MakeByte(Green(GlobalColorTable(GGCT)))
GetGColorTable = GetGColorTable & MakeByte(Blue(GlobalColorTable(GGCT)))
Next
End Function

Private Function GetLColorTable()
Dim GLCT

GetLColorTable = ""

For GLCT = 0 To UBound(LocalColorTable) - 1
GetLColorTable = GetLColorTable & MakeByte(Red(LocalColorTable(GLCT)))
GetLColorTable = GetLColorTable & MakeByte(Green(LocalColorTable(GLCT)))
GetLColorTable = GetLColorTable & MakeByte(Blue(LocalColorTable(GLCT)))
Next
End Function

Private Function GlobalDescriptor()
GlobalDescriptor = 0

If GlobalColorTableFlag Then GlobalDescriptor = GlobalDescriptor Or ShiftLeft(1, 7)
GlobalDescriptor = GlobalDescriptor Or ShiftLeft(ColorResolution, 7)
If SortFlag Then GlobalDescriptor = GlobalDescriptor Or ShiftLeft(1, 3)
GlobalDescriptor = GlobalDescriptor Or GlobalColorTableSize
End Function

Private Function LocalDescriptor()
LocalDescriptor = 0

If LocalColorTableFlag Then LocalDescriptor = LocalDescriptor Or ShiftLeft(1, 7)
If InterlaceFlag Then LocalDescriptor = LocalDescriptor Or ShiftLeft(1, 6)
If SortFlag Then LocalDescriptor = LocallDescriptor Or ShiftLeft(1, 5)
LocalDescriptor = LocalDescriptor Or ShiftLeft(Reserved, 3)
LocalDescriptor = LocalDescriptor Or LocalColorTableSize
End Function

Private Property Get ImageData()
Dim Text, I

ImageData = GIFHeader
ImageData = ImageData & MakeWord(Width_)
ImageData = ImageData & MakeWord(Height_)
ImageData = ImageData & MakeByte(GlobalDescriptor)
ImageData = ImageData & MakeByte(BGroundColorIndex)
ImageData = ImageData & MakeByte(PixelAspectRatio)
ImageData = ImageData & GetGColorTable

If GIF89a Then
If UseTransparency Then
ImageData = ImageData & MakeByte(GraphicControl)
ImageData = ImageData & MakeByte(&HF9)
ImageData = ImageData & MakeByte(&H04)
ImageData = ImageData & MakeByte(&H01)
ImageData = ImageData & MakeByte(&H00)
ImageData = ImageData & MakeByte(TransparentColorIndex)
ImageData = ImageData & MakeByte(&H00)
End If

If Comment <> "" Then
ImageData = ImageData & MakeByte(GraphicControl)
ImageData = ImageData & MakeByte(&HFE)
Text = Left(Comment, &HFF)
ImageData = ImageData & MakeByte(Len(Text))
For I = 1 To Len(Text)
ImageData = ImageData & MakeByte(Asc(Mid(Text, I, 1)))
Next
ImageData = ImageData & MakeByte(&H00)
End If
End If

ImageData = ImageData & MakeByte(Seperator)
ImageData = ImageData & MakeWord(LeftPosition)
ImageData = ImageData & MakeWord(TopPosition)
ImageData = ImageData & MakeWord(Width_)
ImageData = ImageData & MakeWord(Height_)
ImageData = ImageData & MakeByte(LocalDescriptor)
ImageData = ImageData & MakeByte(CodeSize)
ImageData = ImageData & UncompressedData
ImageData = ImageData & MakeByte(&H00)
ImageData = ImageData & MakeByte(EndOfImage)
End Property

Public Sub ImgWrite()
Response.ContentType = "image/gif"
Response.BinaryWrite ImageData
End Sub

Private Function GIFHeader()
GIFHeader = ""
GIFHeader = GIFHeader & ChrB(Asc("G"))
GIFHeader = GIFHeader & ChrB(Asc("I"))
GIFHeader = GIFHeader & ChrB(Asc("F"))
GIFHeader = GIFHeader & ChrB(Asc("8"))
If GIF89a Then
GIFHeader = GIFHeader & ChrB(Asc("9"))
Else
GIFHeader = GIFHeader & ChrB(Asc("7"))
End If
GIFHeader = GIFHeader & ChrB(Asc("a"))
End Function

Public Sub VerifyCode(Text, VCColor)
Dim I1, I2, I3
Dim VCX, VCY, VCIndex

Resize 14 * Len(Text) + 10, UBound(Letter) + 10, False

Randomize
VCX = Int(Rnd * 10)
VCY = Int(Rnd * (Height_ - UBound(Letter)))

For I1 = 0 To UBound(Letter) - 1
For I2 = 1 To Len(Text)
For I3 = 1 To Len(Font(Mid(Text, I2, 1))(I1))
VCIndex = CLng(Mid(Font(Mid(Text, I2, 1))(I1), I3, 1))

If VCIndex <> 0 Then
If VCColor Then
Randomize
VCIndex = Int(Rnd * 7)
End If

Pixel(VCX + ((I2 - 1) * Len(Letter(0))) + I3, VCY + I1) = VCIndex
End If
Next
Next
Next
End Sub

Public Sub Noises(Amount, NColor)
Dim NI, NIndex

For NI = 1 To Amount
NIndex = 1

If NColor Then
Randomize
NIndex = Int(Rnd * 7)
End If

Pixel(Int(Rnd * Width_), Int(Rnd * Height_)) = NIndex
Next
End Sub

End Class
%>


<%
'用法
Dim SessionName
SessionName = "yanzhengcode" '指定一个session变量名

Call ShowCode(SessionName) '调用过程

Sub ShowCode(CodeName)
Set img = New fly38_VerifyCode
Randomize
Dim code
code = Int(Rnd * 9000 + 1000)
Session(CodeName) = code
img.BGroundColor = "#FFFFFF" ' 图片背景颜色
img.FGroundColor = "#FF0000" ' 前景(文本)颜色
Call img.VerifyCode(code, False) ' 处理验证码,第二个参数为是否显示彩色文本
Call img.Noises(100, True) ' 添加杂点,第一个参数为杂点数量,第二个参数为是否显示彩色杂点
img.ImgWrite ' 输出图片
End Sub
%>

-----------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics