java之生成图片验证码

阅读更多
由于自己的项目登录需要验证码,于是便谢了一个图片验证码生成类。

图片验证实体类 ImageVerification.java
package com.jynine.model;

import java.awt.image.BufferedImage;
import java.io.Serializable;

/**
 * 图片验证类
 * @ClassName: ImageVerification
 * @Description: 图片验证实体类
 * @author caijy
 * @date 2013-12-9 下午9:32:32
 *
 */
public class ImageVerification implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4881192896106347995L;
	//图片
	private BufferedImage image;
	//验证码
	private String verifyCode;
	public BufferedImage getImage() {
		return image;
	}
	public void setImage(BufferedImage image) {
		this.image = image;
	}
	public String getVerifyCode() {
		return verifyCode;
	}
	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}
	
}

图片生成类 ImageBuilder.java
package com.jynine.utils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;

import com.jynine.model.ImageVerification;


/**
 * 图片生成类
 * @ClassName: ImageBuilder
 * @Description: 图片生成类
 * @author caijy [email protected]
 * @date 2013-12-9 下午9:28:13
 *
 */
public class ImageBuilder {
	//图片验证码长度为4
	private int length = 4; 
	//图片宽度
	private int width = 80;
	//图片高度
	private int height = 24;
	//字体高度
	private int fontHeight = height- 4;
	//代码字符
	private char[] codeChar = { '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','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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	
	//字符之间的宽度
	private int x_space = 3;
	//字符与图片左右边框的间隔   
	private int lr_space = 4;
	//一个字符的宽度
	private int x = (width - 2*lr_space - x_space*(length-1))/length;
	//画直线数量
	private int lineSize = height/4;
	//字符高度
	private int codeY = height - 4;  
	//圆的半径
	private int r = 3;
	//噪点的个数
	private int np_num = 25;
	//验证码对象
	private ImageVerification iv;
	public ImageVerification builder(){
		iv = new ImageVerification();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = image.createGraphics();
		//背景颜色
		graphics.setColor(Color.white);
		graphics.fillRect(0, 0, width, height);  
		Font font = new Font(null, Font.BOLD|Font.ITALIC, fontHeight);
		graphics.setFont(font);
		//边框
		graphics.setColor(Color.black);
		graphics.drawRect(0, 0, width - 1, height - 1);  
		//画线
		for (int i = 0; i < lineSize; i++) {
			int ly = lineSize*(i+1);
			graphics.setColor(randomColor());
			graphics.drawLine(1, ly, width-2, ly);
		}
		Random random = new Random();
		//生成噪点
		for (int i = 0; i < np_num; i++) {
			graphics.setColor(randomColor());
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			graphics.fillOval(x, y, r,r);
		}
		StringBuffer sb = new StringBuffer();
		//获取字符
		for (int i = 0; i < length; i++) {
			graphics.setColor(randomColor());
			String codeStr = String.valueOf(codeChar[new Random().nextInt(codeChar.length)]);
			sb.append(codeStr);
			graphics.drawString(codeStr, lr_space+i*x_space+i*x, codeY);
		}
		iv.setVerifyCode(sb.toString());
		iv.setImage(image);
		//清空缓存  
		graphics.dispose();
		return iv;
	}
	/**
	 * 随机生成一个颜色
	 * @return
	 */
	private Color randomColor(){
		 // 创建一个随机数生成器类  
        Random random = new Random();  
        int red = random.nextInt(255);  
        int green = random.nextInt(255);  
        int blue = random.nextInt(255);  
        return new Color(red, green, blue);
	}
	public static void main(String[] args) throws IOException {
		ImageBuilder iBuilder = new ImageBuilder();
		ImageVerification imageVer = iBuilder.builder();
		 File file = new File("E:/img.png");  
		ImageIO.write(imageVer.getImage(), "png", file);
	}
}



生成的验证码样列:

java之生成图片验证码_第1张图片

  • java之生成图片验证码_第2张图片
  • 大小: 8 KB
  • 查看图片附件

你可能感兴趣的:(java,图片验证码)