0
点赞
收藏
分享

微信扫一扫

生成验证码功能的实现过程总结

生成验证码的步骤

  1. 创建一个Image
  2. 创建画笔
  3. 获取随机验证码
  4. 获取验证码图片大小和干扰点
  5. 获取绘制好的验证码

创建Image

 VerifyCodeImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);

创建画笔

 Graphics graphics = VerifyCodeImage.getGraphics();

获取随机验证码

public static char[] getVerifyCode() {
        return verifyCode;
    }
    private static char[] generateCheckCode() {
        String chars = "0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] rands = new char[4];
        for (int i = 0; i < 4; i++) {
            int rand = (int) (Math.random() * (10 + 26 * 2));
            rands[i] = chars.charAt(rand);
        }
        return rands;
    }

获取验证码图片大小和干扰点

 private static void drawBackground(Graphics g) {

        //设置背景颜色
        g.setColor(Color.white);
        //设置验证码图片的大小 颜色为上面设置的
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 绘制验证码干扰点
        for (int i = 0; i < 200; i++) {
            int x = (int) (Math.random() * WIDTH);
            int y = (int) (Math.random() * HEIGHT);
            g.setColor(getRandomColor());
            g.drawOval(x, y, 1, 1);

        }
    }

获取绘制好的验证码

private static void drawRands(Graphics g, char[] rands) {
        g.setFont(new Font("Console", Font.BOLD, FONT_SIZE));

        for (int i = 0; i < rands.length; i++) {

            g.setColor(getRandomColor());
            g.drawString("" + rands[i], i * FONT_SIZE + 10, 25);
        }
    }

下面是完整代码

package com.zeng.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @project: ssm_sms
 * @description: 绘制验证码图片
 * @version: 1.0
 * @website: https://yubuntu0109.github.io/
 */
public class CreateVerifyCodeImage {

    private static int WIDTH = 90;
    private static int HEIGHT = 35;
    private static int FONT_SIZE = 20; //字符大小
    private static char[] verifyCode; //验证码
    private static BufferedImage VerifyCodeImage; //验证码图片

    /**
     * @description: 获取验证码图片
     * @param: no
     * @return: java.awt.image.BufferedImage
     */
    public static BufferedImage getVerifyCodeImage() {
        VerifyCodeImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);// create a image
        //画笔对象
        Graphics graphics = VerifyCodeImage.getGraphics();

        //获取验证码的四个字母
        verifyCode = generateCheckCode();
        //获取绘制图片背景
        drawBackground(graphics);
        //获取绘制验证码
        drawRands(graphics, verifyCode);

        graphics.dispose();

        return VerifyCodeImage;
    }

    /**
     * @description: 获取验证码
     * @param: no
     * @date: 2019-06-09 5:47 PM
     * @return: char[]
     */
    public static char[] getVerifyCode() {
        return verifyCode;
    }

    /**
     * @description: 随机生成验证码
     * @param: no
     * @date: 2019-06-09 5:47 PM
     * @return: char[]
     */
    private static char[] generateCheckCode() {
        String chars = "0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] rands = new char[4];
        for (int i = 0; i < 4; i++) {
            int rand = (int) (Math.random() * (10 + 26 * 2));
            rands[i] = chars.charAt(rand);
        }
        return rands;
    }

    /**
     * @description: 绘制验证码
     * @param: g
     * @param: rands
     * @date: 2019-06-09 5:47 PM
     * @return: void
     */
    private static void drawRands(Graphics g, char[] rands) {
        g.setFont(new Font("Console", Font.BOLD, FONT_SIZE));

        for (int i = 0; i < rands.length; i++) {

            g.setColor(getRandomColor());
            g.drawString("" + rands[i], i * FONT_SIZE + 10, 25);
        }
    }

    /**
     * @description: 绘制验证码图片背景
     * @param: g
     * @return: void
     */
    private static void drawBackground(Graphics g) {

        //设置背景颜色
        g.setColor(Color.white);
        //设置验证码图片的大小 颜色为上面设置的
        g.fillRect(0, 0, WIDTH, HEIGHT);

        // 绘制验证码干扰点
        for (int i = 0; i < 200; i++) {
            int x = (int) (Math.random() * WIDTH);
            int y = (int) (Math.random() * HEIGHT);
            g.setColor(getRandomColor());
            g.drawOval(x, y, 1, 1);

        }
    }


    /**
     * @description: 获取随机颜色
     * @param: no
     * @return: java.awt.Color
     */
    private static Color getRandomColor() {
        Random ran = new Random();
        return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));
    }
}

总结

  • 这里的验证码用到了BufferedImage类,然后通过Graphics graphics = VerifyCodeImage.getGraphics();获取画笔
  • 验证码位大小写和数字 颜色随机生成
  • 验证码的干扰使用宽高为1的圆点组成的 位置随机
  • 也可以实现划线作为干扰
举报

相关推荐

0 条评论