反SPAM新思路—换Z-BLOG的验证码!

2019-01-13 19:40:43王振洲

Rem 生成干扰线
Sub makeNoise(ByRef nl,imgW,UnitHeight)
    Dim i,l,x1,y1,x2,y2,dx,dy,deltaT
    x1 = Int(Rnd*imgW)
    y1 = Int(Rnd*UnitHeight)
    x2 = Int(Rnd*imgW)
    y2 = Int(Rnd*UnitHeight)
    dx = X2 - X1
    dy = Y2 - Y1
    If Abs(dx) > Abs(dy) Then deltaT = Abs(dx) Else deltaT = Abs(dy)
    If deltaT = 0 Then Exit Sub
    l = UBound(nl,2)
    ReDim Preserve nl(1,l+deltaT+1)
    l = l + 1
    For i = 0 To deltaT
        nl(0,l+i) = x1 + dx * i  deltaT
        nl(1,l+i) = y1 + dy * i  deltaT
    Next
End Sub

Rem 判断是否为干扰线上的点
Function onNoiseLine(ByRef nl,x,y)
    onNoiseLine = False
    Dim i
    For i=0 To UBound(nl,2)
        If x = nl(0,i) And y = nl(1,i) Then
            onNoiseLine = True
            Exit For
        End If
    Next
End Function

Rem 对单个字的点阵进行干扰
Rem 干扰思想:在点阵范围内随机产生2个端点,进行连线,以位移较大的一方做横轴,先将连线上的点删除,再将被删除点的纵轴方向上方或下方的点(随机确定)移向被删除点,移动后的空白用背景色补充
Function pcd_doubter(ByVal str,UnitWidth,UnitHeight,DotsLimit,tryCount,dbtTimes)
    Randomize
    Dim x1,x2,y1,y2,dx,dy,deltaT,i,ii,way,f1,f2
    For f1=1 To dbtTimes    '干扰次数
        For f2=1 To tryCount    '避免删除有效点超过上限的尝试次数限制
            '随机确定2个端点
            x1 = int(Rnd*UnitWidth)
            x2 = int(Rnd*UnitWidth)
            y1 = int(Rnd*UnitHeight)
            y2 = int(Rnd*UnitHeight)
            dx = X2 - X1
            dy = Y2 - Y1
            If Abs(dx) > Abs(dy) Then deltaT = Abs(dx) Else deltaT = Abs(dy)
            ReDim ary(1,deltaT)    '存储连线的点
            If deltaT = 0 Then
                ary(0,0) = x1
                ary(1,0) = y1
            Else
                ii = 0
                For i = 0 To deltaT
                    ary(0,i) = x1 + dx * i  deltaT
                    ary(1,i) = y1 + dy * i  deltaT
                    If pcd_getDot(ary(0,i),ary(1,i),str,UnitWidth) = "0" Then ii = ii + 1
                Next
                ' 统计连线上有效点的数量,如未超过有效点上限则跳出循环,执行干扰
                If ii <= DotsLimit Then Exit For
            End If
        Next