随机生成验证码的一个演示升级版+++
上次做的是直接在form上画,运行速度有点慢,现在改成在内存上,速度就快多了,而且生成的验证码图片,用上次吹水佬的二极化代码无法彻底去噪附上代码,有兴趣的可以提高一下二极化的代码算法。。。
程序代码:
PUBLIC oform1 oform1=NEWOBJECT("form1") oform1.Show RETURN ************************************************** *-- Form: form1 (d:\documents\visual foxpro 项目\logondemo.scx) *-- ParentClass: form *-- BaseClass: form *-- Time Stamp: 10/23/22 09:00:05 PM * DEFINE CLASS form1 AS form DoCreate = .T. AutoCenter = .T. Caption = "Form1" Name = "Form1" ADD OBJECT label1 AS label WITH ; Caption = "用户名", ; Height = 16, ; Left = 110, ; Top = 30, ; Width = 38, ; Name = "Label1" ADD OBJECT text1 AS textbox WITH ; Height = 20, ; Left = 160, ; Top = 26, ; Width = 100, ; Name = "Text1" ADD OBJECT label2 AS label WITH ; Caption = "密 码", ; Height = 16, ; Left = 110, ; Top = 60, ; Width = 38, ; Name = "Label2" ADD OBJECT text2 AS textbox WITH ; Height = 20, ; Left = 160, ; Top = 56, ; Width = 100, ; PasswordChar = "*", ; Name = "Text2" ADD OBJECT label3 AS label WITH ; Caption = "验证码", ; Height = 16, ; Left = 110, ; Top = 90, ; Width = 38, ; Name = "Label3" ADD OBJECT text3 AS textbox WITH ; Height = 20, ; Left = 160, ; Top = 85, ; Width = 100, ; Name = "Text3" ADD OBJECT label4 AS label WITH ; AutoSize = .T., ; Caption = "点击显示验证码", ; Height = 16, ; Left = 100, ; Top = 128, ; Width = 86, ; Name = "Label4" ADD OBJECT image1 AS image WITH ; Height = 80, ; Left = 100, ; Top = 148, ; Width = 180, ; Name = "Image1" PROCEDURE image1.Init this.click ENDPROC PROCEDURE image1.Click thisform.Cls() RAND(-1) *****************************生成随机干扰码************************************* SET CLASSLIB TO HOME()+"FFC\_gdiplus.vcx" locolor=CREATEOBJECT("gpcolor",255,255,255,255) lobrush=CREATEOBJECT("gpsolidbrush",locolor) lographics=CREATEOBJECT("gpgraphics") lobitmap=CREATEOBJECT("gpbitmap",this.Width,this.height) lographics.createfromimage(lobitmap) lographics.fillrectangle(lobrush,0,0,this.width,this.height) FOR i=1 TO 150 lncolor=INT(RAND()*1000000) nred=MOD(lncolor,0x100) ngreen=INT(MOD(lncolor,0x10000)/0x100) nblue=INT(lncolor/0x10000) locolor.set(nred,ngreen,nblue,255) lobrush.create(locolor) DO WHILE .t. nleft=INT(RAND()*200) ntop=INT(RAND()*100) IF nleft<this.Width-5 AND ntop<this.Height-5 EXIT ELSE loop ENDIF ENDDO lographics.fillrectangle(lobrush,nleft,ntop,5,5) ENDFOR *****************************生成随机干扰码************************************* *****************************生成随机验证码************************************* lpString = randomstr(4) FOR i=1 TO 4 nleft=40*(i-1) ntop=10 lopoint=CREATEOBJECT("gppoint",nleft,ntop) lofont=CREATEOBJECT("gpfont") lofont.create("arial",32,0,3) lncolor=INT(RAND()*1000000) nred=MOD(lncolor,0x100) ngreen=INT(MOD(lncolor,0x10000)/0x100) nblue=INT(lncolor/0x10000) locolor.set(nred,ngreen,nblue,255) lobrush=CREATEOBJECT("gpsolidbrush",locolor) lostringformat=CREATEOBJECT("gpstringformat") lographics.drawstringA(SUBSTR(lpstring,i,1),lofont,lopoint,lostringformat,lobrush) ENDFOR *****************************生成随机验证码************************************* lobitmap.savetofile("background.jpg","image/jpeg","quality=100") this.picture="background.jpg" RELEASE ALL LIKE lo ENDPROC ENDDEFINE * *-- EndDefine: form1 **************************************************
运行效果图如下:
二级化效果图如下: