This blog record the implementation of an image verification code in the project. Although it is not very complicated, a good memory is not as good as a bad pen. Remember it!
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 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; // Height private static final int HEIGHT = 40; // Font size private static final int FONT_SIZE = 30; /** * Generate random verification code and picture* Object[0]: verification code string; * Object[1]: verification code picture. */ 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 image brush Graphics graphic = image.getGraphics(); // 3. Set the brush color graphics.setColor(Color.LIGHT_GRAY); // 4. Draw the rectangle background graphics.fillRect(0, 0, WIDTH, HEIGHT); // 5. Draw random characters Random ran = new Random(); for (int i = 0; i <SIZE; i++) { // Take the random character index int n = ran.nextInt(chars.length); // Set the random color graphic.setColor(getRandomColor()); // Set the font size graphic.setFont(new Font( null, Font.BOLD + Font.ITALIC, FONT_SIZE)); // Draw character 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()); // Random color graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT)); } // 7. Return verification code and image return new Object[]{sb.toString(), image}; } /** * Random color*/ 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 layer
/** * @author ZZC * @date November 6, 2017* @param * @desc Graphic verification code generation method* */ @RequestMapping("/valicode") public void valicode(HttpServletResponse response,HttpSession session) throws Exception{ //Use image tools to generate images//The first parameter is the generated verification code, and the second parameter is the generated image Object[] objs = VerifyUtil.createImage(); //Store the verification code into 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); }Running results:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.