java生成验证码

3 篇文章 0 订阅

 前端

<input id="Id" type="text" class="form-control" placeholder="请输入验证码...">
<img id="verifyCode" src="/getVerifyCode.do" οnclick="refersh()"/>

<script>
        function refersh() {
            var date = new Date(); // 创建一个 Date 对象的 一个 实例
            $("#verifyCode")[0].src = "/getVerifyCode.do?" + date.getTime();
        }

</script>

后台

@RequestMapping("getVerifyCode.do")
    public void getVerifyCode(HttpServletRequest request, HttpServletResponse response){
        try {
            HttpSession session = request.getSession();
            String verifyCode = VerifyCode.createVerifyCode(response.getOutputStream());
            session.setAttribute(BaseContext.session_yzm, verifyCode);
        } catch (IOException e) {
            logWrite.initLog();
            logWrite.writeLog(e.getMessage());
            logWrite.closeLog(LogPath.debugLog);
        }
    }

Util

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.Random;

import javax.imageio.ImageIO;
public class VerifyCode {
    
    /**
     * 生成验证码
     * @param width
     * @param height
     * @param imgType
     * @param output
     * @return
     */
    public static String createVerifyCode(OutputStream output){
        String imgType = "jpg";
        int width = 100;
        int height = 30;
        int times = 4;
        width = width*times;
        height = height*times;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphic = (Graphics2D)image.getGraphics();
        graphic.setColor(getColor(180, 240));
        graphic.fillRect(0, 0, width, height);   
        // 在 "画板"上生成干扰线条 ( 50 是线条个数)
        /*for (int i = 0; i < 50; i++) {
            graphic.setColor(colors[random.nextInt(colors.length)]);
            final int x = random.nextInt(width);
            final int y = random.nextInt(height);
            final int w = random.nextInt(20);
            final int h = random.nextInt(20);
            final int signA = random.nextBoolean() ? 1 : -1;
            final int signB = random.nextBoolean() ? 1 : -1;
            graphic.drawLine(x, y, x + w * signA, y + h * signB);
        }*/
        // 在 "画板"上绘制字母
        //graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
        graphic.setFont(new Font("SimHei", Font.BOLD, 25*times));
        int length = 4;
        String verifyCode = getVerifyCode(length);//获取验证码
        for (int i = 1; i <= length; i++) {
            graphic.setColor(getColor(50, 160));//字体颜色
            int x = width / (length + 1) * i;
            int y = height / 2 + 25;
            //设置旋转角度和坐标原点
            graphic.translate(x, y);
            graphic.drawString(String.valueOf(verifyCode.charAt(i-1)), 0, 0);
            //恢复旋转角度和坐标原点
            graphic.translate(-x, -y);
        }
        graphic.dispose();
        try {
            ImageIO.write(image, imgType, output);//输出到前端字符
        } catch (IOException e) {
            e.printStackTrace();
        }
        return verifyCode;
    }
    
    /**
     * 获取验证码
     * @return
     */
    public static String getVerifyCode(int length){
        StringBuffer verifyCode = new StringBuffer();
        String container = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        for(int i=0; i<length; i++){
            verifyCode.append(container.charAt(random.nextInt(container.length())));
        }
        return verifyCode.toString();
    }
    
    /**
     * 获取颜色
     * @param min
     * @param max
     * @return
     */
    public static Color getColor(int min, int max){
        return new Color(getRandom(min, max), getRandom(min, max), getRandom(min, max));
    }
    
    /**
     * 获取随机数
     * @param min
     * @param max
     * @return
     */
    public static int getRandom(int min, int max){
        return (int)Math.floor(Math.random()*(max-min)+min);
    }
    public static void main(String[] args) {
        try {
            createVerifyCode(new FileOutputStream("D:\\vertifyCode.jpg"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    
}

 

效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值