All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luues.util.random.CodeUtil Maven / Gradle / Ivy

There is a newer version: 1.3.0.5.RELEASE
Show newest version
package com.luues.util.random;

import com.luues.util.logs.LogUtil;

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

/**
 * 验证码工具类
 */
public class CodeUtil {

    private static Random random = new Random();
    private static int width = 74;// 图片
    private static int height = 34;// 图片
    private static int lineSize = 20;// 干扰线数


    /**
     * 根据不同类型获取随机数
     *
     * @param paramstype
     * @param length
     * @return
     */
    public static String getRandomString(ParamsType paramstype, int length) {
        LogUtil.debug("---->获取验证码参数类型:{}", paramstype.type);
        String str = "";
        switch (paramstype.type) {
            case ParamsType.randType_int:
                str = ParamsType.randInt.type.toString();
                break;
            case ParamsType.randType_string:
                str = ParamsType.randString.type.toString();
                break;
            case ParamsType.randType_intString:
                str = ParamsType.randIntString.type.toString();
                break;
            default:
                str = ParamsType.randInt.type.toString();
                break;
        }
        String result = "";
        for (int i = 0; i < length; i++) {
            result += String.valueOf(str.charAt(random.nextInt(str.length())));
        }
        LogUtil.debug("---->随机产生验证码为:{}", result);
        return result;
    }

    /**
     * 生成图片验证码
     */
    public static RandImage getRandcode(ParamsType paramsType, int length) {
        // BufferedImage类是具有缓冲区的Image,Image类是用于描述图像信息的类
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        Graphics g = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操
        g.fillRect(0, 0, width, height);
        g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18));
        g.setColor(getRandColor(110, 133));
        // 绘制干扰线
        for (int i = 0; i <= lineSize; i++) {
            drowLine(g);
        }
        // 绘制随机字符
        String randomString = "";
        for (int i = 0; i < length; i++) {
            randomString = drowString(g, randomString, i, paramsType, 1);
        }
        RandImage randImage = new RandImage();
        randImage.setCode(randomString);
        randImage.setBufferedImage(image);
        g.dispose();
        return randImage;
    }


    /*
     * 获得字体
     */
    private static Font getFont() {
        return new Font("Fixedsys", Font.CENTER_BASELINE, 25);
    }

    /*
     * 获得颜色
     */
    private static Color getRandColor(int fc, int bc) {
        if (fc > 255)
            fc = 255;
        if (bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc - fc - 16);
        int g = fc + random.nextInt(bc - fc - 14);
        int b = fc + random.nextInt(bc - fc - 18);
        return new Color(r, g, b);
    }

    /*
     * 绘制字符
     */
    private static String drowString(Graphics g, String randomString, int i, ParamsType paramsType, int length) {
        g.setFont(getFont());
        g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121)));
        String rand = String.valueOf(getRandomString(paramsType, length));
        randomString += rand;
        g.translate(random.nextInt(3), random.nextInt(3));
        g.drawString(rand, 17 * i + 2, 23);
        return randomString;
    }

    /*
     * 绘制干扰线
     */
    private static void drowLine(Graphics g) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(13);
        int yl = random.nextInt(15);
        g.drawLine(x, y, x + xl, y + yl);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy