验证码工具类

验证码工具类详解:生成图像与配置参数
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @Description: 验证码工具类 ,可以设置背景大体颜色,验证码长度,图像内容,图像大小等等属性,最终的到一个image 对像
 * @author: 007
 * @CreateDate: 
 * @version: 
 */
public class ImageUtil
{

    /**
     * 随机类
     */
    private Random random = new Random();

    /**
     * 验证码值
     */
    private String sRandom = "";

    /**
     * 验证码长度
     */
    private int imageLength = 4;

    /**
     * 随机线数量
     */
    private int randLineCount = 100;

    /**
     * 宽度
     */
    private int width = 60;

    /**
     * 长度
     */
    private int height = 25;

    /**
     * 验证码图像
     */
    private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    /**
     * 获取图形上下文
     */
    private Graphics graphics = image.getGraphics();

    /**
     * 字体类
     */
    private Font font = new Font("Times New Roman", Font.PLAIN, 18);

    /**
     * 验证码显示字符库
     */
    private char[] selectChar = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
            'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
            'Z' };

    /**
     * 随机线颜色起始值
     */
    private int lfCloor = 120;

    /**
     * 随机线颜色结束值
     */
    private int lbCloor = 130;

    /**
     * 
     */
    private int fCloor = 195;

    /**
     * 
     */
    private int bCloor = 205;

    /**
     * 获得验证码的值大写值
     * 
     * @return
     */
    public String getsRandom()
    {
        return sRandom.toUpperCase();
    }

    /**
     * 获取验证码图像公开方法
     * 
     * return 图像
     */
    public BufferedImage getImage()
    {
        DrawImage();
        return image;
    }

    /**
     * 随机划线
     */
    private void drawRandomLine()
    {
        graphics.setColor(getRandColor(lfCloor, lbCloor));
        for (int i = 0; i < randLineCount; i++)
        {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            graphics.drawLine(x, y, x + xl, y + yl);
        }
    }

    /**
     * 生成图验证码图像
     * 
     */
    private void DrawImage()
    {
        // 设定背景色
        graphics.setColor(getRandColor(fCloor, bCloor));

        //换背景图
        graphics.fillRect(0, 0, width, height);

        //设定字体
        graphics.setFont(font);

        //生成随机线
        drawRandomLine();

        //生成随机字符
        for (int i = 0; i < imageLength; i++)
        {
            int charIndex = random.nextInt(selectChar.length);
            String rand = "" + selectChar[charIndex];
            sRandom += rand;
            //调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
            graphics.setColor(new Color(20 + random.nextInt(110), 40 + random.nextInt(110), 60 + random.nextInt(110)));
            graphics.drawString(rand, 13 * i - (random.nextInt(5) * 2) + 6, 16);
        }
        graphics.dispose();
    }

    /**
     * 按给定范围产生随机颜色
     * 
     * @param 开始颜色指数
     * @param 结束颜色指数
     * @return 颜色对象
     */

    private Color getRandColor(int fc, int bc)
    {
        //给定范围获得随机颜色
        Random random = new Random();
        if (fc > 255)
        {
            fc = 255;
        }
        if (bc > 255)
        {
            bc = 255;
        }
        int r = fc + random.nextInt(bc - fc);
        int g = fc + random.nextInt(bc - fc);
        int b = fc + random.nextInt(bc - fc);
        return new Color(r, g, b);
    }

    public void setSelectChar(final char[] selectChar)
    {
        this.selectChar = selectChar;
    }

    public void setImageLength(final int imageLength)
    {
        this.imageLength = imageLength;
    }

    public void setRandLineCount(final int randLineCount)
    {
        this.randLineCount = randLineCount;
    }

    public void setWidth(final int width)
    {
        this.width = width;
    }

    public void setHeight(final int height)
    {
        this.height = height;
    }

    public void setFont(final Font font)
    {
        this.font = font;
    }

    public void setLfCloor(final int lfCloor)
    {
        this.lfCloor = lfCloor;
    }

    public void setLbCloor(final int lbCloor)
    {
        this.lbCloor = lbCloor;
    }

    public void setfCloor(final int fCloor)
    {
        this.fCloor = fCloor;
    }

    public void setbCloor(final int bCloor)
    {
        this.bCloor = bCloor;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值