java background authentication code generation, returns a base64 string
// ==== two-dimensional code generation tool method ====================================== = // / ** * Instructions * import java.awt.Graphics2D; import java.awt.image.BufferedImage; BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics2D g = (Graphics2D) image.getGraphics(); // 1, set the background color Utils.setBackGround(g,width,height); // 2, set the border Utils.setBorder(g,width,height); // 3, draw the line of interference Utils.drawRandomLine(g,width,height); // 4, write random numbers String verifycode = Utils.drawRandomNum(g); // 5, addressed to the browser g.dispose (); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, "JPEG", bos); byte[] buf = bos.toByteArray(); String img = Base64Util.encode(buf);// "/9j/4AAQSkZJRgABAgA" --图片base64 bos.close(); return img; */ /** * Description generate graphics codes * @Param width image width default 96 * @Param height Image Height default 42 * @Return picture base64, img file the string "/ 9j / 4AAQSkZJRgABAgA" removed "data: image / jpeg; base64 ," part * @author * @Date 2019 Nian 9 Yue 20 Ri */ public static String imgVerifycode(int width,int height) throws Exception{ if(width < 96){ width = 96; } if(height < 42){ height = 42; } BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); G the Graphics2D = (the Graphics2D) image.getGraphics (); // . 1, background color setBackground (G, width, height); // 2, set the border the setBorder (G, width, height); // . 3, line drawing interference drawRandomLine (G, width, height); // . 4, the random number nonce write returns String verifycode = drawRandomNum (G); // . 5, the browser written g.dispose (); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, "JPEG", bos); byte[] buf = bos.toByteArray(); String img = Base64Util.encode(buf);// "/9j/4AAQSkZJRgABAgA" --图片base64 bos.close(); return img; } // set the background color public static void setBackground (G Graphics, width Integer, Integer height) { g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); for (int i = 0; i < 120; i++) { int x = (int) (Math.random() * width); int y = (int) (Math.random() * height); int red = (int) (Math.random() * 255); int green = (int) (Math.random() * 255); int blue = (int) (Math.random() * 255); g.setColor(new Color(red, green, blue)); g.drawOval(x, y, 1, 0); } } // set the border public static void the setBorder (G Graphics, width Integer, Integer height) { g.setColor(Color.LIGHT_GRAY); g.drawRect(1, 1, width - 2, height - 2); } // draw line interference public static void drawRandomLine (G Graphics, width Integer, Integer height) { g.setColor(Color.GREEN); for (int i = 0; i < 5; i++) { int x1 = new Random().nextInt(width); int y1 = new Random().nextInt(height); int x2 = new Random().nextInt(width); int y2 = new Random().nextInt(height); g.drawLine(x1, y1, x2, y2); } } // random number generation public static String drawRandomNum (the Graphics2D G) { // set the font color g.setColor (Color.RED); // set the font style g.setFont ( new new the Font ( "Arial", Font.BOLD, 26 is ) ); Rand4 String = "" ; int X =. 5; // location of the font for ( int I = 0; I <. 4; I ++) { // generates four random characters int Degree = new new . The Random () the nextInt () 30% ; // rotation angle int index = new new the Random () the nextInt (STR_NUM.length ()).; String ch = STR_NUM.charAt(index) + ""; rand4 + = ch; g.rotate(degree * Math.PI / 180, x, 20); g.drawString(ch, x, 30); g.rotate(-degree * Math.PI / 180, x, 20); X + = 24; // Font position right 15px } return rand4; } // ==== two-dimensional code generation tool method ====================================== = //