概述
importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.image.BufferedImage;importjava.util.Random;public classCpachaUtil {/*** 验证码来源*/
final private char[] code = { '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F','G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};/*** 字体*/
final private String[] fontNames = new String[] { "黑体", "宋体", "Courier", "Arial", "Verdana", "Times", "Tahoma","Georgia"};/*** 字体样式*/
final private int[] fontStyles = new int[] { Font.BOLD, Font.ITALIC |Font.BOLD };/*** 验证码长度 默认4个字符*/
private int vcodeLen = 4;/*** 验证码图片字体大小 默认17*/
private int fontsize = 21;/*** 验证码图片宽度*/
private int width = (fontsize + 1) * vcodeLen + 10;/*** 验证码图片高度*/
private int height = fontsize + 12;/*** 干扰线条数 默认3条*/
private int disturbline = 3;publicCpachaUtil() {
}/*** 指定验证码长度
*
*@paramvcodeLen 验证码长度*/
public CpachaUtil(intvcodeLen) {this.vcodeLen =vcodeLen;this.width = (fontsize + 1) * vcodeLen + 10;
}/*** 指定验证码长度、图片宽度、高度
*
*@paramvcodeLen
*@paramwidth
*@paramheight*/
public CpachaUtil(int vcodeLen, int width, intheight) {this.vcodeLen =vcodeLen;this.width =width;this.height =height;
}/*** 生成验证码图片
*
*@paramvcode 要画的验证码
*@paramdrawline 是否画干扰线
*@return
*/
public BufferedImage generatorVCodeImage(String vcode, booleandrawline) {//创建验证码图片
BufferedImage vcodeImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g=vcodeImage.getGraphics();//填充背景色
g.setColor(new Color(246, 240, 250));
g.fillRect(0, 0, width, height);if(drawline) {
drawDisturbLine(g);
}//用于生成伪随机数
Random ran = newRandom();//在图片上画验证码
for (int i = 0; i < vcode.length(); i++) {//设置字体
g.setFont(newFont(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)],
fontsize));//随机生成颜色
g.setColor(getRandomColor());//画验证码
g.drawString(vcode.charAt(i) + "", i * fontsize + 10, fontsize + 5);
}//释放此图形的上下文以及它使用的所有系统资源
g.dispose();returnvcodeImage;
}/*** 获得旋转字体的验证码图片
*
*@paramvcode
*@paramdrawline 是否画干扰线
*@return
*/
public BufferedImage generatorRotateVCodeImage(String vcode, booleandrawline) {//创建验证码图片
BufferedImage rotateVcodeImage = newBufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=rotateVcodeImage.createGraphics();//填充背景色
g2d.setColor(new Color(246, 240, 250));
g2d.fillRect(0, 0, width, height);if(drawline) {
drawDisturbLine(g2d);
}//在图片上画验证码
for (int i = 0; i < vcode.length(); i++) {
BufferedImage rotateImage=getRotateImage(vcode.charAt(i));
g2d.drawImage(rotateImage,null, (int) (this.height * 0.7) * i, 0);
}
g2d.dispose();returnrotateVcodeImage;
}/*** 生成验证码
*
*@return验证码*/
publicString generatorVCode() {int len =code.length;
Random ran= newRandom();
StringBuffer sb= newStringBuffer();for (int i = 0; i < vcodeLen; i++) {int index =ran.nextInt(len);
sb.append(code[index]);
}returnsb.toString();
}/*** 为验证码图片画一些干扰线
*
*@paramg*/
private voiddrawDisturbLine(Graphics g) {
Random ran= newRandom();for (int i = 0; i < disturbline; i++) {int x1 =ran.nextInt(width);int y1 =ran.nextInt(height);int x2 =ran.nextInt(width);int y2 =ran.nextInt(height);
g.setColor(getRandomColor());//画干扰线
g.drawLine(x1, y1, x2, y2);
}
}/*** 获取一张旋转的图片
*
*@paramc 要画的字符
*@return
*/
private BufferedImage getRotateImage(charc) {
BufferedImage rotateImage= newBufferedImage(height, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d=rotateImage.createGraphics();//设置透明度为0
g2d.setColor(new Color(255, 255, 255, 0));
g2d.fillRect(0, 0, height, height);
Random ran= newRandom();
g2d.setFont(newFont(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)],
fontsize));
g2d.setColor(getRandomColor());double theta =getTheta();//旋转图片
g2d.rotate(theta, height / 2, height / 2);
g2d.drawString(Character.toString(c), (height- fontsize) / 2, fontsize + 5);
g2d.dispose();returnrotateImage;
}/***@return返回一个随机颜色*/
privateColor getRandomColor() {
Random ran= newRandom();return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));
}/***@return角度*/
private doublegetTheta() {return ((int) (Math.random() * 1000) % 2 == 0 ? -1 : 1) *Math.random();
}/***@return验证码字符个数*/
public intgetVcodeLen() {returnvcodeLen;
}/*** 设置验证码字符个数
*
*@paramvcodeLen*/
public void setVcodeLen(intvcodeLen) {this.width = (fontsize + 3) * vcodeLen + 10;this.vcodeLen =vcodeLen;
}/***@return字体大小*/
public intgetFontsize() {returnfontsize;
}/*** 设置字体大小
*
*@paramfontsize*/
public void setFontsize(intfontsize) {this.width = (fontsize + 3) * vcodeLen + 10;this.height = fontsize + 15;this.fontsize =fontsize;
}/***@return图片宽度*/
public intgetWidth() {returnwidth;
}/*** 设置图片宽度
*
*@paramwidth*/
public void setWidth(intwidth) {this.width =width;
}/***@return图片高度*/
public intgetHeight() {returnheight;
}/*** 设置图片高度
*
*@paramheight*/
public void setHeight(intheight) {this.height =height;
}/***@return干扰线条数*/
public intgetDisturbline() {returndisturbline;
}/*** 设置干扰线条数
*
*@paramdisturbline*/
public void setDisturbline(intdisturbline) {this.disturbline =disturbline;
}
}
最后
以上就是俏皮电源为你收集整理的java 验证码插件_登录验证码插件的全部内容,希望文章能够帮你解决java 验证码插件_登录验证码插件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复