当前位置:首页 > 代码 > 正文

asp图片验证码源代码(jsp生成图片验证码)

admin 发布:2022-12-19 19:40 118


本篇文章给大家谈谈asp图片验证码源代码,以及jsp生成图片验证码对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用asp怎么编写验证码?

使用ASP生成图片彩色校验码

49行代码,三个文件

Asp文件:Code.Asp

数据文件:body.Fix , Head.Fix

用法:img src="code.asp"

%

Option Explicit

Response.buffer=true

NumCode

Function NumCode()

Response.Expires = -1

Response.AddHeader "Pragma","no-cache"

Response.AddHeader "cache-ctrol","no-cache"

dim zNum,i,j

dim Ados,Ados1

Randomize timer

zNum = cint(8999*Rnd+1000)

Session("GetCode") = zNum

dim zimg(4),NStr

NStr=cstr(zNum)

For i=0 to 3

zimg(i)=cint(mid(NStr,i+1,1))

Next

dim Pos

set Ados=Server.CreateObject("Adodb.Stream")

Ados.Mode=3

Ados.Type=1

Ados.Open

set Ados1=Server.CreateObject("Adodb.Stream")

Ados1.Mode=3

Ados1.Type=1

Ados1.Open

Ados.LoadFromFile(Server.mappath("body.Fix"))

Ados1.write Ados.read(1280)

for i=0 to 3

Ados.Position=(9-zimg(i))*320

Ados1.Position=i*320

Ados1.write ados.read(320)

next

Ados.LoadFromFile(Server.mappath("head.fix"))

Pos=lenb(Ados.read())

Ados.Position=Pos

for i=0 to 9 step 1

for j=0 to 3

Ados1.Position=i*32+j*320

Ados.Position=Pos+30*j+i*120

Ados.write ados1.read(30)

next

next

Response.ContentType = "image/BMP"

Ados.Position=0

Response.BinaryWrite Ados.read()

Ados.Close:set Ados=nothing

Ados1.Close:set Ados1=nothing

End Function

' Asp code Created by BlueIdea.COM Web Team V37 2003-7-25

%

代码解释:

Response.buffer=true

NumCode

Function NumCode()

Response.Expires = -1

Response.AddHeader "Pragma","no-cache"

Response.AddHeader "cache-ctrol","no-cache"

dim zNum,i,j

dim Ados,Ados1

Randomize timer

生成随机四位数字:

zNum = cint(8999*Rnd+1000)

传递给session

Session("GetCode") = zNum

该for循环是将随机数字放入一个下标3的数组,便于提供给后面的阵列变换

dim zimg(3),NStr

NStr=cstr(zNum)

For i=0 to 3

zimg(i)=cint(mid(NStr,i+1,1))

Next

dim Pos

'定义二个 ADODB.Stream binary对象,作图像数据操作之用:

set Ados=Server.CreateObject("Adodb.Stream")

Ados.Mode=3

Ados.Type=1

Ados.Open

set Ados1=Server.CreateObject("Adodb.Stream")

Ados1.Mode=3

Ados1.Type=1

Ados1.Open

'载入0~9的数字数据10x100的,Gbr的阵列数据,每个320字节,10个数字3200byte

'BGR一个点,10x10个点一个数字,一个点三个字节(二进制8位,16进制 00~FF)

'一行10个点 30字节 + 行结束标记 00 00 二字节 32字节,所以一个10x100宽小于长的图像每个数字10x10是320字节

'长大于宽的则无行结束标记 0000,直接是300字节

这些就是BMP 24bit的数据详细信息了

‘至于头部,也很简单,包含长宽,图像开始标记等等~~才54字节,远没jpg什么的复杂

Ados.LoadFromFile(Server.mappath("body.Fix"))

Ados1.write Ados.read(1280)

'第一个for循环,按生成的随机数字顺序从 10X100的数字阵列中提取出相应的四个数字

但是竖排的数字阵列

for i=0 to 3

Ados.Position=(9-zimg(i))*320

Ados1.Position=i*320

Ados1.write ados.read(320)

next

'清空已经用完的ADOS的数据,调入替换新的图像头54字节的头文件

Ados.LoadFromFile(Server.mappath("head.fix"))

Pos=lenb(Ados.read())

Ados.Position=Pos '指定Pos位置,即可再偏移54字节的位置添加图形数据

第二个for循环,进行数字的阵列变换,由竖排的块转换为横排的数字块

方法是隔320字节抽取4次30字节写入ados对象,再抽取偏移第二行的图像数据

30字节是因为bmp 宽大于长时无00 00的行结束标记

for i=0 to 9 step 1

for j=0 to 3

Ados1.Position=i*32+j*320

Ados.Position=Pos+30*j+i*120

Ados.write ados1.read(30)

next

next

Ados.Position=0

response.BinaryWrite直接向客户端发送图像数据

Response.ContentType = "image/BMP"

Response.BinaryWrite Ados.read()

Ados.Close:set Ados=nothing

Ados1.Close:set Ados1=nothing

End Function

数字和字母组合并生成图片的验证码祥解

大家经常在网上登陆的时候经常会看到让你输入验证码,有的是文字的,有的呢是图片,比如chinaren.com校友录中留言的时候,我们就会看到数字图片验证码;网上关于数字文字验证码实现方法的相关资料很多,而我们这里介绍的是数字和字母随机组成的并且生成图片的验证码的实现方法。看起来很复杂、其实很简单的,大家跟着我往下看:

首先,我们先介绍一下设计思路,数字和字母的随机组合生成验证码,然后将验证码生成图片,这里“数字和字母的组合”应该是随机取出来的;如果是专门的数字验证码,我们可以这样实现:

ycodenum=4 '验证码的位数,或者说成个数

for i=1 to ycodenum

Randomize '初始化随机数发生器

ycode=ycodeInt((9*Rnd)) 'rnd是随机数,从0到1之间的任意实数,这里获得0到9之间的整数

next

response.write ycode '就可以输出数字验证码(4位)

然而,我们要让数字和字母同样随机生成,这里我们可以用到数组来实现这种效果,如下:

ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" '将数字和大写字母组成一个字符串

yc=split(char,",") '将字符串生成数组

ycodenum=4

for i=1 to ycodenum

Randomize

ycode=ycodeyc(Int((35*Rnd))) '数组一般从0开始读取,所以这里为35*Rnd

next

response.write ycode

现在看看输出结果是不是数字和字母随机组合的呢?

下面看看怎样生成图片,这个也许有些朋友知道:asp不能生成图片,必须使用asp组件。不错,我们这里使用的是ASP图象组件shotgraph。有一点大家注意,服务器不是自己的不能用哦,因为你装不了这组件。

组件的下载地址:,至于怎么注册,这里就不多说了,网上有很多资料

我们看看生成图片的代码:

ychar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z" '将数字和大写字母组成一个字符串

yc=split(char,",") '将字符串生成数组

ycodenum=4

for i=1 to ycodenum

Randomize

ycode=ycodeyc(Int((35*Rnd))) '数组一般从0开始读取,所以这里为35*Rnd

next

Response.Clear

Response.ContentType="image/gif"

set obj=Server.CreateObject("shotgraph.image")

x=55 '图片的宽

y=26 '图片的高

obj.CreateImage x,y,8 '8是图片的颜色8位

obj.SetColor 0,55,126,222

obj.SetColor 1,255,255,255

obj.CreatePen "PS_SOLID",1,0

obj.SetBgColor 0

obj.Rectangle 0,0,x-1,y-1

obj.SetBkMode "TRANSPARENT"

obj.CreateFont "Arial",136,18,1,False,False,False,False

obj.SetTextColor 1

obj.TextOut 5,4,ycode" "

img=obj.GifImage(-1,1,"")

Response.BinaryWrite (img)

针对以上代码也就是说shotgraph普通的画图的原理请参考:

ASP的验证码代码怎么写?

因为字符限制原因,不好粘贴代码,生成验证码的程序粘贴不上来,你把邮箱告诉我,我发你邮件.

以下是一个完整的验证码生成程序,生成的是一张图片.

然后你在其他页面里调用这张图片就可以了.

调用程序如下:

td colspan=2 span class="new"验证码:/span

input name="verifycode" type=text id="verifycode"img src=verifycode.asp/td

验证码生成程序粘贴不了.

你把以下地址的程序存成verifycode.asp就可以了

%

Option Explicit

Response.buffer=true

NumCode

Function NumCode()

Response.Expires = -1

Response.AddHeader "Pragma","no-cache"

Response.AddHeader "cache-ctrol","no-cache"

dim zNum,i,j

dim Ados,Ados1

Randomize timer

zNum = cint(8999*Rnd+1000)

Session("verifycode") = zNum

dim zimg(4),NStr

NStr=cstr(zNum)

For i=0 to 3

zimg(i)=cint(mid(NStr,i+1,1))

Next

dim Pos

set Ados=Server.CreateObject("Adodb.Stream")

Ados.Mode=3

Ados.Type=1

Ados.Open

set Ados1=Server.CreateObject("Adodb.Stream")

Ados1.Mode=3

Ados1.Type=1

Ados1.Open

Ados.LoadFromFile(Server.mappath("comm/body.Fix"))

Ados1.write Ados.read(1280)

for i=0 to 3

Ados.Position=(9-zimg(i))*320

Ados1.Position=i*320

Ados1.write ados.read(320)

next

Ados.LoadFromFile(Server.mappath("comm/head.fix"))

Pos=lenb(Ados.read())

Ados.Position=Pos

for i=0 to 9 step 1

for j=0 to 3

Ados1.Position=i*32+j*320

Ados.Position=Pos+30*j+i*120

Ados.write ados1.read(30)

next

next

Response.ContentType = "image/BMP"

Ados.Position=0

Response.BinaryWrite Ados.read()

Ados.Close:set Ados=nothing

Ados1.Close:set Ados1=nothing

End Function

%

求ASP验证码代码

%

Option Explicit ' 显示声明

Class Com_GifCode_Class

Public Noisy, Count, Width, Height, Angle, Offset, Border

Private Graph(), Margin(3)

Private Sub Class_Initialize()

Randomize

Noisy = 6 ' 干扰点出现的概率

Count = 4 ' 字符数量

Width = 80 ' 图片宽度

Height = 20 ' 图片高度

Angle = 2 ' 角度随机变化量

Offset = 20 ' 偏移随机变化量

Border = 1 ' 边框大小

End Sub

Public Function Create()

Const cCharSet = "123456789"

Dim i, x, y

Dim vValidCode : vValidCode = ""

Dim vIndex

ReDim Graph(Width-1, Height-1)

For i = 0 To Count - 1

vIndex = Int(Rnd * Len(cCharSet))

vValidCode = vValidCode + Mid(cCharSet, vIndex+1 , 1)

SetDraw vIndex, i

Next

Create = vValidCode

End Function

Sub SetDot(pX, pY)

If pX * (Width-pX-1) = 0 And pY * (Height-pY-1) = 0 Then

Graph(pX, pY) = 1

End If

End Sub

Public Sub SetDraw(pIndex, pNumber)

' 字符数据

Dim DotData(8)

DotData(0) = Array(30, 15, 50, 1, 50, 100)

DotData(1) = Array(1 ,34 ,30 ,1 ,71, 1, 100, 34, 1, 100, 93, 100, 100, 86)

DotData(2) = Array(1, 1, 100, 1, 42, 42, 100, 70, 50, 100, 1, 70)

DotData(3) = Array(100, 73, 6, 73, 75, 6, 75, 100)

DotData(4) = Array(100, 1, 1, 1, 1, 50, 50, 35, 100, 55, 100, 80, 50, 100, 1, 95)

DotData(5) = Array(100, 20, 70, 1, 20, 1, 1, 30, 1, 80, 30, 100, 70, 100, 100, 80, 100, 60, 70, 50, 30, 50, 1, 60)

DotData(6) = Array(6, 26, 6, 6, 100, 6, 53, 100)

DotData(7) = Array(100, 30, 100, 20, 70, 1, 30, 1, 1, 20, 1, 30, 100, 70, 100, 80, 70, 100, 30, 100, 1, 80, 1, 70, 100, 30)

DotData(8) = Array(1, 80, 30, 100, 80, 100, 100, 70, 100, 20, 70, 1, 30, 1, 1, 20, 1, 40, 30, 50, 70, 50, 100, 40)

Dim vExtent : vExtent = Width / Count

Margin(0) = Border + vExtent * (Rnd * Offset) / 100 + Margin(1)

Margin(1) = vExtent * (pNumber + 1) - Border - vExtent * (Rnd * Offset) / 100

Margin(2) = Border + Height * (Rnd * Offset) / 100

Margin(3) = Height - Border - Height * (Rnd * Offset) / 100

Dim vStartX, vEndX, vStartY, vEndY

Dim vWidth, vHeight, vDX, vDY, vDeltaT

Dim vAngle, vLength

vWidth = Int(Margin(1) - Margin(0))

vHeight = Int(Margin(3) - Margin(2))

' 起始坐标

vStartX = Int((DotData(pIndex)(0)-1) * vWidth / 100)

vStartY = Int((DotData(pIndex)(1)-1) * vHeight / 100)

Dim i, j

For i = 1 To UBound(DotData(pIndex), 1)/2

If DotData(pIndex)(2*i-2) 0 And DotData(pIndex)(2*i) 0 Then

' 终点坐标

vEndX = (DotData(pIndex)(2*i)-1) * vWidth / 100

vEndY = (DotData(pIndex)(2*i+1)-1) * vHeight / 100

' 横向差距

vDX = vEndX - vStartX

' 纵向差距

vDY = vEndY - vStartY

' 倾斜角度

If vDX = 0 Then

vAngle = Sgn(vDY) * 3.14/2

Else

vAngle = Atn(vDY / vDX)

End If

' 两坐标距离

If Sin(vAngle) = 0 Then

vLength = vDX

Else

vLength = vDY / Sin(vAngle)

End If

' 随机转动角度

vAngle = vAngle + (Rnd - 0.5) * 2 * Angle * 3.14 * 2 / 100

vDX = Int(Cos(vAngle) * vLength)

vDY = Int(Sin(vAngle) * vLength)

If Abs(vDX) Abs(vDY) Then vDeltaT = Abs(vDX) Else vDeltaT = Abs(vDY)

For j = 1 To vDeltaT

SetDot Margin(0) + vStartX + j * vDX / vDeltaT, Margin(2) + vStartY + j * vDY / vDeltaT

Next

vStartX = vStartX + vDX

vStartY = vStartY + vDY

End If

Next

End Sub

Public Sub Output()

Response.Expires = -9999

Response.AddHeader "pragma", "no-cache"

Response.AddHeader "cache-ctrol", "no-cache"

Response.ContentType = "image/gif"

' 文件类型

Response.BinaryWrite ChrB(Asc("G")) ChrB(Asc("I")) ChrB(Asc("F"))

' 版本信息

Response.BinaryWrite ChrB(Asc("8")) ChrB(Asc("9")) ChrB(Asc("a"))

' 逻辑屏幕宽度

Response.BinaryWrite ChrB(Width Mod 256) ChrB((Width \ 256) Mod 256)

' 逻辑屏幕高度

Response.BinaryWrite ChrB(Height Mod 256) ChrB((Height \ 256) Mod 256)

Response.BinaryWrite ChrB(128) ChrB(0) ChrB(0)

' 全局颜色列表

Response.BinaryWrite ChrB(255) ChrB(255) ChrB(255)

Response.BinaryWrite ChrB(0) ChrB(85) ChrB(255)

' 图象标识符

Response.BinaryWrite ChrB(Asc(","))

Response.BinaryWrite ChrB(0) ChrB(0) ChrB(0) ChrB(0)

' 图象宽度

Response.BinaryWrite ChrB(Width Mod 256) ChrB((Width \ 256) Mod 256)

' 图象高度

Response.BinaryWrite ChrB(Height Mod 256) ChrB((Height \ 256) Mod 256)

Response.BinaryWrite ChrB(0) ChrB(7) ChrB(255)

Dim x, y, i : i = 0

For y = 0 To Height - 1

For x = 0 To Width - 1

If Rnd Noisy / 100 Then

Response.BinaryWrite ChrB(1-Graph(x, y))

Else

If x * (x-Width) = 0 or y * (y-Height) = 0 Then

Response.BinaryWrite ChrB(Graph(x, y))

Else

If Graph(x-1, y) = 1 or Graph(x, y) or Graph(x, y-1) = 1 Then

Response.BinaryWrite ChrB(1)

Else

Response.BinaryWrite ChrB(0)

End If

End If

End If

If (y * Width + x + 1) Mod 126 = 0 Then

Response.BinaryWrite ChrB(128)

i = i + 1

End If

If (y * Width + x + i + 1) Mod 255 = 0 Then

If (Width*Height - y * Width - x - 1) 255 Then

Response.BinaryWrite ChrB(255)

Else

Response.BinaryWrite ChrB(Width * Height Mod 255)

End If

End If

Next

Next

Response.BinaryWrite ChrB(128) ChrB(0) ChrB(129) ChrB(0) ChrB(59)

End Sub

End Class

Dim mCode

Set mCode = New Com_GifCode_Class

Session("GetCode") = mCode.Create()

mCode.Output()

Set mCode = Nothing

%

调用方法:

保存以上代码为checkGIF.asp

程序代码

input name="CheckCode" type="text" class="InputTxtLogin" id="CheckCode" onMouseOver="this.style.background='#ffffff';" onMouseOut="this.style.background='#f3f3f3'" size="4" maxlength="4"img src="checkGIF.asp"

关于asp图片验证码源代码和jsp生成图片验证码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/21003.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载