您的位置:首页 > 编程语言 > Java开发

java生成验证码

2015-01-03 22:00 225 查看
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Random;
import javax.imageio.ImageIO;

/**
* @author 王大锤
*
*         E-mail: 1028625100@qq.com
*
* @version v1.1
*
*          创建时间:2014-11-12 下午2:15:12
*/
public class Captcha {

/**
* 生成验证码图片
*
* @param width
* 				图片宽度
* @param height
* 				图片高度
* @param code
* 				要在图片上显示的随机数
* @param os
* 				要输出的流
*
* @throws IOException
*/
public static void outputCodeImg(int width, int height, String code, OutputStream os) throws IOException {

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

// 获取画笔
Graphics2D g2 = image.createGraphics();

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

Color[] colors = new Color[5];

Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,Color.PINK, Color.YELLOW };

float[] fractions = new float[colors.length];

Random rand = new Random();

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

colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];

fractions[i] = rand.nextFloat();

}

Arrays.sort(fractions);

// 设置边框色
g2.setColor(Color.GRAY);

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

Color c = getRandColor(200, 250);

// 设置背景色
g2.setColor(c);

g2.fillRect(0, 2, width, height - 4);

// 绘制干扰线
Random random = new Random();

// 设置线条的颜色
g2.setColor(getRandColor(160, 200));

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

int x = random.nextInt(width - 1);

int y = random.nextInt(height - 1);

int xl = random.nextInt(6) + 1;

int yl = random.nextInt(12) + 1;

g2.drawLine(x, y, x + xl + 40, y + yl + 20);

}

// 添加噪点
float yawpRate = 0.05f;// 噪声率

int area = (int) (yawpRate * width * height);

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

int x = random.nextInt(width);

int y = random.nextInt(height);

int rgb = getRandomIntColor();

image.setRGB(x, y, rgb);

}

// 使图片扭曲
shear(g2, width, height, c);

g2.setColor(getRandColor(100, 160));

int fontSize = height - 4;

Font font = new Font("Algerian", Font.ITALIC, fontSize);

g2.setFont(font);

char[] chars = code.toCharArray();

int verifySize = code.length();

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

AffineTransform affine = new AffineTransform();

affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1),(width / verifySize) * i + fontSize / 2, height / 2);

g2.setTransform(affine);

// 如果上下 左右两边的验证码出现的位置不对,条件4 , 5参数
g2.drawChars(chars, i, 1, ((width - 10) / verifySize) * i + 5,height / 2 + fontSize / 2 - 2);

}

g2.dispose();

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

}

/**
* 获取生产的验证码
*
* @param length
* 				输入验证码长度
*
* @return
* 				返回生成的验证码
*/
public static String getCode(int length) {

Random random = new Random();

StringBuffer code = new StringBuffer();

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

// 数字
int num = random.nextInt(10);

// 大写字母
char upperCase = (char) (random.nextInt(26) + 65);

String[] s = new String[2];

s[0] = num + "";

s[1] = upperCase + "";

code.append(s[random.nextInt(2)]);

}

return code.toString();

}

private static Color getRandColor(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);

}

private static int getRandomIntColor() {

Random random = new Random();

int[] rgb = new int[3];

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

rgb[i] = random.nextInt(255);

}

int color = 0;

for (int c : rgb) {

color = color << 8;

color = color | c;

}

return color;

}

private static void shear(Graphics g, int w1, int h1, Color color) {

shearX(g, w1, h1, color);

shearY(g, w1, h1, color);

}

private static void shearX(Graphics g, int w1, int h1, Color color) {

Random random = new Random();

int period = random.nextInt(2);

boolean borderGap = true;

int frames = 1;

int phase = random.nextInt(2);

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

double d = (double) (period >> 1) * Math.sin((double) i / (double) period+ (6.2831853071795862D * (double) phase) / (double) frames);

g.copyArea(0, i, w1, 1, (int) d, 0);

if (borderGap) {

g.setColor(color);

g.drawLine((int) d, i, 0, i);

g.drawLine((int) d + w1, i, w1, i);

}

}

}

private static void shearY(Graphics g, int w1, int h1, Color color) {

Random random = new Random();

int period = random.nextInt(40) + 10; // 50;

boolean borderGap = true;

int frames = 20;

int phase = 7;

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

double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);

g.copyArea(i, 0, 1, h1, 0, (int) d);

if (borderGap) {

g.setColor(color);

g.drawLine(i, (int) d, i, 0);

g.drawLine(i, (int) d + h1, i, h1);

}

}

}

}


测试方法:

@Test
public void random() {
try {
String code = Captcha.getCode(5);
Captcha.outputCodeImg(120, 30, code, new FileOutputStream(new File("D:/" + code + ".png")));
System.out.println(code);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


效果如下图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: