java验证码工具类(自己编写的,仅供参考)

来源:互联网 发布:装修设计实用软件 编辑:程序博客网 时间:2024/05/21 01:30
package com.chenb.test;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;import javax.swing.ButtonGroup;public class VerifyCodeUtils {//验证码private String codeText;//验证码的宽(默认110)private Integer codeImgWidth;//验证码图片的高(默认25)private Integer codeImgHeight;//验证码的个数(默认4)private Integer codeSize;//验证码的字体大小private Integer fontSize;//干扰线的条数(默认为5)private Integer lineSize;//干扰线的透明度(100)alpha 值为 255 则意味着颜色完全是不透明的,alpha 值为 0意味着颜色是完全透明的private Integer lineAlpha;//噪点(干扰点)的个数private Integer randomPointSize;//背景色private Color backgroundColor;//纯数字验证码private final static Integer MODE_FULL_NUMBER=1;//纯字母验证码private final static Integer MODE_FULL_CHAR=2;//数字和字母组合验证码private final  static Integer MODE_NUMBER_AND_CHAE=3;//验证码的类型private  Integer modeType;//验证码图片private  BufferedImage codeImage;//构造方法public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize,Integer fontSize,Integer lineSize,Integer lineAlpha,Integer randomPointSize,Color backgroundColor,Integer modeType){this.codeImgHeight=codeImgWidth;this.codeImgHeight=codeImgHeight;this.codeSize=codeSize;this.fontSize=fontSize;this.lineSize=lineSize;this.lineAlpha=lineAlpha;this.randomPointSize=randomPointSize;this.backgroundColor=backgroundColor;this.modeType=modeType;this.codeImage=createCodeImage();}//构造方法public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize,Integer fontSize,Integer lineSize,Integer lineAlpha,Integer randomPointSize,Color backgroundColor){this(codeImgWidth,codeImgHeight,codeSize,fontSize,lineSize,lineAlpha,randomPointSize,backgroundColor,null);}public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize,Integer fontSize,Integer lineSize,Integer lineAlpha,Integer randomPointSize){this(codeImgWidth,codeImgHeight,codeSize,fontSize,lineSize,lineAlpha,randomPointSize,null);}public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize,Integer fontSize,Integer lineSize,Integer lineAlpha){this(codeImgWidth,codeImgHeight,codeSize,fontSize,lineSize,lineAlpha,null);}public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize,Integer fontSize,Integer lineSize){this(codeImgWidth,codeImgHeight,codeSize,fontSize,lineSize,null);}public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize,Integer fontSize){this(codeImgWidth,codeImgHeight,codeSize,fontSize,null);}public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight,Integer codeSize){this(codeImgWidth,codeImgHeight,codeSize,null);}public VerifyCodeUtils(Integer codeImgWidth,Integer codeImgHeight){this(codeImgWidth,codeImgHeight,null);}public VerifyCodeUtils(){this(null,null);}//生成0~9随机数private Integer getRandomNumber(){Random random=new Random();return random.nextInt(10);}//生成a-z和A-Z随机字符private char getRandomChar() {Random random=new Random();int randomNum=random.nextInt(26);int Lower_Or_UpperCase=random.nextInt(2);if(Lower_Or_UpperCase==0){return (char)(randomNum+65);}return (char)(randomNum+97);}//生成随机颜色 alpha 值为 255 则意味着颜色完全是不透明的,alpha 值为 0意味着颜色是完全透明的private Color getRandomColor(Integer alpha){Random random=new Random();int red=random.nextInt(150);int green=random.nextInt(150);int blue=random.nextInt(150);if(alpha==null){alpha=255;}return new Color(red,green,blue,alpha);}//创建BufferImageprivate BufferedImage createBufferedImage(){if(this.codeImgWidth==null){this.codeImgWidth=110;}if(this.codeImgHeight==null){this.codeImgHeight=25;}BufferedImage bufferedImage=new BufferedImage(this.codeImgWidth,this.codeImgHeight,BufferedImage.TYPE_INT_RGB);Graphics2D graphics2D=(Graphics2D)bufferedImage.getGraphics();if(this.backgroundColor==null){backgroundColor=new Color(255,255,255);}graphics2D.setBackground(this.backgroundColor);graphics2D.fillRect(0,0,codeImgWidth,codeImgHeight);return bufferedImage;}//画干扰线private BufferedImage drawLine(BufferedImage bufferedImage){Random random=new Random();if(this.lineSize==null){lineSize=5;}Graphics2D graphics2D=(Graphics2D)bufferedImage.getGraphics();for(int i=0;i<lineSize;i++){//直线起点的x坐标int startX=random.nextInt(bufferedImage.getWidth()-1);//直线起点的y坐标int startY=random.nextInt(bufferedImage.getHeight()-1);//直线结束点的x坐标int endX=random.nextInt(bufferedImage.getWidth()-1);//直线结束点的y坐标int endY=random.nextInt(bufferedImage.getHeight()-1);if(this.lineAlpha==null){this.lineAlpha=100;}//直线的颜色graphics2D.setColor(getRandomColor(lineAlpha));// 处理干扰线与边框的交界(1f为线条的宽度)graphics2D.setStroke(new BasicStroke(1f));graphics2D.drawLine(startX,startY,endX,endY);}return bufferedImage;}//添加噪声点//画干扰线private BufferedImage drawRandomPoint(BufferedImage bufferedImage){Random random=new Random();if(this.randomPointSize==null){randomPointSize=20;}for(int i=0;i<randomPointSize;i++){int x=random.nextInt(bufferedImage.getWidth()-1);int y=random.nextInt(bufferedImage.getHeight()-1);int rgb=getRandomColor(100).getRGB();bufferedImage.setRGB(x, y, rgb);}return bufferedImage;}//根据模式生成验证码字符private String careateCode(){String result="";if(this.modeType==null){this.modeType=MODE_NUMBER_AND_CHAE;}if(modeType==MODE_FULL_NUMBER){result=getRandomNumber()+"";}if(modeType==MODE_FULL_CHAR){result=getRandomChar()+"";}if(modeType==MODE_NUMBER_AND_CHAE){Random random=new Random();int randomChoseNum=random.nextInt(2);if(randomChoseNum==0){result=getRandomNumber()+"";}else{result=getRandomChar()+"";}}return result;}//得到验证码public String getCodeText(){return this.codeText;}//输出验证码到HttpResponsepublic void writeOut(OutputStream os){try{ImageIO.write(this.codeImage,"PNG",os);}catch (Exception e){e.printStackTrace();}}//输出验证码图片BufferedImagepublic BufferedImage getBufferedImage(){return this.codeImage;}//设置字体private Font getFont() {if(this.fontSize==null){fontSize=19;}return new Font("Fixedsys", Font.CENTER_BASELINE, fontSize);}//生成验证码图片private BufferedImage createCodeImage(){BufferedImage image = createBufferedImage();//创建图片缓冲区image=drawLine(image);image=drawRandomPoint(image);Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境g2.setStroke(new BasicStroke(1.5f));g2.setFont(getFont());if(this.codeSize==null){this.codeSize=4;}//用来装验证码StringBuilder sb=new StringBuilder();for(int i=0;i<codeSize;i++) {g2.setColor(getRandomColor(null));String codeChar=careateCode();sb.append(codeChar);//平移或旋转变换Random random=new Random(System.nanoTime());AffineTransform affine = new AffineTransform();affine.setToRotation(Math.PI / 4 * random.nextDouble() * (random.nextBoolean() ? 1 : -1), (codeImgWidth/ codeSize) * i + 18/2, codeImgHeight/2);g2.setTransform(affine);// 首字符的基线位于用户空间中的 (x,y) 位置处g2.drawString(codeChar, 6 + i * (codeImgWidth/codeSize), Math.round(codeImgHeight*0.8));}this.codeText=sb.toString();//验证码文本return image;}public static void main(String[] args) throws FileNotFoundException { VerifyCodeUtils verifyCodeUtils = new VerifyCodeUtils(200,40,null,25); //验证码 String verifyCode=verifyCodeUtils.getCodeText(); //本地测试输出到桌面,保存为图片(在项目中使用response.getOutputStream()) OutputStream oStream=new FileOutputStream(new File("C:\\Users\\hasee\\Desktop","verifyCodeImg.jpg"));             verifyCodeUtils.writeOut(oStream);             System.out.println("验证码为:"+verifyCode);}}
效果如图: