?? dvcode.asp
字號:
<%
Rem 動網論壇驗證碼程序改進版
Rem 使用時,請將 Call CreatValidCode("CSName") 中的CSName更改為實際的名稱
Rem 改動:captchio http://www.captchio.com qq:58978386
Rem 2006-3-9 修正了一個可能除0的錯誤
Rem 2006-7-31 字符個數隨機,寬度、高度、位置隨機,加入干擾線
Rem 2007-7-16 修正圖像寬度非4整數倍時的錯誤,干擾線可設置加粗,加入字符粘連
Rem 2007-7-18 來路判斷,允許多頁面同時產生驗證碼而不沖突
Rem ---------------------
Option Explicit
Rem 設置部分
Const nCodeCount = 5 ' 允許保存的驗證碼數量 (原始值 5)
Const nSaturation = 100 ' 色彩飽和度 (100)
Const nBlankNoisyDotOdds = 0.0 ' 空白處噪點率 (0.0)
Const nColorNoisyDotOdds = 0.0 ' 有色處噪點率 (0.0)
Const bNoisyLineBold = False ' 干擾線是否加粗 (False)
Const nNoisyLineCount = 1 ' 噪音線條數 (1)
Const nJoinOdds = 0.5 ' 字符粘連的幾率 (0.5)
Const nCharMin = 3 ' 產生的最小字符個數 (3)
Const nCharMax = 4 ' 產生的最大字符個數 (4)
Const nSpaceX = 2 ' 左右兩邊的空白寬度 (2)
Const nSpaceY = 2 ' 上下兩邊的空白寬度 (2)
Const nImgWidth = 60 ' 數據區寬度 (60)
Const nImgHeight = 16 ' 數據區高度 (16)
Const nCharWidthRandom = 16 ' 字符寬度隨機量 (16)
Const nCharHeightRandom = 16 ' 字符高度隨機量 (16)
Const nPositionXRandom = 10 ' 橫向位置隨機量 (10)
Const nPositionYRandom = 10 ' 縱向位置隨機量 (10)
Const nAngleRandom = 6 ' 筆畫角度隨機量 (6)
Const nLengthRandom = 6 ' 筆畫長度隨機量(百分比) (6)
Const nColorHue = -2 ' 顯示驗證碼的色調(-1表示隨機色調, -2表示灰度色調) (-2)
Const cCharSet = "0123456789"
' 構成驗證碼的字符集
' 如果擴充了下邊的字母矢量庫,則可以相應擴充這個字符集
Rem ---------------------
Randomize
Dim Buf(), DigtalStr, Lasts()
Dim Lines(), LineCount
Dim CursorX, CursorY, DirX, DirY, nCharCount, nPixelWidth, nPixelHeight, PicWidth, PicHeight
nCharCount = nCharMin + CInt(Rnd * (nCharMax - nCharMin))
PicWidth = nImgWidth + nSpaceX + nSpaceX
DirX = PicWidth Mod 4
If DirX <> 0 Then PicWidth = PicWidth + (4-DirX)
PicHeight = nImgHeight + nSpaceY + nSpaceY
Call CreatValidCode("CSName")
Sub CDGen_Reset()
' 復位矢量筆和環境變量
LineCount = 0
CursorX = 0
CursorY = 0
' 初始的光筆方向是垂直向下
DirX = 0
DirY = 1
End Sub
Sub CDGen_Clear()
' 清空位圖陣列
Dim i, j
ReDim Buf(PicHeight - 1, PicWidth - 1)
ReDim Lasts(PicHeight - 1)
For j = 0 To PicHeight - 1
For i = 0 To PicWidth - 1
Buf(j, i) = 0
Next
Next
For j = 0 To PicHeight - 1
Lasts(j) = 0
Next
End Sub
Sub CDGen_PSet(X, Y)
' 在位圖陣列上畫點
If X >= 0 And X < PicWidth And Y >= 0 And Y < PicHeight Then
Buf(Y, X) = 1
If X > Lasts(Y) Then Lasts(Y) = X
End If
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)
If DeltaT = 0 Then
CDGen_PSet CInt(X1),CInt(Y1)
Else
For i = 0 To DeltaT
CDGen_PSet CInt(X1 + DX * i / DeltaT), CInt(Y1 + DY * i / DeltaT)
Next
End If
End Sub
Function CDGen_LineCheck(X1, Y1, X2, Y2)
' 在位圖陣列上進行檢測,返回距離最小值
Dim DX, DY, DeltaT, x, y, i, n, j, v
v = 0
DX = X2 - X1
DY = Y2 - Y1
If Abs(DX) > Abs(DY) Then DeltaT = Abs(DX) Else DeltaT = Abs(DY)
If DeltaT = 0 Then
x = CInt(X1)
y = CInt(Y1)
If x >= 0 And x < PicWidth And y >= 0 And y < PicHeight Then
v = x - Lasts(y)
End If
Else
j = PicWidth
For i = 0 To DeltaT
x = CInt(X1 + DX * i / DeltaT)
y = CInt(Y1 + DY * i / DeltaT)
If x >= 0 And x < PicWidth And y >= 0 And y < PicHeight Then
n = x - Lasts(y)
If n < j Then
j = n
If n <= 2 Then Exit For
End If
End If
Next
v = j
End If
CDGen_LineCheck = v
End Function
Sub CDGen_FowardDraw(nLength)
' 按當前光筆方向繪制指定長度并移動光筆,正數表示從左向右/從上向下繪制,負數表示從右向左/從下向上繪制
nLength = nLength * (1 + (Rnd * 2 - 1) * nLengthRandom / 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 = (nAngle + (Rnd * 2 - 1) * nAngleRandom) / 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) + DeltaX * nPercent / 100
CursorY = Lines(1, nActionIndex) + 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, n, joinX
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)
Lines(1, i) = Round((Lines(1, i) - MaxY + OffsetY) * StepY, 0)
Lines(2, i) = Round((Lines(2, i) - MinX + OffsetX) * StepX, 0)
Lines(3, i) = Round((Lines(3, i) - MinY + OffsetY) * StepY, 0)
Next
If (Rnd >= nJoinOdds) or (Len(DigtalStr)<2) Then
joinX = 0
Else
joinX = PicWidth
For i = 0 To LineCount - 1
n = CDGen_LineCheck(Lines(0, i) + X0 + 1, Lines(1, i) + Y0 + 1, Lines(2, i) + X0 + 1, Lines(3, i) + Y0 + 1)
If n < joinX Then
joinX = n
If n <= 2 Then Exit For
End If
Next
If (joinX <= 2) or (joinX = PicWidth) Then
joinX = 0
Else
joinX = joinX - 2
End If
End If
X0 = X0 + 1 - joinX
For i = 0 To LineCount - 1
CDGen_Line Lines(0, i) + X0, Lines(1, i) + Y0 + 1, Lines(2, i) + X0, 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度(相對于垂直線)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -