Java生成图形验证码

生成图形验证码:
package common;

/**
 * Description: xxxxx
 *
 * @author :  zhoushiwen
 * Created_Date : 2017/9/8  15:23
 */
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

/**
 * 验证码工具类
 * 文件名:CaptchaUtil.java
 */
public class CaptchaUtil {
    private Integer width;//验证码图片宽度
    private Integer height;//验证吗图片高度
    private Integer num;//验证码的个数
    private String code;//生成验证码一组字符串

    private static final Random ran = new Random();//随机数
    private static CaptchaUtil captchaUtil;

    /**
     * 通过默认构造初始化参数
     */
    public CaptchaUtil(){
        width = 80;
        height = 30;
        code = "123456789adcdefghijklmnopqrstuvwxyz";
        num = 4;
    }

    /**
     * 利用单利模式创建该验证码工具类
     * @return
     */
    public static CaptchaUtil getInstance(){
        if(captchaUtil==null){
            captchaUtil=new CaptchaUtil();
        }
        return captchaUtil;
    }
    public Integer getWidth() {
        return width;
    }

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

    public Integer getHeight() {
        return height;
    }

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

    public Integer getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public static Random getRan() {
        return ran;
    }

    public void setCaptchaUtil(Integer width,Integer height,Integer num,String code){
        this.width=width;
        this.height=height;
        this.num=num;
        this.code=code;
    }

    public void setCaptchaUtil(Integer width,Integer height,String code){
        this.width=width;
        this.height=height;
        this.code=code;
    }
    /**
     * 随机生成验证码
     * @param code 生成验证码的一组字符串
     * @return
     */
    public String getCreateCode(){
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            sb.append(code.charAt(ran.nextInt(code.length())));
        }
        return sb.toString();
    }

    /**
     * 生成buffere Image图片
     * @param finshCode 生成好的验证码字符串
     * @return
     */
    public BufferedImage getCreateImg(String finshCode){
        // 创建BufferedImage对象
        BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
        Graphics2D graphic = img.createGraphics();// 创建画笔
        graphic.setColor(Color.WHITE);// 设置颜色
        graphic.fillRect(0, 0, width, height);//是用预定的颜色填充一个矩形,得到一个着色的矩形块。
        graphic.setColor(new Color(213, 213, 213));//设置边框颜色
        graphic.drawRect(0, 0, width - 1, height - 1);// 画正方形
        // 设置字体 风格 风格 高度
        Font font = new Font("宋体", Font.BOLD + Font.ITALIC,(int) (height * 0.8));
        graphic.setFont(font);
        for (int i = 0; i < num; i++) {
            // 随机设置字体RGB颜色
            graphic.setColor(new Color(ran.nextInt(200), ran.nextInt(200),ran.nextInt(200)));
            // 画出验证码
            graphic.drawString(String.valueOf(finshCode.charAt(i)), i* (width / num) + 4, (int) (height * 0.8));
        }
        for (int i = 0; i < (width + height); i++) {
            // 随机设置字体RGB颜色
            graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255),ran.nextInt(255)));
            // 生成干扰点
            graphic.drawOval(ran.nextInt(width), ran.nextInt(height), 1, 1);
        }
        for(int i = 0; i < 3; i++){
            // 随机设置字体RGB颜色
            graphic.setColor(new Color(ran.nextInt(255), ran.nextInt(255),ran.nextInt(255)));
            // 随机生成干扰线
            graphic.drawLine(0, ran.nextInt(height), width,ran.nextInt(height));
        }
        return img;
    }

//    @RequestMapping("captcha.html")
//    public void captcha(HttpServletResponse response, HttpSession session) {
//        OutputStream out = null;
//        try {
//            // 设置响应类型
//            response.setContentType("image/jpg");
//            // 获取创建验证码工具类实例
//            CaptchaUtil yzm = CaptchaUtil.getInstance();
//            // 获取生成的验证码字符串
//            String code = yzm.getCreateYZMCode();
//            // 将验证码存放在session
//            session.setAttribute("code", code);
//            // 获取验证码图片
//            BufferedImage img = yzm.getCreateYZMImg(code);
//            out = response.getOutputStream();
//            // 通过ImageIO写出图片
//            ImageIO.write(img, "jpg", out);
//            out.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (out != null) {
//                try {
//                    out.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }

    @Test
    public void captcha() {
        OutputStream out = null;
        try {

            // 获取创建验证码工具类实例
            CaptchaUtil yzm = CaptchaUtil.getInstance();
            // 获取生成的验证码字符串
            String code = yzm.getCode();

            // 获取验证码图片
            BufferedImage img = yzm.getCreateImg(code);

            // 通过ImageIO写出图片
            ImageIO.write(img, "jpg", new File("D:\\abc.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值