JAVA生成图片验证码

时间:2022-11-04 08:19:38

原文地址:http://blog.csdn.net/ruixue0117/article/details/22829557 本屌稍作修改!再加一个基础功能的方法

一:基础功能方法

Img.java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;

public class Img {
public static void outputCodeImg (int width,int height,String code,OutputStream os ) throws FileNotFoundException, IOException {
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE);
//获取画笔
Graphics gra = bi.getGraphics();
//设置背景颜色
gra.setColor(Color.BLACK);
//填充背景颜色
gra.fillRect(0, 0, width, height);
//第二次 设置字体颜色
gra.setColor(Color.red);
//设置字体样式和大小
gra.setFont(new Font(null, Font.HANGING_BASELINE,20));
//描绘
gra.drawString(code.toString(), 5, height-5);
ImageIO.write(bi, "png", os);
}
}<strong>
</strong>
效果
JAVA生成图片验证码


二:升级版(转帖)
ImgCode.java

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;

public class ImgCode {

/**
* 生成验证码图片
* @param width
* @param height
* @param codeLength
* @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);
}
/**
* 返回验证码
* @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);
}

}

}
}<strong>
</strong>

效果
JAVA生成图片验证码

测试类Test.java

public class Test{  
public static void main(String[] args) {
try {
String code = ImgCode.getCode(5);
ImgCode.outputCodeImg(120, 30, code, new FileOutputStream(new File("c:/"+code+".jpg")));
Img.outputCodeImg(80, 25, code, new FileOutputStream(new File("c:/"+code+".png")));
System.out.println(code);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}