图文验证码的生成

备注:Alibaba Java Coding Guidelines JAVA代码编程提示插件。

生成图片传给前台展示


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

/**
 * <b>function:</b> 验证码生成工具类
 *
 * @author hoojo
 * @project NetWorkServiceclass
 * @package com.hoo.util
 * @fileName VerifyCodeUtils.java
 * @createDate 2010-8-3 下午03:05:50
 */
public class VerifyCodeUtils {
    /*********************************************************************
     * 验证码宽度
     */
    public static final int WIDTH = 90;
    /***
     * 验证码高度
     */
    public static final int HEIGHT = 32;

    /**********************************************************************
     * 验证码背景颜色COLOR_FC_BG 应当小于COLOR_BC_BG
     */
    public static final int COLOR_FC_BG = 200;
    /***
     * 验证码背景颜色COLOR_FC_BG 应当小于COLOR_BC_BG
     */
    public static final int COLOR_BC_BG = 250;

    /**********************************************************************
     * 验证码背景干扰线颜色COLOR_FC_LINE 应当小于COLOR_BC_LINE
     */
    public static final int COLOR_FC_LINE = 160;
    /***
     * 验证码背景干扰线颜色COLOR_FC_LINE 应当小于COLOR_BC_LINE
     */
    public static final int COLOR_BC_LINE = 200;

    /***************************************************************************
     * 验证码颜色COLOR_FC_CODE 应当小于COLOR_BC_CODE
     */
    public static final int COLOR_FC_CODE = 20;
    /***
     * 验证码颜色COLOR_FC_CODE 应当小于COLOR_BC_CODE
     */
    public static final int COLOR_BC_CODE = 170;

    /***************************************************************************
     * 生成在指定范围内的颜色
     * @param fc 范围fc color值 小于255
     * @param bc 范围bc color值 小于255
     * @return Color
     */
    private static Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc < 0) {
            fc = 0;
        }
        if (bc < 0) {
            bc = 1;
        }
        if (fc > 255) {
            fc = 255;
        }
        if (bc > 255) {
            bc = 255;
        }
        if (bc == fc) {
            bc += 10;
        }
        int temp = 0;
        if (bc < fc) {
            temp = bc;
            bc = fc;
            fc = temp;
        }
        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);
    }

    /**
     * <b>function:</b> 生成图片方法
     *
     * @param codeStrs String[]
     * @param response HttpServletResponse
     * @return boolean
     * @throws Exception
     * @createDate 2010-8-3 下午03:06:22
     * @author hoojo
     */
    public static boolean getImage(String[] codeStrs, HttpServletResponse response) throws Exception {
        response.reset();
        response.setContentType("image/jpeg");
        // 设置页面不缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 在内存中创建图象
        BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

        // 获取图形上下文
        Graphics img = image.getGraphics();
        // 生成随机类
        Random random = new Random();

        // 设定背景色
        img.setColor(getRandColor(COLOR_FC_BG, COLOR_BC_BG));
        img.fillRect(0, 0, WIDTH, HEIGHT);

        // 设定字体
        img.setFont(new Font("Times New Roman", Font.PLAIN, 50));

        //  随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
        img.setColor(getRandColor(COLOR_FC_LINE, COLOR_BC_LINE));
        for (int i = 0; i < 15; i++) {
            int x = random.nextInt(WIDTH);
            int y = random.nextInt(HEIGHT);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            img.drawLine(x, y, x + xl, y + yl);
        }

        // 取随机产生的认证码
        for (int i = 0; i < 4; i++) {
            // 随机字体
            img.setFont(getRandomFont());
            // 将认证码显示到图象中
            img.setColor(getRandColor(COLOR_FC_CODE, COLOR_BC_CODE));
            img.drawString(codeStrs[i], 22 * i + 5, 25);
        }
        // 图象生效
        img.dispose();
        // 输出图象到页面
        return ImageIO.write(image, "JPEG", response.getOutputStream());
    }

    /**
     * <b>function:</b> 随机生成字体、文字大小
     *
     * @return
     * @createDate 2010-8-23 上午10:44:22
     * @author hoojo
     */
    public static Font getRandomFont() {
        String[] fonts = {"Georgia", "Verdana", "Arial", "Tahoma", "Time News Roman", "Courier New", "Arial Black", "Quantzite"};
        int fontIndex = (int) Math.round(Math.random() * (fonts.length - 1));
        int fontSize = (int) Math.round(Math.random() * 4 + 26);
        return new Font(fonts[fontIndex], Font.PLAIN, fontSize);
    }
}
复制代码

生成四位随机文字

public String sendVerifyCode(HttpServletResponse response) {
        String codeValue = "";
        String[] codeStrs = new String[4];
        for (int i = 0; i < 4; i++) {
            codeStrs[i] = StringUtil.getRandomChar();
            codeValue = codeValue.concat(codeStrs[i]);
        }
        try {
            VerifyCodeUtils.getImage(codeStrs, response);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return codeValue;
    }
复制代码
public class StringUtil {
    /**
     * 随机生成字符,含大写、小写、数字
     * <b>function:</b> 功能
     *
     * @return
     * @createDate 2010-8-23 上午10:33:55
     * @author hoojo
     */
    public static String getRandomChar() {
        int index = (int) Math.round(Math.random() * 2);
        String randChar;
        switch (index) {
            // 小写字符
            case 0:
            case 1:
                String chars = "abcdefghkmnrstuvwxyz";
                randChar = String.valueOf(chars.charAt((int) (Math.random() * 20)));
                break;
            default: // 数字
                String charsNums = "23456789";
                randChar = String.valueOf(charsNums.charAt((int) (Math.random() * 8)));
                break;
        }
        return randChar;
    }
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值