欢迎使用CSDN-markdown编辑器

简化版(没有扭曲设置的可选字体也很少)随机生成验证码

package demo;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Stroke;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import javax.imageio.ImageIO;
import javax.swing.text.StyledEditorKit.ForegroundAction;
import org.w3c.dom.css.RGBColor;
//验证码类
public class Verifaction {

    public Verifaction(){

    }
    //验证码的长度和高度
    private int w = 200;
    private int h = 80;
    //验证码中出现的字符
    private String source = "234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    //验证码字体
    private String[] fontString =new String[]{"华文楷体","宋体","黑体","微软雅黑","楷体_GB2312"};
    private String[] textString = new String[4];
    private Random random = new Random();
    private Color bgColor =Color.BLACK; 
    //随机设置颜色方法
    public Color randomColor(){
        int red = random.nextInt(150);
        int green = random.nextInt(150);
        int blue = random.nextInt(150);
        return new Color(red,green,blue);
    }
    //创建验证码方法  count为生成验证码数量
    //实现了随机选择字符随机选择字体随机选择字体颜色
    //并且在验证码上画上了四条干扰线
    //字体可以自己多加几个。。。
    public void creatVerifaction(int Count) throws Exception, IOException{
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2d = (Graphics2D)image.getGraphics();
        for(int j=0;j<Count;j++){
            getTextString();
            graphics2d.setColor(Color.white);
            graphics2d.fillRect(0, 0, w, h);
            int x= 20;
            int y = 45;
            for(int i=0;i<4;i++){
                graphics2d.setColor(randomColor());
                graphics2d.setFont(new Font(fontString[random.nextInt(fontString.length)],Font.BOLD,45));
                graphics2d.drawString(textString[i], x+=30, y);
                int x2=0,y2=0,x3=0,y3=0;
                x2 = random.nextInt(w);
                y2 = random.nextInt(h);
                x3 = random.nextInt(w);
                y3 = random.nextInt(h);
                Line2D l1 = new Line2D.Float(x2,y2,x3,y3);
                graphics2d.setStroke(new BasicStroke(5.0f));
                graphics2d.draw(l1);

            }
            //把生成的验证码放在F盘根目录下
            //自己可以修改方法传入路径
            String fileString = "F:/"+j+".jpg";
            ImageIO.write(image, "JPEG", new FileOutputStream(fileString));
        }



    }
    //获取随机验证码文本
    public void getTextString(){
        String tempString="";
        for(int i=0;i<4;i++){
            int pos = random.nextInt(source.length());
            tempString +=source.charAt(pos);
            tempString+=' ';
        }
        textString = tempString.split(" ");
    }
    public static void main(String[] sss) throws IOException, Exception{

//      GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
//      String[] ffontNames = ge.getAvailableFontFamilyNames();
//      
//      for(String s:ffontNames){
//          System.out.println(s);
//      }
//      System.out.println(ffontNames.length);
        Verifaction demo = new Verifaction();
        demo.creatVerifaction(10);
        System.out.println("输出结束");
    }

}

原创作品 一切解释权归作者所有!转载请注明出处!
链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值