java编写简单漂亮的验证码

代码如下

package com.bdqn.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import javax.imageio.ImageIO;

import org.apache.struts2.ServletActionContext;

/**
 * 生成验证码
 * <p>Title:RandomCodeGenerator<p>
 * <p>Description: </p>
 * <p>Company: </p>
 * @author 123456
 * <p>yangyang</p>
 */

public class RandomCodeGenerator {
    private static final char[] CHARS = "abcdefghijkmnpqrstovwxyz0123456789".toCharArray();
    private int length;
    private int fontSize = 14;
    private String randomCode = null;

    public RandomCodeGenerator(int length) {
        this.length = length;
    }

    public RandomCodeGenerator() {
        this(4);
    }

    public String getRandomCode() {
        if (randomCode == null) {
            // 生成验证码
            this.randomCode = initRandomCode();
        }
        return randomCode;
    }

    // 初始化验证码
    private String initRandomCode() {
        Set<Character> chars = new HashSet<>();
        Random r = new Random();
        while(chars.size() != length) {
            chars.add(CHARS[r.nextInt(CHARS.length)]);
        }

        char[] cs = new char[chars.size()];
        int index = 0;
        for (char c : chars) {
            cs[index++] = c;
        }
        return new String(cs).toUpperCase();
    }

    public BufferedImage generator(OutputStream output) throws IOException {
        int width = length * (fontSize + 2) + 10;
        int height = fontSize + 10;
        //创建一个缓存图片对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //得到画笔对象
        Graphics2D g = (Graphics2D)image.getGraphics();
        //设置前景色
        g.setColor(Color.PINK);
        //使用前景色填充距形
        g.fillRect(0, 0, width, height);
        Random r = new Random();
        //绘制干扰线
        for(int i = 0; i < 3; i++){
            //生成随机颜色
            Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
            g.setColor(c);
            //画线
            g.drawLine(r.nextInt(width / 2), r.nextInt(height), r.nextInt(width / 2) + width / 2, r.nextInt(height));
        }

        g.setColor(Color.WHITE);
        Font font = new Font("Cooper Std", Font.PLAIN, fontSize);
        //设置字体
        g.setFont(font);
        if(randomCode == null)
            this.randomCode = getRandomCode();
        for(int i = 0; i < randomCode.length(); i++){
            //画字符
            g.drawString(randomCode.substring(i, i+1), 8 + fontSize * i, height / 2 + fontSize / 2);
        }
        //完成,并将验证码放入session中
        //此处是将验证码存入session中进行验证码验证              //ServletActionContext.getRequest().getSession().setAttribute("checkCode", randomCode);
        image.flush();
        //输出
        ImageIO.write(image, "PNG", output);
        return image;

    }

    public int getFontSize() {
        return fontSize;
    }

    public void setFontSize(int fontSize) {
        this.fontSize = fontSize;
    }


    public static void main(String[] args) {
        RandomCodeGenerator rcg = new RandomCodeGenerator(6);
        System.out.println(rcg.getRandomCode());
        try {
            rcg.generator(new FileOutputStream("F:/a.png"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

效果图如下:
这里写图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值