java编写简单漂亮的验证码

来源:互联网 发布:墨尔本海鲜市场 知乎 编辑:程序博客网 时间:2024/04/28 09:30

代码如下

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();        }    }}

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

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 中国移动客户端密码忘记了怎么办 再歪一点授权码绑定怎么办 网易邮箱号忘了怎么办 忘记支付宝账号和密码怎么办 支付宝账号密码忘了怎么办 发邮箱文件超2g怎么办 报考计算机二级邮箱不存在怎么办 苹果邮箱登录要imap密码怎么办 注销微信支付后怎么办 手机卡注销后支付宝怎么办 12306手机邮箱都换了怎么办 网易手机邮箱手机换了怎么办 崩坏3号被盗了怎么办 qq账号被永久冻结了怎么办 淘宝账号被永久冻结了怎么办 qq账号被永久冻结怎么办 多多理财账号冻结了怎么办 苹果id安全问题忘记了怎么办 网易邮箱帐号忘了怎么办 网易邮箱密码忘记了怎么办 网易邮箱密码忘了怎么办 网易邮箱忘记邮箱账号怎么办 崩坏3三无号被盗怎么办 qq邮箱给58占用怎么办 支付宝邮箱被占用怎么办 苹果手机下载不了qq怎么办 附件预览时发生错误怎么办 手机下载的压缩包打不开怎么办 邮箱提示中转站剩余容量不足怎么办 手机邮箱密码忘记了怎么办 垃圾邮件被系统删除了怎么办 邮箱里的邮件下载不了怎么办 qq邮箱下载不了文件怎么办 苹果手机忘记邮箱密码怎么办 oppo手机安装包损坏怎么办 手机安装包损坏无法安装怎么办 手机qq安装包损坏了怎么办 安装包损坏无法安装怎么办 方舟生存进化安装包损坏怎么办 如果安装包坏了怎么办 酷狗下载音乐收费怎么办