- dsna
- 等级: 初级会员
- 性别:
- 文章: 14
- 积分: 80
- 来自: 北京
|
发表时间:2010-01-18 最后修改:2010-01-18
生成验证码效果
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;
-
-
-
-
-
- public class ValidateCode {
-
- private int width = 160;
-
- private int height = 40;
-
- private int codeCount = 5;
-
- private int lineCount = 150;
-
- private String code = null;
-
- 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();
- }
-
-
-
-
-
-
- public ValidateCode(int width,int height) {
- this.width=width;
- this.height=height;
- this.createCode();
- }
-
-
-
-
-
-
-
- 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;
-
-
- 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);
- }
-
-
- StringBuffer randomCode = new StringBuffer();
-
- 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);
- }
-
- 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.*;
-
-
-
-
-
- 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;
- }
- }
-
-
-
- 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());
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
测试类
ValidateCodeTest.java
- package cn.dsna.util.images;
-
- import java.io.IOException;
- import java.util.Date;
-
- public class ValidateCodeTest {
-
-
-
-
- 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>
- 大小: 2.1 KB
- 大小: 2.2 KB
- 大小: 2.1 KB
- 大小: 2.1 KB
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
-
id="cproIframe_u1617757" width="728" height="90" src="http://pos.baidu.com/acom?adn=3&at=103&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=14&ch=0&col=zh-CN&conOP=0&cpa=1&dai=2&dis=0<r=http%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3Djava%2520%25E5%259B%25BE%25E5%25BD%25A2%25E9%25AA%258C%25E8%25AF%2581%25E7%25A0%2581%26ie%3Dutf-8%26tn%3Dbaiduhome_pg%26oq%3Djava%2520%25E5%259B%25BE%25E5%25BD%25A2%25E9%25AA%258C%25E8%25AF%2581%25E7%25A0%2581%2520%25E5%25BC%2580%25E6%25BA%2590%26f%3D1%26rsv_bp%3D1%26rsv_enter%3D1%26rsv_sug3%3D142%26rsv_sug4%3D8368%26rsv_n%3D2%26rsv_sug1%3D48%26rsv_sug2%3D0%26inputT%3D3561%26bs%3Djava%2520%25E5%259B%25BE%25E5%25BD%25A2%25E9%25AA%258C%25E8%25AF%2581%25E7%25A0%2581%26rsp%3D0%26rsv_ers%3Dxz%26rs_src%3D0<u=http%3A%2F%2Fwww.iteye.com%2Ftopic%2F573456&lunum=6&n=csdnbaidu_cpr&pcs=1347x667&pis=10000x10000&ps=7877x372&psr=1366x768&pss=1347x7923&qn=8ebaf03b64c14628&rad=&rsi0=728&rsi1=90&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1617757&tn=text_default_728_90&tpr=1410521165892&ts=1&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1617757" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true">
|
返回顶楼 |
|
|
- jitabc
- 等级: 初级会员
- 性别:
- 文章: 153
- 积分: 10
- 来自: 北京
|
发表时间:2010-01-20 最后修改:2010-01-20
效果挺不错的,建议把I,1这种易混淆的去掉
|
返回顶楼 |
|
|
- dsna
- 等级: 初级会员
- 性别:
- 文章: 14
- 积分: 80
- 来自: 北京
|
jitabc 写道
效果挺不错的,建议把I,1这种易混淆的去掉
接收建议
测试的时候生成出来的I、1区别挺大的所以保留了,0、O 实在区分不出来就去掉了。
|
返回顶楼 |
|
|
- llfc
- 等级: 初级会员
- 性别:
- 文章: 6
- 积分: 30
- 来自: 西安
|
这种效果测试出来挺不错的。。。
|
返回顶楼 |
|
|
- phenom
- 等级: 初级会员
- 性别:
- 文章: 112
- 积分: 30
- 来自: 福州
|
这种看上去有点脏,不过比网络上流传的好多了。前几周论坛要验证码,好看多了,不知道是怎么弄的。
|
返回顶楼 |
|
|
- coolszy
- 等级:
- 性别:
- 文章: 77
- 积分: 130
- 来自: 南京
|
效果很不错,不会被识别
|
返回顶楼 |
|
|
- laitaogood
- 等级: 初级会员
- 性别:
- 文章: 167
- 积分: 40
- 来自: 豫章郡→紫禁城
|
好像最近很多都是文字验证的啊,也可以设置成文字的吧
|
返回顶楼 |
|
|
- ilove2009
- 等级: 初级会员
- 性别:
- 文章: 70
- 积分: 0
- 来自: 深圳
|
非常不错。学习啦
|
返回顶楼 |
|
|
- greatghoul
- 等级:
- 性别:
- 文章: 143
- 积分: 130
- 来自: 西安
|
生成的验证码效果不错呀,能不能生成中文的?
|
返回顶楼 |
|
|
- axhack
- 等级: 初级会员
- 性别:
- 文章: 63
- 积分: 10
- 来自: 吉林
|
这个太好的了。
收藏。
|
返回顶楼 |
|
|