验证码的实现(4字符+干扰线)

生成验证码(4位字符+干扰线)

这里用到了swing图形化技术

在这里封装成工具类

public class VerifyCode {
    private int width = 100;//宽度
    private int height = 35;//高度
    private Random r = new Random();//随机产生数的对象
    private String[] fontNames = {"宋体","华文楷体","黑体","隶书","微软雅黑","Tahoma","Segoe Print"};//定义一个字体的数组
    private String codes = "123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIPLKJHGFDSAZXCVBNM";//字符
    private Color bgColor = new Color(255, 255, 255);//颜色(r,g,b)
    private String text;

    //生成一个颜色
    public Color randomColor(){
        int red = r.nextInt(200); 
        int green = r.nextInt(200); 
        int blue = r.nextInt(200); 
        return new Color(red,green,blue);
    }

    //生成随机字体
    public Font randomFont(){
        int index  = r.nextInt(fontNames.length);//随机生成的字体在数组中的下标
        String fontName = fontNames[index];//利用下标得到字体名称

        int style = r.nextInt(4);
        int size = r.nextInt(6)+20;

        return new Font(fontName, style, size);//1、字体  ;2、样式(0,表示无样式;1,表示粗体;2,斜体;3,表示粗体加斜体)3,大小

    }

    //生成一个字符
    public char randomChar(){
        int index  = r.nextInt(codes.length());
        return codes.charAt(index);
    }


    //画干扰线
    public void drawLine(BufferedImage image){

        Graphics2D graph =  (Graphics2D) image.getGraphics();//得到画笔
        int len = r.nextInt(3)+3;
        //生成两个坐标点,4个值
        for(int i=0;i<len;i++){
            int x1 = r.nextInt(width);
            int y1 = r.nextInt(height);
            int x2 = r.nextInt(width);
            int y2 = r.nextInt(height);
        //坐标不能超出所给框的范围
            graph.setColor(randomColor());//设置笔的颜色
            graph.setStroke(new BasicStroke(1.2f));//设置线的粗细

            graph.drawLine(x1, y1, x2, y2);//画线
        }
    }

    //创建BufferedImage对象  
    public BufferedImage createImage(){
        //创建图片缓冲区
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics2D graph =  (Graphics2D) image.getGraphics();//得到画笔
        //设置笔的颜色
        graph.setColor(bgColor);
        graph.fillRect(0, 0, width, height);
        return image;
    }
    //图片上画4个字符
    public BufferedImage getImage(){
        BufferedImage image = createImage();//得到矩型
        Graphics2D graph =  (Graphics2D) image.getGraphics();//得到画笔
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 4; i++) {//向图片上画4个字符
            String str = randomChar()+"";
            sb.append(str);
            graph.setFont(randomFont());//设置画笔的字体
            graph.setColor(randomColor());//设置画笔的颜色
            float x = i * 1.0f * width / 4;//设置当前字符的X位置
            graph.drawString(str, x, height-5);
        }

        text = sb.toString();//4个字符
        drawLine(image);
        return image;
    }


    //得到字符串
    public String getText(){
        return text;
    }


    //保存图片到指定位置
    public void output(BufferedImage image,OutputStream out){
        try {
            ImageIO.write(image, "JPEG", out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //测试方法

    public static void main(String[] args) throws FileNotFoundException {

        VerifyCode verify = new VerifyCode();
        BufferedImage image = verify.getImage();
        OutputStream out = new FileOutputStream("E:\\XXX.jpg");
        verify.output(image, out);

        System.out.println(verify.getText());


    }



}

在具体的JSP实现界面中,对其进行实例化得到实例化对象就OK。

生成图片如下


not pleased by external gains,not saddened bypersonnal losses;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值