web123456

Java code generation image verification code implementation

  • package ;
  • import ;
  • import ;
  • import .Graphics2D;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • import ;
  • /**
  • * Verification code generator
  • */
  • public class ValidateCode {
  • // The width of the picture.
  • private int width = 160;
  • // The height of the picture.
  • private int height = 40;
  • // Number of verification code characters
  • private int codeCount = 5;
  • // Number of interference lines of verification code
  • private int lineCount = 150;
  • // Verification code
  • private static String code = null;
  • // Verification code picture Buffer
  • private BufferedImage buffImg=null;
  • private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L',
  • 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y',
  • 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  • public ValidateCode() {
  • this.createCode();
  • }
  • /**
  • *
  • * @paramwidth picture width
  • * @paramheight picture high
  • */
  • public ValidateCode(int width,int height) {
  • this.width=width;
  • this.height=height;
  • this.createCode();
  • }
  • /**
  • *
  • * @paramwidth picture width
  • * @paramheight picture high
  • * @paramcodeCount number of characters
  • * @paramlineCount Number of interfering lines
  • */
  • public ValidateCode(int width,int height,int codeCount,int lineCount) {
  • this.width=width;
  • this.height=height;
  • this.codeCount=codeCount;
  • this.lineCount=lineCount;
  • this.createCode();
  • }
  • public void createCode() {
  • int x = 0,fontHeight=0,codeY=0;
  • int red = 0, green = 0, blue = 0;
  • x = width / (codeCount +2);//Width of each character
  • fontHeight = height - 2;//The height of the font
  • codeY = height - 4;
  • // Image buffer
  • buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  • Graphics2D g = ();
  • // Generate random numbers
  • Random random = new Random();
  • // Fill the image into white
  • ();
  • (0, 0, width, height);
  • // Create fonts
  • ImgFontByte imgFont=new ImgFontByte();
  • Font font =(fontHeight);
  • (font);
  • for (int i = 0; i < lineCount; i++) {
  • int xs = (width);
  • int ys = (height);
  • int xe = xs+(width/8);
  • int ye = ys+(height/8);
  • red = (255);
  • green = (255);
  • blue = (255);
  • (new Color(red, green, blue));
  • (xs, ys, xe, ye);
  • }
  • // randomCode records randomly generated verification code
  • StringBuffer randomCode = new StringBuffer();
  • // Randomly generate a codeCount character verification code.
  • for (int i = 0; i < codeCount; i++) {
  • String strRand = (codeSequence[()]);
  • // Generate random color values ​​so that the color values ​​of each character output will be different.
  • red = (255);
  • green = (255);
  • blue = (255);
  • (new Color(red, green, blue));
  • (strRand, (i + 1) * x, codeY);
  • // Combine the generated four random numbers together.
  • (strRand);
  • }
  • // Save the four-digit verification code to the Session.
  • code = ();
  • }
  • public void write(String path) throws IOException {
  • OutputStream sos = new FileOutputStream(path);
  • this.write(sos);
  • }
  • public void write(OutputStream sos) throws IOException {
  • (buffImg, "png", sos);
  • ();
  • }
  • public BufferedImage getBuffImg() {
  • return buffImg;
  • }
  • public static String getCode() {
  • return code;
  • }
  • }