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

本文介绍了一个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<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);
		}
}
效果如图:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值