Java生成图形验证码工具类
程序员文章站
2024-03-06 23:41:26
生成验证码效果
validatecode.java 验证码生成类
package cn.dsna.util.images;
import java.aw...
生成验证码效果
validatecode.java 验证码生成类
package cn.dsna.util.images; import java.awt.color; import java.awt.font; import java.awt.graphics2d; import java.awt.image.bufferedimage; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.util.random; import javax.imageio.imageio; /** * 验证码生成器 * @author dsna * */ public class validatecode { // 图片的宽度。 private int width = 160; // 图片的高度。 private int height = 40; // 验证码字符个数 private int codecount = 5; // 验证码干扰线数 private int linecount = 150; // 验证码 private string code = null; // 验证码图片buffer private bufferedimage buffimg=null; private char[] codesequence = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9' }; public validatecode() { this.createcode(); } /** * * @param width 图片宽 * @param height 图片高 */ public validatecode(int width,int height) { this.width=width; this.height=height; this.createcode(); } /** * * @param width 图片宽 * @param height 图片高 * @param codecount 字符个数 * @param linecount 干扰线条数 */ public validatecode(int width,int height,int codecount,int linecount) { this.width=width; this.height=height; this.codecount=codecount; this.linecount=linecount; this.createcode(); } public void createcode() { int x = 0,fontheight=0,codey=0; int red = 0, green = 0, blue = 0; x = width / (codecount +2);//每个字符的宽度 fontheight = height - 2;//字体的高度 codey = height - 4; // 图像buffer buffimg = new bufferedimage(width, height,bufferedimage.type_int_rgb); graphics2d g = buffimg.creategraphics(); // 生成随机数 random random = new random(); // 将图像填充为白色 g.setcolor(color.white); g.fillrect(0, 0, width, height); // 创建字体 imgfontbyte imgfont=new imgfontbyte(); font font =imgfont.getfont(fontheight); g.setfont(font); for (int i = 0; i < linecount; i++) { int xs = random.nextint(width); int ys = random.nextint(height); int xe = xs+random.nextint(width/8); int ye = ys+random.nextint(height/8); red = random.nextint(255); green = random.nextint(255); blue = random.nextint(255); g.setcolor(new color(red, green, blue)); g.drawline(xs, ys, xe, ye); } // randomcode记录随机产生的验证码 stringbuffer randomcode = new stringbuffer(); // 随机产生codecount个字符的验证码。 for (int i = 0; i < codecount; i++) { string strrand = string.valueof(codesequence[random.nextint(codesequence.length)]); // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。 red = random.nextint(255); green = random.nextint(255); blue = random.nextint(255); g.setcolor(new color(red, green, blue)); g.drawstring(strrand, (i + 1) * x, codey); // 将产生的四个随机数组合在一起。 randomcode.append(strrand); } // 将四位数字的验证码保存到session中。 code=randomcode.tostring(); } public void write(string path) throws ioexception { outputstream sos = new fileoutputstream(path); this.write(sos); } public void write(outputstream sos) throws ioexception { imageio.write(buffimg, "png", sos); sos.close(); } public bufferedimage getbuffimg() { return buffimg; } public string getcode() { return code; } }
imgfontbyte.java
package cn.dsna.util.images; import java.io.bytearrayinputstream; import java.awt.*; /** * ttf字体文件 * @author dsna * */ public class imgfontbyte { public font getfont(int fontheight){ try { font basefont = font.createfont(font.truetype_font, new bytearrayinputstream(hex2byte(getfontbytestr()))); return basefont.derivefont(font.plain, fontheight); } catch (exception e) { return new font("arial",font.plain, fontheight); } } private byte[] hex2byte(string str) { if (str == null) return null; str = str.trim(); int len = str.length(); if (len == 0 || len % 2 == 1) return null; byte[] b = new byte[len / 2]; try { for (int i = 0; i < str.length(); i += 2) { b[i / 2] = (byte) integer .decode("0x" + str.substring(i, i + 2)).intvalue(); } return b; } catch (exception e) { return null; } } /** * ttf字体文件的十六进制字符串 * @return */ private string getfontbytestr(){ return null; return str;//字符串太长 在附件中找 } }
validatecodeservlet.java servlet调用方法
package cn.dsna.util.images; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; public class validatecodeservlet extends httpservlet { private static final long serialversionuid = 1l; @override protected void doget(httpservletrequest reqeust, httpservletresponse response) throws servletexception, ioexception { // 设置响应的类型格式为图片格式 response.setcontenttype("image/jpeg"); //禁止图像缓存。 response.setheader("pragma", "no-cache"); response.setheader("cache-control", "no-cache"); response.setdateheader("expires", 0); httpsession session = reqeust.getsession(); validatecode vcode = new validatecode(120,40,5,100); session.setattribute("code", vcode.getcode()); vcode.write(response.getoutputstream()); } /** * web.xml 添加servlet <servlet> <servlet-name>validatecodeservlet</servlet-name> <servlet-class>cn.dsna.util.images.validatecodeservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>validatecodeservlet</servlet-name> <url-pattern>*.images</url-pattern> </servlet-mapping> 在地址栏输入xxx/dsna.images 测试 */ }
测试类
validatecodetest.java
package cn.dsna.util.images; import java.io.ioexception; import java.util.date; public class validatecodetest { /** * @param args */ public static void main(string[] args) { validatecode vcode = new validatecode(120,40,5,100); try { string path="d:/t/"+new date().gettime()+".png"; system.out.println(vcode.getcode()+" >"+path); vcode.write(path); } catch (ioexception e) { e.printstacktrace(); } } }
web.xml 配置
<servlet> <servlet-name>validatecodeservlet</servlet-name> <servlet-class>cn.dsna.util.images.validatecodeservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>validatecodeservlet</servlet-name> <url-pattern>*.images</url-pattern> </servlet-mapping>
以上所述是小编给大家介绍的java生成图形验证码工具类,希望对大家有所帮助