唉 很辛苦啊 花费了我 一下午的时间 来写图片验证码,总算是小有功效 。
嘿嘿 废话少说,先看效果!
嗯 整体感觉 还不错 嘿嘿 。(当然这是其中一张而已,由于背景色都是随机生成的,所有漂亮方面嘿嘿 自己选择咯 要死感觉那种好看 直接就把程序中的颜色写死 好了 嘿嘿 ) 不多少 贴代码 为了 进一步锻炼我的 编码规范 尽量不要出现荣冗余的代码 尽量把代码 写的美观一点 嘿嘿 下面就晒一下我的 代码咯 希望大家 一起讨论进步哈 ,有不对的地方 可以还请指教!谢谢!
package com.dk.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
public class ValidateCode {
/**
* @see 图片验证码提供类
* @author 邓葵 QQ:467529125 <E-mail:467529125@qq.com>
* @param 属性说明:
* @param imageBuffer 生成图片对象
* @param fontSize 图片码字体大小 初始化参数为:35
* @param positionStr1 第一个位置的字符
* @param positionStr2 第二个位置的字符
* @param positionStr3 第三个位置的字符
* @param positionStr4 第四个位置的字符
* @param imageStr 图片上的字符
* @param imageWidth 图片初始化宽度 初始化宽度为:200
* @param imageHeight 图片初始化高度 初始化高度为:50
* @param random 产生随机数机器
* @param String[] collectStr 所有字符产生集合
* @param Graphics2D g 绘图对象
* @param fontType 图片码字体类型 初始化参数为:微软雅黑
* @param imageFilePath 验证码输出路径
* @param
* @param
* @param 方法说明:
* @param Color getColor(); 产生一个随机背景色
* @param Color reverseColor(Color c) 产生一个随机背景色的反色(即设置为图片字体颜色)
* @param methodForGetImageStr() 获得从String字符集合中产生的4个字符
* @param methodForDrawImagePic() 使用绘图对象Grahics2D g对象绘制验证码图形
* @param methodForOutPutImageToClient() 将图片输出到客户端
* @param ValidateCode() 图片验证码生成入口
* @param
*/
private BufferedImage imageBuffer = null; /*生成图片对象*/
private int fontSize = 35; /*图片码字体大小 初始化参数为:35*/
private String fontType ="微软雅黑"; /*图片码字体类型 初始化参数为:微软雅黑*/
private String positionStr1; /*第一个位置的字符*/
private String positionStr2; /*第二个位置的字符*/
private String positionStr3; /*第三个位置的字符*/
private String positionStr4; /*第四个位置的字符*/
private String imageStr; /*图片上的字符*/
private int imageWidth = 200; /*图片初始化宽度*/
private int imageHeight = 50; /*图片初始化高度*/
private String imageFilePath = "c:validateImage.jpg"; /*验证码输出路径*/
private static Random random = new Random(); /*产生随机数机器*/
public static Color getColor() { /*产生一个随机背景色*/
return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
}
public static Color reverseColor(Color c) { /*产生一个随机背景色的反色(即设置为图片字体颜色)*/
return new Color(255 - c.getRed(), 255 - c.getGreen(), 255 - c.getRed());
}
private String methodForGetImageStr() { /* 获得从String[] collectStr字符集合中产生的4个字符*/
//所有字符产生集合
String[] collectStr = { "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", "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" };
//产生每一个字符串位置的随机字符
positionStr1 = collectStr[random.nextInt(collectStr.length)];
positionStr2 = collectStr[random.nextInt(collectStr.length)];
positionStr3 = collectStr[random.nextInt(collectStr.length)];
positionStr4 = collectStr[random.nextInt(collectStr.length)];
//将以上随机字符组合成图片四个随机字符
imageStr = positionStr1 + positionStr2 + positionStr3 + positionStr4;
//加上空格显示在以便于显示在图片上看起来不是那么紧凑
String str = positionStr1 + " " + positionStr2 + " " + positionStr3 + " " + positionStr4;
return str;
}
private void methodForDrawImagePic() { /*使用绘图对象Grahics2D g对象绘制验证码图形*/
//获取绘图对象
Graphics2D g = imageBuffer.createGraphics();
//绘制干扰线大小为0-3F粗细
g.setStroke(new BasicStroke((float)(random.nextInt(3))));
//获取背景颜色
Color color = this.getColor();
//设置背景颜色
g.setColor(color);
//设置背景颜色从x=0,y=0开始化 背景色区域宽度为:imageWidth 背景色区域高度为:imageHeight
g.fillRect(0, 0, imageWidth, imageHeight);
//产生10条干扰线条
for (int i = 0; i < 10; i++) {
//设置干扰线输出的随机颜色
g.setColor(new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255)));
//随机输出干扰线在图片的位置
g.drawLine(random.nextInt(200),random.nextInt(50),random.nextInt(200),random.nextInt(50));
}
//随机产生100个噪音点
for (int i = 0; i < 100; i++) {
//设置噪音点的大小宽度:0 高度:1 覆盖整个图片区域
g.drawRect(random.nextInt(imageWidth), random.nextInt(imageHeight), 0, 1);
}
//设置字体大小,字体类型
g.setFont(new Font(fontType, Font.BOLD, fontSize));
//获取图片上的四个字符
String str = methodForGetImageStr();
//取得背景色的反色
Color reverseColor = reverseColor(color);
//设置字体颜色为背景颜色的反色
g.setColor(reverseColor);
//将文字字符画在图片上
g.drawString(str, 30, 40);
}
private void methodForOutPutImageToClient() throws IOException { /*将图片输出到客户端*/
//生成图片
methodForDrawImagePic();
//使用ImageIO对象直接将imageBuffer对象以JPG 的格式传给客户端
ImageIO.write(imageBuffer, "jpg", new File(imageFilePath));
}
//图片验证码生成入口
public ValidateCode() {
try {
imageBuffer = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
methodForOutPutImageToClient();
} catch (Exception e) {
e.printStackTrace();
}
}
// 返回图片验证码字符
public String getImageStr() {
return imageStr;
}
public static void main(String args[]) {
ValidateCode code = new ValidateCode();
System.out.println("验证码字符:\t"+code.getImageStr());
}
}