生成 java valid_Java生成校验码

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import java.util.Random;

import javax.imageio.ImageIO;

public class SimpleCaptcha {

private static final String VALID_CHARS = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789"; // remove o,i,l

private int width;// 验证码图片的宽度。

private int height;// 验证码图片的高度。

private int fontSize; // 字体大小

private int chars; // 字符个数

private BufferedImage image;

private Random random = new Random();

public SimpleCaptcha() {

this(100, 30, 26, 4);

}

public SimpleCaptcha(int width, int height, int fontSize, int chars) {

this.width = width;

this.height = height;

this.fontSize = fontSize;

this.chars = chars;

this.image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

}

public void createCode(File file) {

try {

FileOutputStream fos = new FileOutputStream(file);

createCode(fos);

fos.close();

} catch (IOException e) {

throw new RuntimeException(e);

}

}

public void createCode(OutputStream os) {

Graphics2D g = image.createGraphics();

drawBackground(g);

drawRandomLines(g, 100);

drawFrame(g);

drawString();

try {

ImageIO.write(image, "JPEG", os);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

private void drawBackground(Graphics2D g) {

// 设定图像背景色(因为是做背景,所以偏淡)

g.setColor(getRandomColor(180, 250));

g.fillRect(0, 0, width, height);

}

/**

* 绘制干扰线

* @param gGraphics2D对象,用来绘制图像

* @param nums干扰线的条数

*/

private void drawRandomLines(Graphics2D g, int nums) {

g.setColor(getRandomColor(100, 200));

int maxlength = 12;

for (int i = 0; i < nums; i++) {

int x = random.nextInt(width);

int y = random.nextInt(height);

int xl = random.nextInt(maxlength);

int yl = random.nextInt(maxlength);

g.drawLine(x, y, x + xl, y + yl);

}

}

private void drawFrame(Graphics2D g) {

//画边框。

g.setColor(Color.BLACK);

g.drawRect(0, 0, width - 1, height - 1);

}

/**

* 生成随机颜色

* @param fc前景色

* @param bc背景色

* @returnColor对象,此Color对象是RGB形式的。

*/

private Color getRandomColor(int fc, int bc) {//给定范围获得随机颜色

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);

}

private String getRandomString(int length) {

char[] codes = new char[length];

int size = VALID_CHARS.length();

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

codes[i] = VALID_CHARS.charAt(random.nextInt(size));

}

return new String(codes);

}

private void drawString() {

final String randomCode = getRandomString(chars);

for (int i = 0; i < chars; i++) {

String ch = randomCode.substring(i, i + 1);

//缩放文字

double scale = (random.nextInt(60) + 70D) / 100D;

Font font = new Font("Consola", Font.BOLD, (int) (fontSize * scale));

Graphics2D g = image.createGraphics();

g.setFont(font);

//旋转文字

int w = g.getFontMetrics().charWidth(ch.charAt(0));

int h = g.getFontMetrics().getAscent() - g.getFontMetrics().getDescent();

int x1 = (width / chars) * i + (width / chars - w) / 2;

int y1 = height - (height - h) / 2;

int x2 = x1 + w / 2;

int y2 = y1 - h / 2;

g.rotate((random.nextInt(90) - 45) * Math.PI / 180, x2, y2);

// 输出字符

g.setColor(getRandomColor(1, 120));

g.drawString(ch, x1, y1);

}

}

public static void main(String[] args) {

new SimpleCaptcha().createCode(new File("c:\\1.jpg"));

}

}

Java身份证校验是指在Java编程语言中对身份证号码进行合法性验证的过程。身份证号码是由公民身份证号码规则生成的,校验身份证号码的合法性可以通过以下步骤进行: 1. 长度验证:身份证号码一般为18位,如果不是18位则认为是非法的。 2. 前17位数字验证:前17位是身份证号码的主体部分,需要验证是否都是数字。 3. 最后一位校验码验证:身份证号码的最后一位是校验码,用于验证前面17位的正确性。校验码的计算方法如下: - 将前17位数字分别乘以对应的权重因子,权重因子从左到右依次为:7、9、10、5、8、4、2、1、6、3、7、9、10、5、8、4、2。 - 将乘积相加得到总和。 - 将总和除以11取余数,得到余数对应的校验码。余数为0时,校验码为1;余数为1时,校验码为0;余数为2时,校验码为X;余数为其他数字时,校验码为11减去余数。 - 将计算得到的校验码与身份证号码的最后一位进行比较,如果相等则认为是合法的。 以下是Java代码示例,用于验证身份证号码的合法性: ```java public class IDCardValidator { public static boolean isValid(String idCard) { // 长度验证 if (idCard.length() != 18) { return false; } // 前17位数字验证 String idCard17 = idCard.substring(0, 17); if (!isNumeric(idCard17)) { return false; } // 最后一位校验码验证 char checkCode = calculateCheckCode(idCard17); return checkCode == idCard.charAt(17); } private static boolean isNumeric(String str) { for (int i = 0; i < str.length(); i++) { if (!Character.isDigit(str.charAt(i))) { return false; } } return true; } private static char calculateCheckCode(String idCard17) { int sum = 0; for (int i = 0; i < idCard17.length(); i++) { int num = Character.getNumericValue(idCard17.charAt(i)); int weight = (int) Math.pow(2, 17 - i) % 11; sum += num * weight; } int remainder = sum % 11; if (remainder == 0) { return '1'; } else if (remainder == 1) { return '0'; } else if (remainder == 2) { return 'X'; } else { return (char) (11 - remainder + '0'); } } } ``` 使用示例: ```java String idCard = "身份证号码"; boolean isValid = IDCardValidator.isValid(idCard); System.out.println("身份证号码是否合法:" + isValid); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值