验证码随机生成
- 无需赘述,这个项目很简单。
- 主要是就是熟悉一下java里面的画板 画笔以及随机类的使用
public class ImageCode {
static String[] strs = {"a","b","c","d","e","f","g","h",
"i","j","k","m","n","p","q","r","s","t","u",
"v","w","x","y","z","2","3","4","5","6","7","8","9"};
public static void main(String[] args) throws IOException {
int w = 150;
int h = 50;
int imageType = BufferedImage.TYPE_INT_RGB;
BufferedImage image = new BufferedImage(w,h,imageType);
Graphics g = image.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0,0,w,h);
g.setColor(Color.BLACK);
g.setFont(new Font("楷体",Font.PLAIN,25));
Random random = new Random();
int x = 25;
int y = 30;
for (int i = 0; i < 4; i++) {
int num = random.nextInt(strs.length);
String str = strs[num];
g.drawString(str,x,y);
x += 30;
}
g.setColor(Color.GREEN);
for (int i = 0; i < 10; i++) {
int x1 = random.nextInt(30);
int y1 = random.nextInt(50);
int x2 = random.nextInt(30)+120;
int y2 = random.nextInt(50);
g.drawLine(x1,y1,x2,y2);
}
ImageIO.write(image,"jpg",new File("E:\\gitee\\java-study\\capImage\\aaa.jpg"));
}
}