博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
自己封装的一个java图片验证码
阅读量:6333 次
发布时间:2019-06-22

本文共 9076 字,大约阅读时间需要 30 分钟。

原文:http://www.cnblogs.com/chiangchou/p/VCodeGenerator.html

验证码生成器:

1 package com.lz.Tools;  2   3 import java.awt.Color;  4 import java.awt.Font;  5 import java.awt.Graphics;  6 import java.awt.Graphics2D;  7 import java.awt.image.BufferedImage;  8 import java.util.Random;  9  10 /** 11  * 验证码生成器 12  *  13  * @author bojiangzhou 14  */ 15 public class VCodeGenerator { 16      17     /** 18      * 验证码来源 19      */ 20     final private char[] code = { 21         '2', '3', '4', '5', '6', '7', '8', '9', 22         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 23         'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v',  24         'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 25         'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 26         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' 27     }; 28     /** 29      * 字体 30      */ 31     final private String[] fontNames = new String[]{ 32             "黑体", "宋体", "Courier", "Arial",  33             "Verdana", "Times", "Tahoma", "Georgia"}; 34     /** 35      * 字体样式 36      */ 37     final private int[] fontStyles = new int[]{ 38             Font.BOLD, Font.ITALIC|Font.BOLD 39     }; 40      41     /** 42      * 验证码长度 43      * 默认4个字符 44      */ 45     private int vcodeLen = 4; 46     /** 47      * 验证码图片字体大小 48      * 默认17 49      */ 50     private int fontsize = 21; 51     /** 52      * 验证码图片宽度 53      */ 54     private int width = (fontsize+1)*vcodeLen+10; 55     /** 56      * 验证码图片高度 57      */ 58     private int height = fontsize+12; 59     /** 60      * 干扰线条数 61      * 默认3条 62      */ 63     private int disturbline = 3; 64      65      66     public VCodeGenerator(){} 67      68     /** 69      * 指定验证码长度 70      * @param vcodeLen 验证码长度 71      */ 72     public VCodeGenerator(int vcodeLen) { 73         this.vcodeLen = vcodeLen; 74         this.width = (fontsize+1)*vcodeLen+10; 75     } 76      77     /** 78      * 生成验证码图片 79      * @param vcode 要画的验证码 80      * @param drawline 是否画干扰线 81      * @return 82      */ 83     public BufferedImage generatorVCodeImage(String vcode, boolean drawline){ 84         //创建验证码图片 85         BufferedImage vcodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 86         Graphics g = vcodeImage.getGraphics(); 87         //填充背景色 88         g.setColor(new Color(246, 240, 250)); 89         g.fillRect(0, 0, width, height); 90         if(drawline){ 91             drawDisturbLine(g); 92         } 93         //用于生成伪随机数 94         Random ran = new Random(); 95         //在图片上画验证码 96         for(int i = 0;i < vcode.length();i++){ 97             //设置字体 98             g.setFont(new Font(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)], fontsize)); 99             //随机生成颜色100             g.setColor(getRandomColor());101             //画验证码102             g.drawString(vcode.charAt(i)+"", i*fontsize+10, fontsize+5);103         }104         //释放此图形的上下文以及它使用的所有系统资源105         g.dispose();106         107         return vcodeImage;108     }109     /**110      * 获得旋转字体的验证码图片111      * @param vcode112      * @param drawline 是否画干扰线113      * @return114      */115     public BufferedImage generatorRotateVCodeImage(String vcode, boolean drawline){116         //创建验证码图片117         BufferedImage rotateVcodeImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);118         Graphics2D g2d = rotateVcodeImage.createGraphics();119         //填充背景色120         g2d.setColor(new Color(246, 240, 250));121         g2d.fillRect(0, 0, width, height);122         if(drawline){123             drawDisturbLine(g2d);124         }125         //在图片上画验证码126         for(int i = 0;i < vcode.length();i++){127             BufferedImage rotateImage = getRotateImage(vcode.charAt(i));128             g2d.drawImage(rotateImage, null, (int) (this.height * 0.7) * i, 0);129         }130         g2d.dispose();131         return rotateVcodeImage;132     }133     /**134      * 生成验证码135      * @return 验证码136      */137     public String generatorVCode(){138         int len = code.length;139         Random ran = new Random();140         StringBuffer sb = new StringBuffer();141         for(int i = 0;i < vcodeLen;i++){142             int index = ran.nextInt(len);143             sb.append(code[index]);144         }145         return sb.toString();146     }147     /**148      * 为验证码图片画一些干扰线149      * @param g 150      */151     private void drawDisturbLine(Graphics g){152         Random ran = new Random();153         for(int i = 0;i < disturbline;i++){154             int x1 = ran.nextInt(width);155             int y1 = ran.nextInt(height);156             int x2 = ran.nextInt(width);157             int y2 = ran.nextInt(height);158             g.setColor(getRandomColor());159             //画干扰线160             g.drawLine(x1, y1, x2, y2);161         }162     }163     /**164      * 获取一张旋转的图片165      * @param c 要画的字符166      * @return167      */168     private BufferedImage getRotateImage(char c){169         BufferedImage rotateImage = new BufferedImage(height, height, BufferedImage.TYPE_INT_ARGB);170         Graphics2D g2d = rotateImage.createGraphics();171         //设置透明度为0172         g2d.setColor(new Color(255, 255, 255, 0));173         g2d.fillRect(0, 0, height, height);174         Random ran = new Random();175         g2d.setFont(new Font(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)], fontsize));176         g2d.setColor(getRandomColor());177         double theta = getTheta();178         //旋转图片179         g2d.rotate(theta, height/2, height/2);180         g2d.drawString(Character.toString(c), (height-fontsize)/2, fontsize+5);181         g2d.dispose();182         183         return rotateImage;184     }185     /**186      * @return 返回一个随机颜色187      */188     private Color getRandomColor(){189         Random ran = new Random();190         return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220)); 191     }192     /**193      * @return 角度194      */195     private double getTheta(){196         return ((int) (Math.random()*1000) % 2 == 0 ? -1 : 1)*Math.random();197     }198 199     /**200      * @return 验证码字符个数201      */202     public int getVcodeLen() {203         return vcodeLen;204     }205     /**206      * 设置验证码字符个数207      * @param vcodeLen208      */209     public void setVcodeLen(int vcodeLen) {210         this.width = (fontsize+3)*vcodeLen+10;211         this.vcodeLen = vcodeLen;212     }213     /**214      * @return 字体大小215      */216     public int getFontsize() {217         return fontsize;218     }219     /**220      * 设置字体大小221      * @param fontsize222      */223     public void setFontsize(int fontsize) {224         this.width = (fontsize+3)*vcodeLen+10;225         this.height = fontsize+15;226         this.fontsize = fontsize;227     }228     /**229      * @return 图片宽度230      */231     public int getWidth() {232         return width;233     }234     /**235      * 设置图片宽度236      * @param width237      */238     public void setWidth(int width) {239         this.width = width;240     }241     /**242      * @return 图片高度243      */244     public int getHeight() {245         return height;246     }247     /**248      * 设置图片高度249      * @param height 250      */251     public void setHeight(int height) {252         this.height = height;253     }254     /**255      * @return 干扰线条数256      */257     public int getDisturbline() {258         return disturbline;259     }260     /**261      * 设置干扰线条数262      * @param disturbline263      */264     public void setDisturbline(int disturbline) {265         this.disturbline = disturbline;266     }267     268 }

在servlet里获取验证码:

1 package com.lz.Servlet; 2  3 import java.awt.image.BufferedImage; 4 import java.io.IOException; 5  6 import javax.imageio.ImageIO; 7 import javax.servlet.ServletException; 8 import javax.servlet.http.HttpServlet; 9 import javax.servlet.http.HttpServletRequest;10 import javax.servlet.http.HttpServletResponse;11 12 import com.lz.Tools.VCodeGenerator;13 14 15 16 /**17  * 获取验证码18  * @author bojiangzhou19  *20  */21 public class GetVCodeServlet extends HttpServlet {22 23     private static final long serialVersionUID = 3259532010990625726L;24 25     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {26         //验证码不能缓存27         response.setHeader("Expires", "-1");28         response.setHeader("cache-control", "no-cahce");29         response.setHeader("pragma", "no-cache");30         31         VCodeGenerator vcg = new VCodeGenerator();32         //取得验证码33         String vcode = vcg.generatorVCode();34         //获取验证码图片35 //        BufferedImage vcodeImage = vcg.generatorRotateVCodeImage(vcode, true);36         BufferedImage vcodeImage = vcg.generatorVCodeImage(vcode, true);37         //将验证码保存到session域对象38         request.getSession().setAttribute("vcode", vcode);39         //输出验证码图片40         ImageIO.write(vcodeImage, "gif", response.getOutputStream());41     }42     43 }

前台在img标签里用src获取验证码图片即可

 

转载于:https://www.cnblogs.com/Bloglwu/p/8744257.html

你可能感兴趣的文章
CentOS 6.5下编译安装新版LNMP
查看>>
Android Picasso
查看>>
top命令
查看>>
javascript的作用域
查看>>
新形势下初创B2B行业网站如何经营
查看>>
初心大陆-----python宝典 第五章之列表
查看>>
java基础学习2
查看>>
sysbench使用笔记
查看>>
有关电子商务信息的介绍
查看>>
NFC·(近距离无线通讯技术)
查看>>
多线程基础(三)NSThread基础
查看>>
PHP的学习--Traits新特性
查看>>
ubuntu下,py2,py3共存,/usr/bin/python: No module named virtualenvwrapper错误解决方法
查看>>
Ext.form.field.Number numberfield
查看>>
Linux文件夹分析
查看>>
解决部分月份绩效无法显示的问题:timestamp\union al\autocommit等的用法
查看>>
nginx 域名跳转 Nginx跳转自动到带www域名规则配置、nginx多域名向主域名跳转
查看>>
man openstack >>1.txt
查看>>
linux几大服务器版本大比拼
查看>>
在BT5系统中安装postgresQL
查看>>