发新话题
移动帖子 加入精华 加入置顶 加入收藏 关注此帖

验证图片生成





验证图片生成

自己写的一个验证图片生成代码,不晓得有啥要改进的没?
package image;

import java.util.Random;

public class ValidateCode {
	/*
	 * 数字常量数组
	 */
	private final char [] iValidateIntArray= {'1','2','3','4','5','6','7','8','9','0','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'};
	
	private int iCount;

	private String sValidate="";
	
	//初始化对象
	Random random=new Random();

	/*
	 * 构造方法
	 */
	ValidateCode(){
		
	}
	/*
	 * 带参数的构造方法
	 */
	ValidateCode(int iCount){
		this.iCount=iCount;
		create();
	}
	
	/*
	 * 生成验证字符串
	 */
	public void create() {
		int i=0;
		int index;
		String temp;
		while(i<(iCount)) {
			index=random.nextInt(iValidateIntArray.length);
			if (sValidate.indexOf(String.valueOf(iValidateIntArray[index]))<0) {
				sValidate=sValidate+String.valueOf(iValidateIntArray[index]);
				i++;
			}
		}
	}
	
	/*
	 * 返回验证码字符串
	 */
	public String getValidateString() {
		return sValidate;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ValidateCode vc=new ValidateCode(4);
		vc.create();
		System.out.println(vc.getValidateString());

	}

}

package image;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

public class ValidateImage {

	private String sVlaidateCode = "";
	private int iWidth;
	private int iHeight;
	private int iCount;
	private String sTempValidateString;

	// 初始化对象
	ValidateCode vc;
	BufferedImage image;
	Random random = new Random();

	/*
	 * 构造方法
	 */
	public void create() {
		iWidth = 60;
		iHeight = 20;
		iCount = 4;
		init();
	}

	public void create(int count, int width, int height) {
		iWidth = width;
		iHeight = height;
		iCount = count;
		init();
	}

	private void init() {
		vc = new ValidateCode(iCount);
		sTempValidateString = vc.getValidateString();
		createImage();
	}

	/*
	 * 随机生成颜色对象
	 */
	private 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 void createImage() {
		// 在内存中创建图像
		image = new BufferedImage(iWidth, iHeight, BufferedImage.TYPE_INT_RGB);
		Graphics g = image.getGraphics();
		g.setColor(getRandColor(191, 200));
		g.fillRect(0, 0, iWidth, iHeight);
		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
		// 生成干扰线 使图像的验证码不容易被其它程序探测到
		g.setColor(getRandColor(160, 200));
		for (int i = 0; i < 10; i++) {
			int x = random.nextInt(iWidth);
			int y = random.nextInt(iHeight);
			int xl = random.nextInt(iWidth);
			int yl = random.nextInt(iHeight);
			g.drawLine(x, y, x + xl, y + yl);
		}
		// 将认证码显示到图象中
		for (int i = 0; i < sTempValidateString.length(); i++) {
			g.setColor(new Color(20 + random.nextInt(110), 20 + random
					.nextInt(110), 20 + random.nextInt(110)));
			// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
			g.drawString(String.valueOf(sTempValidateString.charAt(i)),
					11 * i + 1, iHeight / 2 + 5);
		}
		g.dispose();
	}

	/*
	 * 返回图像对象
	 */
	public BufferedImage getValidateImage() {
		return image;
	}

	/*
	 * 返回验证码,生成全字母小写字符串
	 */
	public String getValidateCode() {
		return sTempValidateString.toLowerCase();
	}

}



编辑 回复 快速回复 TOP


Re:验证图片生成

晕,连注释都是我写的那个版本。哈哈!
快乐渡过每一天,减肥坚持每一天
编辑 回复 快速回复 TOP

Re:验证图片生成

图片生成 写来写去,都不成样,就抄你现成的了。
编辑 回复 快速回复 TOP

Re:验证图片生成

不知道有没,文字扭曲增加噪点的那样的验证码的示例啊。
编辑 回复 快速回复 TOP
发新话题