Java生成验证码功能实例代码
页面上输入验证码是比较常见的一个功能,实现起来也很简单.给大家写一个简单的生成验证码的示例程序,需要的朋友可以借鉴一下.
闲话少续,直接上代码.代码中的注释很详细.
packagecom.SM_test.utils; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.Graphics2D; importjava.awt.RenderingHints; importjava.awt.geom.AffineTransform; importjava.awt.image.BufferedImage; importjava.io.File; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.OutputStream; importjava.util.Arrays; importjava.util.Random; importjavax.imageio.ImageIO; publicclassVerifyCodeUtils{ //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符 publicstaticfinalStringVERIFY_CODES="23456789ABCDEFGHJKLMNPQRSTUVWXYZ"; privatestaticRandomrandom=newRandom(); /** *使用系统默认字符源生成验证码 *@paramverifySize验证码长度 *@return */ publicstaticStringgenerateVerifyCode(intverifySize){ returngenerateVerifyCode(verifySize,VERIFY_CODES); } /** *使用指定源生成验证码 *@paramverifySize验证码长度 *@paramsources验证码字符源 *@return */ publicstaticStringgenerateVerifyCode(intverifySize,Stringsources){ if(sources==null||sources.length()==0){ sources=VERIFY_CODES; } intcodesLen=sources.length(); Randomrand=newRandom(System.currentTimeMillis()); StringBuilderverifyCode=newStringBuilder(verifySize); for(inti=0;i255) fc=255; if(bc>255) bc=255; intr=fc+random.nextInt(bc-fc); intg=fc+random.nextInt(bc-fc); intb=fc+random.nextInt(bc-fc); returnnewColor(r,g,b); } privatestaticintgetRandomIntColor(){ int[]rgb=getRandomRgb(); intcolor=0; for(intc:rgb){ color=color<<8; color=color|c; } returncolor; } privatestaticint[]getRandomRgb(){ int[]rgb=newint[3]; for(inti=0;i<3;i++){ rgb[i]=random.nextInt(255); } returnrgb; } privatestaticvoidshear(Graphicsg,intw1,inth1,Colorcolor){ shearX(g,w1,h1,color); shearY(g,w1,h1,color); } privatestaticvoidshearX(Graphicsg,intw1,inth1,Colorcolor){ intperiod=random.nextInt(2); booleanborderGap=true; intframes=1; intphase=random.nextInt(2); for(inti=0;i >1) *Math.sin((double)i/(double)period +(6.2831853071795862D*(double)phase) /(double)frames); g.copyArea(0,i,w1,1,(int)d,0); if(borderGap){ g.setColor(color); g.drawLine((int)d,i,0,i); g.drawLine((int)d+w1,i,w1,i); } } } privatestaticvoidshearY(Graphicsg,intw1,inth1,Colorcolor){ intperiod=random.nextInt(40)+10;//50; booleanborderGap=true; intframes=20; intphase=7; for(inti=0;i
>1) *Math.sin((double)i/(double)period +(6.2831853071795862D*(double)phase) /(double)frames); g.copyArea(i,0,1,h1,0,(int)d); if(borderGap){ g.setColor(color); g.drawLine(i,(int)d,i,0); g.drawLine(i,(int)d+h1,i,h1); } } } publicstaticvoidmain(String[]args)throwsIOException{ Filedir=newFile("e:/abc"); intw=200,h=80; for(inti=0;i<50;i++){ StringverifyCode=generateVerifyCode(4); Filefile=newFile(dir,verifyCode+".jpg"); outputImage(w,h,file,verifyCode); } } }
上面这段代码就能生成一个验证码,略微修改就能生成各种各样的形式,main方法可以测试.
下面为大家写一下如何返回到页面
packagecom.SM_test.saomiao.constroller; importjava.io.IOException; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjavax.servlet.http.HttpSession; importorg.springframework.stereotype.Controller; importorg.springframework.web.bind.annotation.RequestMapping; importcom.SM_test.utils.VerifyCodeUtils; @Controller publicclassIndexConstroller{ @RequestMapping(value="/index") publicvoidindex(HttpServletRequestrequest,HttpServletResponseresponse)throwsIOException{ response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); response.setContentType("image/jpeg"); //生成随机字串 StringverifyCode=VerifyCodeUtils.generateVerifyCode(4); //存入会话session HttpSessionsession=request.getSession(true); session.setAttribute("rand",verifyCode.toLowerCase()); //生成图片 intw=200,h=80; VerifyCodeUtils.outputImage(w,h,response.getOutputStream(),verifyCode); } }
很简单,用HttpServletResponse就OK了.
需要显示验证码的地方可以直接用img标签,地址就是该Controller的URL就OK了
以上所述是小编给大家介绍的Java生成验证码功能实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!