[RandomUtil]随机编码生成通用类

package com.common;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Random;


/**
 * @类描述 [RandomUtil]随机编码生成通用类
 * @创建人 winy
 * @创建时间 2014-3-18 上午11:16:08
 * @修改人 
 * @修改记录 
 * @版本
 */
public class RandomUtil {
private static final char[] codecParams = { 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };


/**
* 根据输入长度随即生成英文字母组合的字符串
* @param length 长度
* @return
*/
public static String getMathRandomChar(int length) {
String random = "";
int[] key = new int[length];
for (int i = 0, j = 0; i < 100; i++) {
int code = (int) (Math.random() * 100);
if (code >= 0 && code <= 25) {
if (j == length) {
break;
}
key[j] = code;
j++;
}
}
for (int f = 0; f < key.length; f++) {
random += codecParams[key[f]];
}
return random;
}


/**
* 生成length长度的随即数字
* @param length 长度
* @return
*/
public static String getMathRandomDigit(int length) {
String random = "";
for (int i = 0, j = 0; i < 100; i++) {
int code = (int) (Math.random() * 10);
if (code >= 0 && code <= 9) {
if (j == length) {
break;
}
random += code;
j++;
}
}
return random;
}

/**
* 获取指定数值内(0~该数值)中任意值
* @param num 指定数值
* @param includeZero 是否包含0
* @return
*/
public static int getMathRandomNumWithin(int num, boolean includeZero) {
if (num == 0) return 0;
return includeZero ? (int) (Math.random() * (num + (num > 0 ? 1 : -1)))
: (int) (Math.random() * num + (num > 0 ? 1 : -1));
}


/**
* 随机生成验证码,目前固定4位
* @return
*/
public static String getCheckImageNumber() {
String random = "";
// 2个数字加2个字母
String baseCode = getMathRandomDigit(2) + getMathRandomChar(2);
int digit = 4;// 验证码位数
Character[] charArr = new Character[digit];
// 把字符串转换成数组在转换成List
for (int i = 0; i < digit; i++) {
charArr[i] = baseCode.charAt(i);
}
List<Character> charList = Arrays.asList(charArr);
// 将顺序打乱
Collections.shuffle(charList);
// 重新拼接成字符串
for (Character ch : charList) {
random += ch.charValue();
}
return random;
}

/**
* 生成验证码图片
* @param code 验证码,至少4位
* @return
*/
public static BufferedImage getCheckImage(String code) {
if (code != null && code.length() >= 4) {
// 在内存中创建图象


int width = 60, height = 20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);


// 获取图形上下文
Graphics g = image.getGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandomColor(200, 250));
g.fillRect(0, 0, width, height);
// 设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));


// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandomColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}


// 取随机产生的认证码
int digit = code.length();
for (int i = 0; i < digit; i++) {
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 
20 + random.nextInt(110), 
20 + random.nextInt(110)));// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(code.substring(i, i + 1), 13 * i + 6, 16);
}
g.dispose();
return image;
}
return null;
}
private static Color getRandomColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
* 随机生成日期(日期起始范围1900~2100)
* @return
*/
public static Date getRandomDate() {
return getRandomDate(1900, 2100);
}
/**
* 随机生成指定范围内的日期
* @param startYear 开始年份(默认1900)
* @param endYear 终止年份
* @return
*/
@SuppressWarnings("deprecation")
public static Date getRandomDate(int startYear, int endYear) {
return new Date((startYear - 1900) + 
getMathRandomNumWithin((endYear - startYear), true),//-1900后的值
getMathRandomNumWithin(11, true), // 0~11
getMathRandomNumWithin(31, false) // 1~31
);
}

/**
* 随机生成18位有效的身份证号码:
* 身份证18位编码规则:dddddd yyyymmdd xxx y   
* dddddd:地区码   
* yyyymmdd: 出生年月日   
* xxx:顺序类编码,无法确定,奇数为男,偶数为女   
* y: 校验码,该位数值可通过前17位计算获得
* 18位号码加权因子为(从右到左) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]  
* 验证位 Y = [ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 ]   
* 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 )   
* i为身份证号码从右往左数的 2...18 位; Y_P为脚丫校验码所在校验码数组位置 
     * @return
     */
public static String getRandomIdCardNo() {
String areaCode = getMathRandomDigit(6);//地区码6位
String seqCode = getMathRandomDigit(3);//随机码3位
String dateCode = String.format("%1$tY%1$tm%1$td", getRandomDate(1900, 2010));//日期码8位

int[] Wi = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
char[] Y = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };


int sum = 0;
String idCardNo = areaCode + dateCode + seqCode;
for (int i = 0; i < 17; i++) {
sum += Wi[i] * Character.digit(idCardNo.charAt(i), 10);
}
idCardNo += Y[sum % 11];
return idCardNo;
}
public static void main(String[] args) {
int i = 1;
int a = ++i;
System.out.println(getRandomIdCardNo());
}



/* 需要有效格式的18位身份证号的时候,可以复制下面20中任意1个即可:*/
/* 
88243619520509372X
853824194512094164
607092200702137282
644762191507081394
282734199405100269
412396192510085056
168378191907197212
370702200612256738
135485199711059050
356698198707299553
065115198912078313
480785195201287743
225018198508076096
103748195610115729
792156195405100485
577302190903127982
719863200709154383
728011199111241947
050218199912297068
116233199807201330
*/

}


// 判断字符串长度(比如文件名),汉字为2,字符为1
    protected static int count(String str) {
        if (str == null || str.length() == 0) {
            return 0;
        }
        int count = 0;
        char[] chs = str.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            count += (chs[i] > 0xff) ? 2 : 1;
        }
        return count;
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值