java验证码工具类(自己编写的,仅供参考)
2017-06-16 14:56
246 查看
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); } //创建BufferImage private 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; } //输出验证码到HttpResponse public void writeOut(OutputStream os){ try{ ImageIO.write(this.codeImage,"PNG",os); }catch (Exception e){ e.printStackTrace(); } } //输出验证码图片BufferedImage public 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); } }效果如图:
相关文章推荐
- 【自己编写的Java数组操作工具】
- 程序员修炼之路(六)编写自己的工具(JAVA注册系统级快捷键源码>jintellitype<)
- 一个自己编写的xml和JAVA对象之间的转换工具,xml和对象轻松转换(simple-xml2object)
- 我的Java开发学习之旅------>自己编写的Java数组操作工具
- Java-如何定制自己的工具库: mytools or myutil
- 自己用Java编写的简易计算器源代码
- 编写自己的PING工具
- 一个自己写的用来搜索各种文件格式生成txt清单的java工具
- 自己使用java做的小工具
- 自己编写JAVA环境下的文件上传组件
- Java编写验证码
- 自己动手编写启动JAVA程序的EXE文件
- java提供的类库,并不是一切都能支持你现有的功能的,要敢于编写自己的类。
- 自己编写的多线程Socket负载测试工具
- 用JAVA编写自己的机器人
- 自己动手编写Web服务工具[执行]
- 自己动手编写Web服务解析工具
- 编写一个Java应用程序,直接查询自己主机的IP地址和Internet上的某个www服务器地址
- Java编写验证码
- 编写Java GUI的好工具WindowBuilder Pro[包含Swt-Designer和Swing-Designer]