Generate the letter captcha using java

  • 2020-05-24 05:32:56
  • OfStack

The example of this article is to share the specific code of the letter verification code generated by java for your reference. The specific content is as follows


import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;


public class VerifyCode {
  // High image width 
  private int w=70;
  private int h=35;
  private Random r=new Random();
  private String[] fontNames={" Song typeface "," Chinese regular script "," blackbody "," Microsoft jas black "," Regular script _GB2312"};
  private String codes="234567890qwertyuipasdfghjkzxcvbnmQWERTYUIPASDFGHJKZXCVBNM";
  private Color bgColor=new Color(255,255,255);
  private String text;
  // Generate a random color 
  private Color randomColor(){
    int red=r.nextInt(150);
    int green=r.nextInt(150);
    int blue=r.nextInt(150);
    return new Color(red, green, blue);
    }
  // Generate a random font 
  private Font randomFont(){
    int index=r.nextInt(fontNames.length);
    String fontName=fontNames[index];
    int style=r.nextInt(4);//0  There is no  1  The bold  2  italics  3  coarse + oblique 
    int size=r.nextInt(5)+24; // Generate random size  24~28
    return new Font(fontName, style, size);
  }
  // Interference lines 
  private void drowLine(BufferedImage image){
    // generate 4 Interference lines 
    int num=4;
    Graphics2D bi=(Graphics2D) image.getGraphics();
    for (int i = 0; i < num; i++) {
      int x1=r.nextInt(w);
      int x2=r.nextInt(w);
      int y1=r.nextInt(h);
      int y2=r.nextInt(h);
      bi.setStroke(new BasicStroke(1.5F));
      bi.setColor(Color.BLUE);// Interference lines color 
      bi.drawLine(x1,y1,x2,y2);
    }
  }
  // Randomly generated 1 A character 
  private char randomChar(){
    int index=r.nextInt(codes.length());
    return codes.charAt(index);
  }
  // Image buffer 
  private BufferedImage createImage(){
    BufferedImage image=new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
    Graphics2D bi=(Graphics2D) image.getGraphics();
    bi.setColor(this.bgColor);
    bi.fillRect(0, 0, w, h);
    return image;
  }
  // Generate images 
  public BufferedImage getImage(){
    BufferedImage image=createImage();
    Graphics2D bi=(Graphics2D) image.getGraphics();
    StringBuilder sb=new StringBuilder();
    for (int i = 0; i <4; i++) {
      String string=randomChar()+"";
      sb.append(string);
      // Each character takes up the picture 1/4 wide 
      float x=i*1.0F*w/4;
      // Random font format 
      bi.setFont(randomFont());
      bi.setColor(randomColor());
      // Write the words where appropriate ( h-6 Refers to the bottom of the picture 6 A height) 
      bi.drawString(string, x, h-6);
    }
    this.text=sb.toString();
    drowLine(image);
    return image;
  }
  // Returns the resulting font 
  public String getText(){
    return text;
  }
  // Writes the image to the specified location 
  public static void output(BufferedImage image,OutputStream out){
    try {
      ImageIO.write(image, "JPG", out);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}


Related articles: