public class validateCode{
private static Random rand = new Random();
public static void main(String[] args){
int val1 = rand.nextInt(9);
int val2 = rand.nextInt(9);
int val3 = rand.nextInt(9);
int val4 = rand.nextInt(9);
String val = val1 + " " + val2 + " " + val3 + " " + val4'
BufferedImage buf = drawImage(val);
//将最终的图片保存到D://cheng.png下
ImageIO.write(buf,"png",new File("D://cheng.png");
}
public static BufferedImage drawImage(String code){
int height = 30;
int width = 60;
BufferedImage buf = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D gs = buf.createGraphics();
gs.setBackground(Color.black);
gs.drawRect(0,0,width,height);
//绘制随机干扰线
int total = 100;
drawRandLine(gs,total);
//绘制验证码
Font font = new Font("行楷",Font.BOLD,20);
gs.setFont(font);
gs.setColor(getRandColor(155,255));
gs.drawString(code,5,20);
return buf;
}
public static void drawRandLine(Graphics2D gs,int total){
for(int i=0; i<total; i++){
int x1 = rand.nextInt(width);
int x2 = rand.nextInt(width);
int y1 = rand.nextInt(height);
int y2 = rand.nextInt(height);
//设置随机颜色
gs.setColor(getRandColor(0,155));
gs.drawLine(x1,y1,x2,y2);
}
}
public static Color getRandColor(int from,int to){
int r = from + rand.nextInt(to-from);
int g = from + rand.nextInt(to-from);
int b = from + rand.nextInt(to-from);
return new Color(r,g,b);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有