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

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
效果如图:

你可能感兴趣的:(java验证码,java验证码工具类,java个人工具类)