项目模块--1.实现验证码功能

简介:

Java web项目中,在后端随机生成一个验证码,绘制成图像,并在图像上添加两条干扰线,发送到浏览器,供用户使用。

本片博文内容包括,功能实现的逻辑步骤,Java实现代码,生成的验证码图片展示。

步骤一:生成一个包含四个字符的字符串

使用一个数组char[]+一个Random对象实现该功能。代码如下:


private char code[]={
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2',
'3', '4', '5', '6', '7', '8', '9' };
Random rd = new Random();

String result = "";

for(int i = 0;i < 4;i++ ){
  result+=code[rd.nextInt[code.length]];
}
 


步骤二:绘制图片

使用一个BufferdImage对象,Graphics对象,Font对象

BufferedImage类:一个图像类,它生成的图像在内存中占有一个缓存区。

使用方法有两种:

1.读取一个已经存在的图片文件到BufferedImage对象的缓存区中,然后对该图片文件进行操作。

  BufferedImage b = ImageIO.read(new FileOutputStream(File f));

2.创建一个BufferedImage对象,然后通过Graphics对象对其进行绘制。

  BufferedImage b = new BufferedImage(int width,int height,IndexColorModel cn);  //设定图片的宽度,高度,颜色设定模式

  Graphics g = b.getGraphics();                        //得到图片绘制器

3.Graphics对象常用方法

  g.setColor(Color color);

  g.fillRect(int x,int y,int width,int heigh)                    //两个一组,为图片的指定位置设置颜色,以图片的左下角为原点,(x,y)为绘制图片的左下

                                      //角坐标,绘制面积为width,height。

  g.drawString(String s,int x,int y)                      //在(x,y)坐标系下绘制字符串,坐标系原点为BufferedImage的左下角

  g.drawLine(int x1,int y1,int x2,int y2)                    //在两点之间绘制一条直线,颜色使用最近的g.setColor()设定。

  g.dispose()                               //释放正在使用的系统资源

 1 BufferdImage bufferedImage = new     BufferedImage(50,30,BufferedImage.TYPE_INT_BGR);
 2 Graphics graphics = bufferedImage.getGraphics();
 3 Font font = new Font("Arial",Font.TRUETYPE_FONT,18);
 4 //设置背景颜色,并填充
 5 graphics.setColor(new Color(rd.nextInt(55)+200,rd.nextInt(55)+20,rd.nextInt(55)+200));
 6 graphics.fillRect(0,0,WIDTH,HEIGH);   //设定要填充的矩形的坐标(x,y),矩形的宽度和高度
 7 //边框颜色
 8 graphics.setColor(Color.black);
 9 graphics.fillRect(0,0,WIDTH-1,HEIGH-1);
10 //设置字体
11 graphics.setFont(font);        
12 for (int i = 0; i <result.length() ; i++) {
13             g.setColor(new Color(rd.nextInt(200),rd.nextInt(200),rd.nextInt(200)));
14             g.drawString(result.charAt(i)+"",12*i+1,16);  //需要绘制的字符串,绘制的位置。
15         }
16 for (int i = 0; i < 2 ; i++) {
17             g.setColor(new Color(rd.nextInt(200),rd.nextInt(200),rd.nextInt(200)));
18             int x1 = rd.nextInt(WIDTH);
19             int x2 = rd.nextInt(WIDTH);
20             int y1 = rd.nextInt(HEIGH);
21             int y2 = rd.nextInt(HEIGH);
22             g.drawLine(x1,y1,x2,y2);
23         }

步骤三:将图片发送到浏览器端,存放此次产生的验证码中的字符串

设置浏览器端不能缓存验证码图片

Pragma域名:用来包含实现特定的指令,Pragma:No-cache(不允许缓存响应或请求报文)

Cache-Controller域名:在HTTP:1.1中使用,指定缓存规则。

response.setHeader("Pragma","No-cache");   //通知浏览器不要使用缓存功能
        response.setHeader("Cache-Control","no-cache"); //通知浏览器不要缓存
        response.setDateHeader("Expires",0);

设置响应的媒体类型

response.setContentType("image/jpeg");

将验证码中的字符串发送到session中,以便验证时从session中获取

session.setAttribute("code",result);

将验证码图片发送给浏览器端

1 try{
2      OutputStream os = reponse.getOutputStream();
3      ImageIo.write(image,"JPEG",os);  
4 }catch(IOException e){
5      e.printStackTrace();  
6 }

ImageIO类:

  图像读写工具类,提供静态方法来将任意格式的图片读取到内存中或写入到文件中。

原文地址:https://www.cnblogs.com/deijiawoyu/p/12489165.html