Paint color code

Without further ado, to see results

 

 Front page:

 

 

 

 

For more back-end source code and paste comments

package com.controller;


import com.tools.InterLine;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@Controller
public class GetImageCode {

/**
* 获取图片验证码
*/
@RequestMapping("/getImageCode")
@ResponseBody
public void getImageCode (the HttpServletRequest Request, the HttpServletResponse Response, the HttpSession the session) throws IOException {
// System.out.println ( "Get authentication code");

/ **
* custom drawing
* /
// image pixel width
int wpx = 80;
// high pixel
int HPX = 30;

// 1. create a transparent color without BufferedImage object, that is, the picture object
BufferedImage BFI = new new BufferedImage (WPX, HPX, BufferedImage.TYPE_INT_RGB);
// painting tools, Photo Paint
Graphics bfi.getGraphics G = ();
// with pre-specified color fill rectangle
g.fillRect (0, 0, WPX, HPX);
// character codes range
char [] = CH "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" .toCharArray ();
// a random number category
the Random R & lt new new = the Random ();
int index;
// save the string
the StringBuffer the StringBuffer new new SB = ();
for (int I = 0; I <. 4; I ++) {
// int generate a random value between [0, n-) interval, i.e. int random value between 0 and n, not including 0 contains n.
= r.nextInt index (ch.length);
// set a different color for each character
g.setColor (new Color (r.nextInt (255 ), r.nextInt (255), r.nextInt (255)));
// set Font
// Font font = new Font ( "Arial", 30, 30);
// italicized, bold, size 30
the Font = new new font the Font ( "Arial", Font.BOLD, 27);
G. setFont (font);
// use the pre-set font and color of text to draw str, the coordinates of the lower left corner of the text (X, Y)
g.drawString (CH [index] + "", (* 20 is I) + 2, 23 is);
/ / string concatenation
sb.append (CH [index]);
}
// System.out.println (SB);


// add noise, more noise, not the identification
// noise area, where the entire set of pictures 2 percent
int Area = (int) (* 0.04 * WPX HPX);
for (int I = 0; I <Area; I ++) {
// set the random noise coordinates, width and height of image pixels not exceed
int x = (int ) (Math.random () * WPX);
int Y = (int) (Math.random () * HPX);
// Sets a pixel BufferedImage to the specified RGB values of the colors on the screen are by a set of RGB values is recorded and expressed.
// mean parameters: x coordinate, y coordinate, the brightness of the RGB color values, this is not a painting, a noise image is provided
bfi.setRGB (X, Y, (int) (Math.random () * 255));
}

// set the codes interference line
// number of interfering lines
int = COUNT. 6;
// call to set the RGB method
InterLine interLine Interline new new = ();
for (int I = 0; I <COUNT; I ++) {
// Get random disturbance start and end lines, i.e., the beginning and ending coordinates of two points
// start coordinates
int xstart = (int) (Math.random () * WPX);
int yStart = (int) (Math.random () * HPX);
// end coordinates
int the xend = (int) (Math.random () * WPX);
int 's ending = ( int) (Math.random () * HPX);
// set the line color
color color interLine.interLine = (. 1, 255);
g.setColor (color);
g.drawLine (Xstart, yStart, the xend, 's ending);
}
// set the session
the HttpSession Request.getSession the HttpSession = ();
HttpSession.setAttribute ( "verificationCode", SB);
// String String verificationCode new new = ((the StringBuffer) the session .getAttribute ( "verificationCode"));
// System.out.println ( "output SB:" + verificationCode);
// output stream pictures, meaning parameters: the picture object, the picture suffix format output stream in what way transmission, if remote control programs to be transmitted websokect
ImageIO.write (BFI, "JPG", response.getOutputStream ());

}
}

 Another tool categories:

package com.tools;

import java.awt.*;

public class InterLine {
public Color interLine(int Low, int High) {
if (Low > 255)
Low = 255;
if (High > 255)
High = 255;
if (Low < 0)
Low = 0;
if (High < 0)
High = 0;
//RGB参数范围,最大范围在0~255
int interval = High - Low;
int r = Low + (int) (Math.random() * interval);
int g = Low + (int) (Math.random() * interval);
int b = Low + (int) (Math.random() * interval);
return new Color(r, g, b);
}
}

 

Guess you like

Origin www.cnblogs.com/c2g5201314/p/11601284.html