Java动态验证码单线设计的两种方法
程序员文章站
2024-02-15 14:53:58
1.java的动态验证码我这里将介绍两种方法:
一:根据java本身提供的一种验证码的写法,这种呢只限于大家了解就可以了,因为java自带的模式编写的在实际开发中是没有意...
1.java的动态验证码我这里将介绍两种方法:
一:根据java本身提供的一种验证码的写法,这种呢只限于大家了解就可以了,因为java自带的模式编写的在实际开发中是没有意义的,所以只供学习一下就可以了,待会讲解的第二种呢就是我们需要掌握的一种模式了:
第一种的代码如下:
import java.awt.color; import java.awt.font; import java.awt.graphics; import java.awt.image.bufferedimage; import java.io.ioexception; import java.util.random; import javax.imageio.imageio; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; /** * servlet implementation class inde */ @webservlet("/inde") public class inde extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public inde() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } /**动态生成图片验证码 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub //创建图像 int width=100; int height=40; //图片的大小设置 bufferedimage image=new bufferedimage(width,height,bufferedimage.type_int_rgb); //创建画板 graphics g=image.getgraphics(); setsquarebackground(g,width,height,5); //确定画笔颜色 g.setcolor(color.black); //填充矩形 g.fillrect(0, 0, width, height); //在大矩形中放小矩形 g.setcolor(color.white); g.fillrect(1, 1, width-2, height-2); //填充字符 string str = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm0123456789"; stringbuffer sb=new stringbuffer(); //画随机干扰框 setsquarebackground(g,width,height,3); //画干扰点 createrandompoint(width, height,100,g,100); //随机画几条线 createrandomline(width, height,8,g,100); //随机获取4个字符 random random=new random(); for (int i = 0; i < 4; i++) { //62个填充字符里面随机的随机的收取字符 int index=random.nextint(62); //截取一个字符 string st=str.substring(index, index+1); //把字符放到图片中去 g.setcolor(color.red); //设置字体 g.setfont(new font("宋体",font.bold,30)); g.drawstring(st, 20*i, 30);//防止4个字符在一起 sb.append(st); } //把stringbuffer中的验证码放到session里面,目的是让login调用 httpsession se=request.getsession(); se.setattribute("number", sb.tostring()); //发送图片到浏览器 指定发送的图片 和格式 response.setcontenttype("image/jpeg"); //图片,图片的格式,输出的方式 imageio.write(image, "jpg", response.getoutputstream()); } random rand = new random(); private void createrandompoint(int width,int height,int many,graphics g,int alpha) { // 随机产生干扰点 for (int i=0;i<many;i++) { int x = rand.nextint(width); int y = rand.nextint(height); g.setcolor(getcolor(alpha)); g.drawoval(x,y,rand.nextint(3),rand.nextint(3)); } } /** * 随机产生干扰线条 * @param width * @param height * @param minmany 最少产生的数量 * @param g * @param alpha 透明度0~255 0表示全透 */ private void createrandomline(int width,int height,int minmany,graphics g,int alpha) { // 随机产生干扰线条 for (int i=0;i<getintrandom(minmany, minmany+6);i++) { int x1 =getintrandom(0,(int)(width*0.6)); int y1 =getintrandom(0,(int)(height*0.6)); int x2 =getintrandom((int)(width*0.4),width); int y2 =getintrandom((int)(height*0.2),height); g.setcolor(getcolor(alpha)); g.drawline(x1, y1, x2, y2); } } /** * 由随机产生的方块来作为干扰背景 */ private void setsquarebackground(graphics g,int width,int height,int count){ // 随机产生干扰线条 for (int i=0;i<getintrandom(count, count+2);i++) { int x1 =getintrandom(0,(int)(width*0.3)); int y1 =getintrandom(0,(int)(height*0.3)); int x2 =getintrandom((int)(width*0.5),width); int y2 =getintrandom((int)(height*0.55),height); g.setcolor(getcolor(100)); int w=x2-x1; int h=y2-y1; if(w<0) w=-w; if(h<0) h=-h; g.drawrect(x1, y1, w, h); g.setcolor(getcolor(25)); g.fillrect(x1, y1, w, h); } } private int getintrandom(double start,double end) { if(end<start) { double t=end; end=start; start=t; } double i=start+(int) (math.random()*(end-start)); return (int)i; }
上面的代码呢写的很详细了,这里我就不重复了,这里的干扰线条是有很多写法的,我这里就没有全写出来,有需要的话可以私聊我哦!
下面介绍第二种:
这种呢是我们开发中是可以用得到的,使用在的是网页端的交互,我们在登录网站的时候有很多的验证码就可以用这个来写了
import java.io.ioexception; import java.util.random; import javax.imageio.imageio; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; /** * servlet implementation class inde */ @webservlet("/inde") public class inde extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public inde() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } /**动态生成图片验证码 * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub //创建图像 int width=100; int height=40; //图片的大小设置 bufferedimage image=new bufferedimage(width,height,bufferedimage.type_int_rgb); //创建画板 graphics g=image.getgraphics(); setsquarebackground(g,width,height,5); //确定画笔颜色 g.setcolor(color.black); //填充矩形 g.fillrect(0, 0, width, height); //在大矩形中放小矩形 g.setcolor(color.white); g.fillrect(1, 1, width-2, height-2); //填充字符 string str = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnm0123456789"; stringbuffer sb=new stringbuffer(); //随机获取4个字符 random random=new random(); for (int i = 0; i < 4; i++) { //62个填充字符里面随机的随机的收取字符 int index=random.nextint(62); //截取一个字符 string st=str.substring(index, index+1); //把字符放到图片中去 g.setcolor(color.red); //设置字体 g.setfont(new font("宋体",font.bold,30)); g.drawstring(st, 20*i, 30);//防止4个字符在一起 sb.append(st); } //把stringbuffer中的验证码放到session里面,目的是让login调用 httpsession se=request.getsession(); se.setattribute("number", sb.tostring()); //发送图片到浏览器 指定发送的图片 和格式 response.setcontenttype("image/jpeg"); //图片,图片的格式,输出的方式 imageio.write(image, "jpg", response.getoutputstream()); }
这样就可以运行了,当然了我们可以在这里面加入第一种的干扰模块给拿过来这样就可以实现整体的一个效果了。
总结
以上所述是小编给大家介绍的java动态验证码单线设计的两种方法,希望对大家有所帮助