The examples in this article share with you the specific code for Java generation letter verification code 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 { //Picture width and height private int w=70; private int h=35; private Random r=new Random(); private String[] fontNames={"Song font","Chinese kai font","Bold font","Microsoft Elegant Black","Kai font_GB2312"}; private String codes="234567890qwertyuipasdfghjkzxcvbnmQWERTYUIPASDFGHJKZXCVBNM"; private Color bgColor=new Color(255,255,255); private String text; //Generate random colors 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 random font private Font randomFont(){ int index=r.nextInt(fontNames.length); String fontName=fontNames[index]; int style=r.nextInt(4);//0 None 1 Bold 2 Italic 3 Bold + oblique int size=r.nextInt(5)+24; //Generate random font size 24~28 return new Font(fontName, style, size); } //Interference line private void drawLine(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 line color bi.drawLine(x1,y1,x2,y2); } } //Random generation of 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 image 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 accounts for 1/4 width of the image float x=i*1.0F*w/4; //Random font format bi.setFont(randomFont()); bi.setColor(randomColor()); //Write the characters at the appropriate place of the image (h-6 means the image is 6 heights from the bottom) bi.drawString(string, x, h-6); } this.text=sb.toString(); drawLine(image); return image; } //Return the obtained font public String getText(){ return text; } //Write 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(); } }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.