This example shares the specific code of Java randomly generated verification code for your reference. The specific content is as follows
import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class RandImage extends HttpServlet {/*** Constructor of the object.*/ public RandImage() { super(); } private int imgWidth = 0; // Image width private int imgHeight = 0; // Image height private int codeCount = 0; //The number of characters in the picture private int x = 0; private int fontHeight; //The height of the font private int codeY; private String fontStyle; //Font style//Serialization ID Avoid duplicate private static final long serialVersionUID = 128554012633034503L; /** * Initialization configuration parameters*/public void init() throws ServletException { // Width String strWidth = "200"; // Height String strHeight ="80"; // Number of characters String strCodeCount ="5"; //Font fontStyle = "Times New Roman";// Convert the configured information into a numerical try { if (strWidth != null && strWidth.length() != 0) { imgWidth = Integer.parseInt(strWidth); } if (strHeight != null && strHeight.length() != 0) { imgHeight = Integer.parseInt(strHeight); } if (strCodeCount != null && strCodeCount.length() != 0) { codeCount = Integer.parseInt(strCodeCount); } } catch (NumberFormatException e) { e.printStackTrace(); } x = imgWidth / (codeCount + 1); //Character spacing fontHeight = imgHeight - 2; //Font height codeY = imgHeight - 12; //Code height} protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Output stream set response.setContentType("image/jpeg"); //Output format response.setHeader("Pragma", "No-cache"); //No cache regenerate response.setHeader("Cache-Control", "no-cache"); //No cache regenerate response.setDateHeader("Expires", 0); //No cache invalidation is also not cached HttpSession session = request.getSession(); //Get session session//Create image in memory BufferedImage image = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB); // Get the graphical context Graphics2D g = image.createGraphics(); // Generate random random random = new Random(); // Random class// Set the background color of the rectangle g.setColor(Color.WHITE); // Fill the rectangle Rect to white g.fillRect(0, 0, imgWidth, imgHeight); // Set the border font g.setFont(new Font(fontStyle, Font.PLAIN + Font.ITALIC, fontHeight)); // Set the border color g.setColor(new Color(55, 55, 12)); // Draw the border g.drawRect(0, 0, imgWidth - 1, imgHeight - 1); // Randomly generate 160 interference lines, making the authentication code in the image not easy to be detected by other programs g.setColor(getRandColor(160, 200)); for (int i = 0; i < 160; i++) { int x = random.nextInt(imgWidth); int y = random.nextInt(imgHeight); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x, y, x + xl, y + yl); } // Take the randomly generated authentication code (4-digit number) String sRand = ""; int red = 0, green = 0, blue = 0; for (int i = 0; i < codeCount; i++) { //Change to generate codeCount random characters//Stories the new color through rgb three colors red = random.nextInt(255); green = random.nextInt(255); blue = random.nextInt(255); //Stories to get a number of 0 1 2 int wordType = random.nextInt(3);//Stories to get 3 numbers between 0-2 char retWord = 0; //0 Number 1 Lowercase letter 2 Capital letter switch (wordType) { case 0: retWord = this.getSingleNumberChar(); //Get the char-type break of 0-9; case 1: retWord = this.getLowerOrUpperChar(0); //Get the lowercase char-type break; case 2: retWord = this.getLowerOrUpperChar(1); //Get the uppercase char-type break; } sRand += String.valueOf(retWord); //Get the obtained random characters g.setColor(new Color(red, green, blue)); //Set a color g.drawString(String.valueOf(retWord), 2+(i) * x, codeY); //Write the characters to the corresponding position in the picture} //Save the authentication code in SESSION session.setAttribute("rand", sRand); //Save the obtained random characters into the session reply, and you can call //The image takes effect g.dispose(); //Release the g object ServletOutputStream responseOutputStream = response.getOutputStream(); //Output stream//Output image to the page ImageIO.write(image, "JPEG", responseOutputStream); //Output in JPEG format// Close the input stream below! responseOutputStream.flush();//Fresh and close the stream responseOutputStream.close(); } Color getRandColor(int fc, int bc) {//Give a random color Random = new Random(); if (fc > 255) fc = 255; if (bc > 255) bc = 255; int r = fc + random.nextInt(bc - fc); int g = fc + random.nextInt(bc - fc); int b = fc + random.nextInt(bc - fc); return new Color(r, g, b); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } // Convert an integer random number to char to return private char getSingleNumberChar() { Random random = new Random(); int numberResult = random.nextInt(10); int ret = numberResult + 48; //When converting character '0' to ascall code, it is 48 return (char) ret; } //Get 26 characters private char getLowerOrUpperChar(int upper) { Random random = new Random(); int numberResult = random.nextInt(26); int ret = 0; if (upper == 0) {// Lowercase ret = numberResult + 97; } else if (upper == 1) {// capital ret = numberResult + 65; } return (char) ret; }} 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.