服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - springboot控制层图片验证码生成

springboot控制层图片验证码生成

2021-04-08 14:26把杯子装进水里 JAVA教程

这篇文章主要为大家详细介绍了springboot控制层图片验证码生成,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本次博客记录项目中一个图片验证码的实现,虽然不是很复杂,但好记性不如烂笔头,谨记!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.zl.util;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
 
import javax.imageio.ImageIO;
 
/**
 * @author ZZC
 * @date 2017年11月6日
 * @param
 * @desc 图形验证码生成
 *
 */
public class VerifyUtil {
 // 验证码字符集
  private static final char[] chars = { 
   '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'
   'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
  // 字符数量
  private static final int SIZE = 4;
  // 干扰线数量
  private static final int LINES = 5;
  // 宽度
  private static final int WIDTH = 80;
  // 高度
  private static final int HEIGHT = 40;
  // 字体大小
  private static final int FONT_SIZE = 30;
 
  /**
   * 生成随机验证码及图片
   * Object[0]:验证码字符串;
   * Object[1]:验证码图片。
   */
  public static Object[] createImage() {
   StringBuffer sb = new StringBuffer();
   // 1.创建空白图片
   BufferedImage image = new BufferedImage(
     WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
   // 2.获取图片画笔
   Graphics graphic = image.getGraphics();
   // 3.设置画笔颜色
   graphic.setColor(Color.LIGHT_GRAY);
   // 4.绘制矩形背景
   graphic.fillRect(0, 0, WIDTH, HEIGHT);
   // 5.画随机字符
   Random ran = new Random();
   for (int i = 0; i <SIZE; i++) {
    // 取随机字符索引
    int n = ran.nextInt(chars.length);
    // 设置随机颜色
    graphic.setColor(getRandomColor());
    // 设置字体大小
    graphic.setFont(new Font(
      null, Font.BOLD + Font.ITALIC, FONT_SIZE));
    // 画字符
    graphic.drawString(
      chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3);
    // 记录字符
    sb.append(chars[n]);
   }
   // 6.画干扰线
   for (int i = 0; i < LINES; i++) {
    // 设置随机颜色
    graphic.setColor(getRandomColor());
    // 随机画线
    graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT),
      ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
   }
   // 7.返回验证码和图片
   return new Object[]{sb.toString(), image};
  }
 
  /**
   * 随机取色
   */
  public static Color getRandomColor() {
   Random ran = new Random();
   Color color = new Color(ran.nextInt(256), 
     ran.nextInt(256), ran.nextInt(256));
   return color;
  }
 
  public static void main(String[] args) throws IOException {
   Object[] objs = createImage();
   BufferedImage image = (BufferedImage) objs[1];
   OutputStream os = new FileOutputStream("d:/1.png");
   ImageIO.write(image, "png", os);
   os.close();
  }
}

controller层

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
  * @author ZZC
  * @date 2017年11月6日
  * @param
  * @desc 图形验证码生成方法
  *
  */
 @RequestMapping("/valicode")
 public void valicode(HttpServletResponse response,HttpSession session) throws Exception{
  //利用图片工具生成图片
  //第一个参数是生成的验证码,第二个参数是生成的图片
  Object[] objs = VerifyUtil.createImage();
  //将验证码存入Session
  session.setAttribute("imageCode",objs[0]);
   
  //将图片输出给浏览器
  BufferedImage image = (BufferedImage) objs[1];
  response.setContentType("image/png");
  OutputStream os = response.getOutputStream();
  ImageIO.write(image, "png", os);
 }

运行结果:

springboot控制层图片验证码生成

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/qq_40293192/article/details/78462154

延伸 · 阅读

精彩推荐