资源描述:
《使用java生成四位随机验证码图片》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、使用JAVA生成四位随机验证码图片效果:是第一位和第三位是随机数字,第二位和第四位是随机字母,图片中还有随机干扰线,这些数字,字母,干扰线的颜色均随机packagecn.itcast.web.response;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.image.BufferedImage;importjava.awt.image.BufferedImageOp;importjava.io.IO
2、Exception;importjava.io.PrintWriter;importjava.util.Random;importjavax.imageio.ImageIO;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;//生成验证码图
3、片publicclassDemo1extendsHttpServlet{ publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ intWIDTH=100; intHIGHT=30; //在内在中创建一幅图片 BufferedImageimage=newBufferedImage(WIDTH,HIGHT,BufferedImage.TYPE_INT_
4、RGB); //取得一个画笔 Graphicsg=image.getGraphics(); //设置字符串格式 g.setFont(newFont("黑体",Font.BOLD,24)); //画干扰线 for(inti=0;i<7;i++){ Randomr=newRandom(); intx1=r.nextInt(WIDTH); intx2=r.nextInt(WIDTH); inty1=r.nextInt(HIGHT); inty2=r.nextInt(HIGHT);
5、Colorcolor=newColor( (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValue()+128); g.setColor(color); g.drawLine(x1,y1,x2,y2); } //得
6、到一个随机颜色 Colorcolor=newColor( (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValue()+128); g.setColor(color); //画字符串 g.drawString(getVaul
7、e(),WIDTH/5,2*HIGHT/3); //blog.sina.com.cn/flyzhouw Colorcolor1=newColor( (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValue()+128); g.
8、setColor(color1); g.drawString(getChar(),WIDTH/3,2*HIGHT/3); Colorcolor2=newColor( (newDouble(Math.random()*128)).intValue()+128, (newDouble(Math.random()*128)).intValu