Java验证码图片生成工具类

验证码是为了区分进行操作的是否是人,一般常见于登录、注册等。使用验证码可以防止有人恶意注册等操作。

使用java编写一个生成验证码图片生成工具类。主要使用java的绘图类来实现。

验证码图片生成工具类代码

实例化一些需要用到的值

//验证码宽度

private int width=70;

//验证码高度

private int height=35;

//实例化一个Random对象,用于生成随机数、颜色等

private Random random = new Random();

//字体

private String[] fontName={"宋体","黑体","微软雅黑","华文行楷","楷体_GB2312"};

//生成的字符1-9 a-z

private String str ="23456789abcdefghjkmnopqrstuvwxyzABCDFGHJKMNOPQRSTUVWXYZ";

private Color fontColor = new Color(255,255,255); //字体颜色

private String text; //当前的验证码

//随机颜色生成 rgb 注意nextInt()里面的值,不能太大。

    private Color randomColor(){

        int red =random.nextInt(150);

        int green= random.nextInt(150);

        int blue = random.nextInt(150);

        return new Color(red,green,blue);

    }

    //随机字体 返回随机大小、样式的字体

    private Font randomFont(){

        int index = random.nextInt(fontName.length);

        String name = fontName[index];//获取随机后的字体

        int style =random.nextInt(4);

        int fontsize = random.nextInt(5)+22;

        return new Font(name, style,fontsize);

    }

/*返回随机字符 获取字符串长度,将返回的随机数作为String.charAt()方法的参数返回该索引的字符*/

    private char randomChar(){

        int num = random.nextInt(str.length());

        return str.charAt(num);

    }

上面三个方法都是返回相对应的返回值,使用实例化好的Random对象返回一个随机int值,根据返回随机int值来获取定义好的值从而返回我们看到的随机字符等。

返回验证码图片关键是绘制图片,在java中可以使用BufferedImage、Graphics、Graphics2D、Color等对象进行绘图

下面方法返回一个BufferedImage, BufferedImage可以想象为画板,主要使用Graphics2D对象进行绘制图片。

//绘制图片

    public BufferedImage createImae(){

        BufferedImage buffimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

//得到Graphics2D对象

        Graphics2D gp2d = (Graphics2D) buffimg.getGraphics();

        gp2d.setColor(this.randomColor());//设置颜色

        gp2d.fillRect(0, 0, width, height); //绘制矩形

        return buffimg;

    }

验证码是需要干扰线条,防止机器分别。可以使用Graphics类的drawLine()方法进行绘制线条,使用一个循坏来绘制三条不同的随机线。

//绘制线条

    private void drawLine(BufferedImage bufferedimg){

       //绘制线条数

        int num =3;

       /*得到Graphics2D对象,只有从这个BufferedImage得到这个对象才能对这图片进行绘制*/

        Graphics2D gp2d = (Graphics2D)bufferedimg.getGraphics();

        for (int i = 0; i < num; i++) {

            int x = random.nextInt(width);//

            int y = random.nextInt(height);

            int x1 =random.nextInt(width);

            int y1= random.nextInt(height);

            //设置笔画属性

            gp2d.setStroke(new BasicStroke(1.5f));

            //随机颜色

gp2d.setColor(randomColor());

//根据随机的参数进行绘制线条

            gp2d.drawLine(x, y, x1, y1);  

        }

    }

 

是调用createImae()方法获取到图片,根据当前的BufferedImage得到Graphics2D对象,再循坏向图片绘制随机字符,其中字体颜色、样式、大小等都是随机的,将当前字符串(也就是验证码)保存到变量中以便调用,再调用drawLine线条进行绘制线条达到干扰线的作用,返回验证码图片。

//返回验证码图片

public BufferedImage getImage(){

        BufferedImage image = createImae();

        Graphics2D grap2d = (Graphics2D) image.getGraphics();

        StringBuilder str = new StringBuilder();

        //向图片绘制4个字符串

        for (int i = 0; i < 4; i++) {

            String a1 = randomChar()+"";

            str.append(a1);

            float b = i*1.0F*width/4;

            grap2d.setFont(randomFont());

            grap2d.setColor(randomColor());

            grap2d.drawString(a1, b, height-5);

        }

        this.text = str.toString();

        drawLine(image);

        return image;

    }

//获取当前验证码

public String getText(){

        return text;

    }

下面方法是输出当前验证码图片,得到一个格式为png的图片文件

//输出图片文件

    public   void out(BufferedImage image,OutputStream out) throws IOException{

    ImageIO.write(image, "png", out);

    }

@Test

public void test() {

        Demo4 demo = new Demo4();

        BufferedImage image = demo.getImage();

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

        try {

            demo.out(image, new FileOutputStream("D:/验证码.png"));

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();}}

运行上面test()方法,效果如下

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值