Java生成一个随机验证码

生成一个随机的验证码,判断输入是否正确。

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

/**
 * @author: 13667
 * @date: 2020/10/19 10:28
 * @description: 生成一个随机的验证码
 */
public class RandomCode {
    
    

    public static void main(String[] args) throws IOException {
    
    
        RandomCode rc = new RandomCode();
        boolean isFlag = true;
        while (isFlag){
    
    
            try {
    
    
                isFlag = rc.randomImgs();
            }catch (RuntimeException e){
    
    
                System.out.println(e.getMessage());
            }
        }
    }
    /**
     * description 获取一个随机数
     * author chen
     * updateTime 2020/10/19 10:30
     *
     * return: 返回一个随机数0-val
     */
    public int randomNum(int val){
    
    
        Random random = new Random();
        int num = random.nextInt(val);
        return num;
    }
    /**
     * description 生成一个随机的图片验证码
     * author chen
     * updateTime 2020/10/19 11:20
     *
     * return: false输入正确 错误时抛出一个异常
     */
    //A-z  65 - 90
    public boolean randomImgs() throws IOException {
    
    
        //生成3组随机字母 + 随机数
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 3; i++) {
    
    
            sb.append((char)( this.randomNum(25) + 60));
            sb.append(this.randomNum(9));
        }
        //保存生成的随机字母和随机数
        String random = sb.toString();
        File file = new File(System.currentTimeMillis() + ".jpg");
        BufferedImage bi = new BufferedImage(200,80,BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bi.getGraphics();

        //增加干扰线条
        for (int i = 0; i < 10; i++) {
    
    
            graphics.setColor(new Color(this.randomNum(255),this.randomNum(255),this.randomNum(255)));
            graphics.drawLine(this.randomNum(10),this.randomNum(80),(this.randomNum(10) + 190),this.randomNum(80));
        }
        //写入文本
        Font f = new Font("宋体",Font.BOLD,40);
        graphics.setFont(f);
        graphics.drawString(random,50,50);
        //增加干扰线条
        for (int i = 0; i < 10; i++) {
    
    
            graphics.setColor(new Color(this.randomNum(255),this.randomNum(255),this.randomNum(255)));
            graphics.drawLine(this.randomNum(10),this.randomNum(80),(this.randomNum(10) + 190),this.randomNum(80));
        }

        ImageIO.write(bi,"jpg",file);

        ImageIcon ii = new ImageIcon(file.getAbsolutePath());

        //从弹窗获取到输入验证码的值
        Object result = JOptionPane.showInputDialog(null,"请输入左边图片验证码:","验证",0,ii,null,"");
        //将获取到的值转换成String类型,使得输入不区分大小写
        String str = String.valueOf(result);
        if(!str.toUpperCase().equals(random)){
    
    
            file.delete();
            throw new RuntimeException("验证码输入错误!请重新输入!");
        }
        file.delete();
        return false;
    }

}

猜你喜欢

转载自blog.csdn.net/OrangeNotFat/article/details/109177521