There have been many articles about java image verification codes recently, helping everyone master the java verification code generation technology. The following is a simplest way to generate java image verification codes for your reference.
Nowadays, all industries will consider machine registration when customizing systems. The most effective way now is to enter verification. There are many ways to verify now:
1. Problem verification is actually also image verification. Generate a question on the picture, and then enter the box to enter the answer.
2. Picture verification, enter the text information displayed on the picture.
3. SMS verification is quite complicated and users don’t like it very much.
4. There is also Baidu's latest verification method. Text is generated on the picture, and a text click box appears to select the text you see on the verification picture.
Now let’s share the code for java to generate verification code, which is a basic code. It can be used directly in learning. If you need more complex verification, you can add logical verification yourself.
@Controllerpublic class ImgVerifyCode extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Width of the verification code image. */ private int width = 70; /** * The height of the verification code picture. */ private int height =30; /** * Number of verification code characters*/ private int codeCount = 5; /** * xx */ private int xx = 0; /** * Font height*/ private int fontHeight; /** * codeY */ private int codeY; /** * codeSequence */ String[] codeSequence = { "1" , "2" , "3" , "4" , "5" ,"6","7","8","9","A","a","B","b","c","C" ,"D","d","E","E","F","f","G","z","X","Q","v"}; /** * Initialize the verification image attributes*/ public void init() throws ServletException { // Get initial information from web.xml// Width String strWidth =width+""; // Height String strHeight = height+""; // Number of characters String strCodeCount = codeCount+""; // Convert the configured information into a numerical try { if (strWidth != null && strWidth.length() != 0) { width = Integer.parseInt(strWidth); } if (strHeight != null && strHeight.length() != 0) { height = Integer.parseInt(strHeight); } if (strCodeCount != null && strCodeCount.length() != 0) { codeCount = Integer.parseInt(strCodeCount); } } catch (NumberFormatException e) { e.printStackTrace(); } xx = width / (codeCount + 2); //The horizontal distance of generating random numbers fontHeight = height - 12; //The numerical height of generating random numbers codeY = height - 8; //The vertical distance of generating random numbers } protected String images(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException { init(); //Define image buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gd = buffImg.createGraphics(); //Define image buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gd = buffImg.createGraphics(); //Define image buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D gd = buffImg.createGraphics(); // Create a random number generator class Random random = new Random(); // Fill the image as white gd.setColor(Color.WHITE); gd.fillRect(0, 0, width, height); // Create a font, the size of the font should be determined according to the height of the image. Font font = new Font("Fixedsys", Font.PLAIN, fontHeight); // Set the font. gd.setFont(font); // Draw borders. gd.setColor(Color.BLACK); gd.drawRect(0, 0, width - 1, height - 1); // Randomly generates 4 interference lines, making the authentication code in the image less likely to be detected by other programs. gd.setColor(Color.BLACK); for (int i = 0; i < 4; i++) { int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); gd.drawLine(x, y, x + xl, y + yl); } // randomCode is used to save randomly generated verification codes so that users can verify after logging in. StringBuffer randomCode = new StringBuffer(); int red = 0, green = 0, blue = 0; // Randomly generates a verification code of the codeCount number. for (int i = 0; i < codeCount; i++) { // Get the randomly generated verification code number. String strRand = String.valueOf(codeSequence[random.nextInt(27)]); // Generate random color components to construct the color value, so that the color value of each digit output will be different. red = random.nextInt(125); green = random.nextInt(255); blue = random.nextInt(200); // Use the randomly generated color to draw the verification code into the image. gd.setColor(new Color(red, green, blue)); gd.drawString(strRand, (i + 1) * xx, codeY); // Combine the generated four random numbers together. randomCode.append(strRand); } // Save the four-digit verification code into the Session. HttpSession session = req.getSession(); session.setAttribute("validateCode", randomCode.toString()); // Image cache is prohibited. resp.setHeader("Pragma", "no-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); resp.setContentType("image/jpeg"); // Output the image to the Servlet output stream. ServletOutputStream sos = resp.getOutputStream(); ImageIO.write(buffImg, "jpeg", sos); sos.close(); return null; } }This code is the basic method for generating verification images.
The above is all the content of this article. I hope it will be helpful to everyone's learning. You can also check out the previous article for in-depth study.