java生成随机图片验证码

2021-05-14 12:29

阅读:451

标签:rac   stream   test   技术   roman   content   ssi   session   http   

一、创建图片验证码生成工具类

  1 /**
  2  * 图片验证码生成工具类
  3  */
  4 public class validationCodeUtil {
  5 
  6     public static final String RANDOMCODEKEY= "RANDOMREDISKEY";//放到session中的key
  7     private String randString = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生数字与字母组合的字符串
  8     private int width = 95;// 图片宽
  9     private int height = 25;// 图片高
 10     private int lineSize = 40;// 干扰线数量
 11     private int stringNum = 4;// 随机产生字符数量
 12 
 13     private Random random = new Random();
 14 
 15     /**
 16      * 获得字体
 17      */
 18     private Font getFont() {
 19         return new Font("Fixedsys", Font.CENTER_BASELINE, 18);
 20     }
 21 
 22     /**
 23      * 获得颜色
 24      */
 25     private Color getRandColor(int fc, int bc) {
 26         if (fc > 255)
 27             fc = 255;
 28         if (bc > 255)
 29             bc = 255;
 30         int r = fc + random.nextInt(bc - fc - 16);
 31         int g = fc + random.nextInt(bc - fc - 14);
 32         int b = fc + random.nextInt(bc - fc - 18);
 33         return new Color(r, g, b);
 34     }
 35 
 36     /**
 37      * 生成随机图片
 38      */
 39     public void getRandcode(HttpServletRequest request, HttpServletResponse response) {
 40         HttpSession session = request.getSession();
 41         // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
 42         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
 43         Graphics g = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
 44         g.fillRect(0, 0, width, height);//图片大小
 45         g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));//字体大小
 46         g.setColor(getRandColor(110, 133));//字体颜色
 47         // 绘制干扰线
 48         for (int i = 0; i ) {
 49             drowLine(g);
 50         }
 51         // 绘制随机字符
 52         String randomString = "";
 53         for (int i = 1; i ) {
 54             randomString = drowString(g, randomString, i);
 55         }
 56         //将生成的随机字符串保存到session中
 57         session.removeAttribute(RANDOMCODEKEY);
 58         session.setAttribute(RANDOMCODEKEY, randomString);
 59         //设置失效时间1分钟
 60         session.setMaxInactiveInterval(60);
 61         g.dispose();
 62         try {
 63             // 将内存中的图片通过流动形式输出到客户端
 64             ImageIO.write(image, "JPEG", response.getOutputStream());
 65         } catch (Exception e) {
 66         }
 67 
 68     }
 69 
 70     /**
 71      * 绘制字符串
 72      */
 73     private String drowString(Graphics g, String randomString, int i) {
 74         g.setFont(getFont());
 75         g.setColor(new Color(random.nextInt(101), random.nextInt(111), random
 76                 .nextInt(121)));
 77         String rand = String.valueOf(getRandomString(random.nextInt(randString
 78                 .length())));
 79         randomString += rand;
 80         g.translate(random.nextInt(3), random.nextInt(3));
 81         g.drawString(rand, 13 * i, 16);
 82         return randomString;
 83     }
 84 
 85     /**
 86      * 绘制干扰线
 87      */
 88     private void drowLine(Graphics g) {
 89         int x = random.nextInt(width);
 90         int y = random.nextInt(height);
 91         int xl = random.nextInt(13);
 92         int yl = random.nextInt(15);
 93         g.drawLine(x, y, x + xl, y + yl);
 94     }
 95 
 96     /**
 97      * 获取随机的字符
 98      */
 99     public String getRandomString(int num) {
100         return String.valueOf(randString.charAt(num));
101     }

二、controller层调用

 1 @Controller
 2 public class CodeTest {
 3     /**
 4      * 图片验证码测试类
 5      * @param request
 6      * @param response
 7      * @throws Exception
 8      */
 9     @RequestMapping("/createImg")
10 public void createImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
11         try {
12         response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
13         response.setHeader("Pragma", "No-cache");//设置响应头信息,告诉浏览器不要缓存此内容
14         response.setHeader("Cache-Control", "no-cache");
15         response.setDateHeader("Expire", 0);
16         validationCodeUtil randomValidateCode = new validationCodeUtil();
17         randomValidateCode.getRandcode(request, response);//输出验证码图片
18         } catch(Exception e){
19         e.printStackTrace();
20         }
21         //从session中取出随机验证码
22         System.out.println(request.getSession().getAttribute("RANDOMREDISKEY"));
23         }
24 }

 三、运行项目访问地址

技术图片

 

java生成随机图片验证码

标签:rac   stream   test   技术   roman   content   ssi   session   http   

原文地址:https://www.cnblogs.com/liusha-1/p/13124608.html


评论


亲,登录后才可以留言!