1. Create a tool class and configure the verification code related parameters
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Random; /** * @author ld *@date November 6, 2017 * @param * @desc graphic verification code generation * */ public class VerifyUtil { // verification code character set 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'}; // number of characters private static final int SIZE = 4; // number of interference lines private static final int LINES = 5; // width private static final int WIDTH = 80; // high private static final int HEIGHT = 40; // font size private static final int FONT_SIZE = 30; /** * Generate random verification codes and pictures * Object[0]: verification code string; * Object[1]: Captcha image. */ public static Object[] createImage() { StringBuffer sb = new StringBuffer(); // 1. Create a blank image BufferedImage image = new BufferedImage( WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 2. Get the picture brush Graphics graphic = image.getGraphics(); // 3. Set the brush color graphic.setColor(Color.LIGHT_GRAY); // 4. Draw a rectangle background graphic.fillRect(0, 0, WIDTH, HEIGHT); // 5. Draw random characters Random ran = new Random(); for (int i = 0; i <SIZE; i++) { // get random character index int n = ran.nextInt(chars.length); // set random color graphic.setColor(getRandomColor()); // set font size graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); // draw characters graphic.drawString( chars[n] + "", i * WIDTH / SIZE, HEIGHT*2/3); // record characters sb.append(chars[n]); } // 6. Draw the interference line for (int i = 0; i < LINES; i++) { // set random color graphic.setColor(getRandomColor()); // draw lines randomly graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } // 7. Return the verification code and picture return new Object[]{sb.toString(), image}; } /** * Random color selection */ public static Color getRandomColor() { Random ran = new Random(); Color color = new Color(ran.nextInt(256), ran.nextInt (256), ran.nextInt (256)); return color; } }
2. Interface
@RequestMapping(value="/createValicode",method=RequestMethod.GET) public void valicode(HttpServletResponse response,HttpSession session) throws Exception{ //Using the image tool to generate the image //The first parameter is the generated verification code, and the second parameter is the generated image Object[] objs = VerifyUtil.createImage(); //Save the verification code to the Session session.setAttribute("imageCode",objs[0]); // output the image to the browser BufferedImage image = (BufferedImage) objs[1]; response.setContentType("image/png"); OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); }
3. Test page call
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8" /> <title>hello</title> </head> <body> <h1 th:text="${info}" /> <div> <!-- <img alt="This is a picture" src="/img/001.png"/> --> <img alt="验证码" onclick = "this.src='/iot-frame/createValicode?' + Math.floor(Math.random() * 100)" src="/iot-frame/createValicode" /> </div> <form action="imgvrifyControllerDefaultKaptcha"> <input type="text" name="vrifyCode" /> <input type="submit" value="提交"></input> </form> </body> </html>