java 生成验证码
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
public class Test {
public static void main(String[] args) throws Exception {
Random random = new Random();
BufferedImage image = new BufferedImage(100, 50, BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.fillRect(0, 0, 100, 50);
String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
char[] cs = words.toCharArray();
for (int i = 0; i <= 3; i++) {
graphics.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
graphics.setFont(new Font("微软雅黑", Font.BOLD, 30));
char c = cs[random.nextInt(cs.length)];
graphics.drawString(c + "", i * 20, 30);
}
int max = random.nextInt(10);
for (int i = 0; i < max; i++) {
graphics.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
graphics.drawLine(random.nextInt(100), random.nextInt(50), random.nextInt(100), random.nextInt(50));
}
int max2 = random.nextInt(10);
for (int i = 0; i < max2; i++) {
graphics.setColor(new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256)));
graphics.drawOval(random.nextInt(80), random.nextInt(40), random.nextInt(5), random.nextInt(10));
}
ImageIO.write(image, "jpg", new File("D:\\验证码.jpg"));
}
}