【零碎JAVA】Java生成验证码的类实现

运用JAVA的java.awt包实现验证码生成,如图




下面贴出实现代码:

package com.test.vcode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Window.Type;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;
/**
 * 这个类用于生成验证码图片
 * 使用方法:
 * 		1.创建VerifyCode对象。
 * 		2.设置验证码长度,干扰线条数。(此步操作可以在创建对象的时候完成)
 * 		3.调用getVerifyCodeImg方法,生成这个验证码,并获取BufferedImage对象
 * 		4.调用getVerifyCode方法获取验证码的值
 * 		5.可通过output方法将这个验证码图片通过输出流传出
 * 示例:
 * 		VerifyCode vc = new VerifyCode();
		vc.setCodeCount(5);
		vc.setLineCount(20);
		BufferedImage image = vc.getVerifyCodeImg();
		sop(vc.getVerifyCode());
		//将验证码保存到F://t.jpg
		VerifyCode.output(image, new FileOutputStream(new File("F://t.jpg")));
 * @author hyz
 *
 */
public class VerifyCode {
	//验证码图片宽度
	private int width = 100;
	//验证码图片高度
	private int height = 40;
	//干扰线条数
	private int lineCount = 10;
	//验证码位数
	private int codeCount = 4;
	//验证码
	private String verifyCode = null;
	//验证码所能包含的字符,将一些容易混淆的字符去除了
	private String codes = "23456789ABCDEFGHJKMNPQRSTUWXYZabcdefghjkmnpqrstuwxyz";
	//字体格式
	private String[] fontNames  = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};
	//验证码图片的背景色
	private Color bgColor = new Color(255, 255, 255);
	//用于生成随机数
	private Random random = new Random();
	
	/**
	 * 无参的构造函数,使用默认的高度,宽度,干扰线条数
	 */
	public VerifyCode(){};
	
	/**
	 * 构造函数
	 * @param width		指定验证码的宽度
	 * @param height	指定验证码的高度
	 */
	public VerifyCode(int width, int height){
		this.width = width;
		this.height = height;
	}
	
	/**
	 * 构造函数
	 * @param width			指定验证码的宽度
	 * @param height		指定验证码的高度
	 * @param lineCode		指定验证码的干扰线条数
	 */
	public VerifyCode(int width, int height, int lineCount){
		this.width = width;
		this.height = height;
		this.lineCount = lineCount;
	}
	
	/**
	 * 构造函数
	 * @param width			指定验证码宽度
	 * @param height		指定验证码高度
	 * @param lineCount		指定验证码干扰线条数
	 * @param codeCount		指定验证码长度
	 */
	public VerifyCode(int width, int height, int lineCount, int codeCount){
		this.width = width;
		this.height = height;
		this.lineCount = lineCount;
		this.codeCount = codeCount;
	}
	
	/**
	 * 设置验证码中干扰线条数
	 * @param lineCount		干扰线条数
	 */
	public void setLineCount(int lineCount){
		this.lineCount = lineCount;
	}
	
	/**
	 * 设置验证码中验证码长度
	 * @param codeCount		验证码长度
	 */
	public void setCodeCount(int codeCount){
		this.codeCount = codeCount;
	}
	
	/**
	 * 创建验证码画布,背景设置为白色,并绘制边框
	 * @return	验证码图片对象
	 */
	private BufferedImage createImage(){
		//创建一块画布
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		//获取该画布的画笔
		Graphics2D g2 = (Graphics2D)image.getGraphics();
		g2.setColor(this.bgColor);
		g2.fillRect(0,  0, width, height);
		//设置图片边框的颜色
		Color c = randomColor();
		g2.setColor(c);
		g2.drawRect(0, 0, width-1, height-1);
		
		return image;
		
	}
	
	/**
	 * 从codes中随机获取字符
	 * @return	随机获取的字符
	 */
	private char randomChar(){
		int index = random.nextInt(codes.length());
		return codes.charAt(index);
	}
	
	/**
	 * 随机生成一个Color对象
	 * @return Color对象
	 */
	private Color randomColor(){
		int red = random.nextInt(255);
		int green = random.nextInt(255);
		int blue = random.nextInt(255);
		
		return new Color(red, green, blue);
	}
	
	private Font randomFont(){
		int index = random.nextInt(fontNames.length);
		String fontName = fontNames[index];
		
		return new Font(fontName, random.nextInt(4), random.nextInt(5) + 24);
	}
	
	/**
	 * 获取验证码图片对象
	 * @return	验证码图片对象
	 */
	public BufferedImage getVerifyCodeImg(){
		BufferedImage image = createImage(); 
		Graphics2D g2 = (Graphics2D)image.getGraphics();
		String str = "";
		
		//生成验证码字符串
		for(int i = 0; i < this.codeCount; ++i){
			String s = randomChar() + "";
			str += s;
			float x = i * 1.0F * width / codeCount;
			g2.setColor(randomColor());
			g2.setFont(randomFont());
			g2.drawString(s, x, height-5);
		}
		this.verifyCode = str;
		drawLine(image);
		return image;
		
	}
	
	private void drawLine(BufferedImage image){
		Graphics2D g2 = (Graphics2D)image.getGraphics();
		for(int i = 0; i < this.lineCount; ++i){
			int x1 = random.nextInt(width);
			int y1 = random.nextInt(height);
			int x2 = random.nextInt(width);
			int y2 = random.nextInt(height);
			g2.setColor(randomColor());
			g2.drawLine(x1, y1, x2, y2);
		}
	}
	
	/**
	 * 获取验证码值
	 * @return	验证码字符串
	 */
	public String getVerifyCode(){
		if(verifyCode == null){
			throw new RuntimeException("操作不规范!");
		}
		return verifyCode;
	}
	
	/**
	 * 讲验证码图片输出输出到输出流
	 * @param image			验证码图片对象
	 * @param out			输出流
	 * @throws IOException	
	 */
	public static void output(BufferedImage image, OutputStream out) throws IOException{
		ImageIO.write(image, "JPEG", out);
	}

}
 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值