验证码生成器.docx
- 文档编号:170687
- 上传时间:2022-10-05
- 格式:DOCX
- 页数:7
- 大小:14.03KB
验证码生成器.docx
《验证码生成器.docx》由会员分享,可在线阅读,更多相关《验证码生成器.docx(7页珍藏版)》请在冰豆网上搜索。
packagecom.lizhou.tools;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.image.BufferedImage;
importjava.util.Random;
/**
*验证码生成器
*
*@authorbojiangzhou
*/
publicclassVCodeGenerator{
/**
*验证码来源
*/
finalprivatechar[]code={
'2','3','4','5','6','7','8','9',
'a','b','c','d','e','f','g','h','i','j',
'k','m','n','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F',
'G','H','J','K','L','M','N','P','Q','R',
'S','T','U','V','W','X','Y','Z'
};
/**
*字体
*/
finalprivateString[]fontNames=newString[]{
"黑体","宋体","Courier","Arial",
"Verdana","Times","Tahoma","Georgia"};
/**
*字体样式
*/
finalprivateint[]fontStyles=newint[]{
Font.BOLD,Font.ITALIC|Font.BOLD
};
/**
*验证码长度
*默认4个字符
*/
privateintvcodeLen=4;
/**
*验证码图片字体大小
*默认17
*/
privateintfontsize=21;
/**
*验证码图片宽度
*/
privateintwidth=(fontsize+1)*vcodeLen+10;
/**
*验证码图片高度
*/
privateintheight=fontsize+12;
/**
*干扰线条数
*默认3条
*/
privateintdisturbline=3;
publicVCodeGenerator(){}
/**
*指定验证码长度
*@paramvcodeLen验证码长度
*/
publicVCodeGenerator(intvcodeLen){
this.vcodeLen=vcodeLen;
this.width=(fontsize+1)*vcodeLen+10;
}
/**
*生成验证码图片
*@paramvcode要画的验证码
*@paramdrawline是否画干扰线
*@return
*/
publicBufferedImagegeneratorVCodeImage(Stringvcode,
booleandrawline){
//创建验证码图片
BufferedImagevcodeImage=newBufferedImage(width,height,
BufferedImage.TYPE_INT_RGB);
Graphicsg=vcodeImage.getGraphics();
//填充背景色
g.setColor(newColor(246,240,250));
g.fillRect(0,0,width,height);
if(drawline){
drawDisturbLine(g);
}
//用于生成伪随机数
Randomran=newRandom();
//在图片上画验证码
for(inti=0;i //设置字体 g.setFont(newFont(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)],fontsize)); //随机生成颜色 g.setColor(getRandomColor()); //画验证码 g.drawString(vcode.charAt(i)+"",i*fontsize+10,fontsize+5); } //释放此图形的上下文以及它使用的所有系统资源 g.dispose(); returnvcodeImage; } /** *获得旋转字体的验证码图片 *@paramvcode *@paramdrawline是否画干扰线 *@return */ publicBufferedImagegeneratorRotateVCodeImage(Stringvcode, booleandrawline){ //创建验证码图片 BufferedImagerotateVcodeImage=newBufferedImage(width,height, BufferedImage.TYPE_INT_RGB); Graphics2Dg2d=rotateVcodeImage.createGraphics(); //填充背景色 g2d.setColor(newColor(246,240,250)); g2d.fillRect(0,0,width,height); if(drawline){ drawDisturbLine(g2d); } //在图片上画验证码 for(inti=0;i BufferedImagerotateImage=getRotateImage(vcode.charAt(i)); g2d.drawImage(rotateImage,null,(int)(this.height*0.7)*i,0); } g2d.dispose(); returnrotateVcodeImage; } /** *生成验证码 *@return验证码 */ publicStringgeneratorVCode(){ intlen=code.length; Randomran=newRandom(); StringBuffersb=newStringBuffer(); for(inti=0;i intindex=ran.nextInt(len); sb.append(code[index]); } returnsb.toString(); } /** *为验证码图片画一些干扰线 *@paramg */ privatevoiddrawDisturbLine(Graphicsg){ Randomran=newRandom(); for(inti=0;i intx1=ran.nextInt(width); inty1=ran.nextInt(height); intx2=ran.nextInt(width); inty2=ran.nextInt(height); g.setColor(getRandomColor()); //画干扰线 g.drawLine(x1,y1,x2,y2); } } /** *获取一张旋转的图片 *@paramc要画的字符 *@return */ privateBufferedImagegetRotateImage(charc){ BufferedImagerotateImage=newBufferedImage(height,height, BufferedImage.TYPE_INT_ARGB); Graphics2Dg2d=rotateImage.createGraphics(); //设置透明度为0 g2d.setColor(newColor(255,255,255,0)); g2d.fillRect(0,0,height,height); Randomran=newRandom(); g2d.setFont(newFont(fontNames[ran.nextInt(fontNames.length)], fontStyles[ran.nextInt(fontStyles.length)],fontsize)); g2d.setColor(getRandomColor()); doubletheta=getTheta(); //旋转图片 g2d.rotate(theta,height/2,height/2); g2d.drawString(Character.toString(c),(height-fontsize)/2, fontsize+5); g2d.dispose(); returnrotateImage; } /** *@return返回一个随机颜色 */ privateColorgetRandomColor(){ Randomran=newRandom(); returnnewColor(ran.nextInt(220),ran.nextInt(220), ran.nextInt(220)); } /** *@return角度 */ privatedoublegetTheta(){ return((int)(Math.random()*1000)%2==0? -1: 1)*Math.random(); } /** *@return验证码字符个数 */ publicintgetVcodeLen(){ returnvcodeLen; } /** *设置验证码字符个数 *@paramvcodeLen */ publicvoidsetVcodeLen(intvcodeLen){ this.width=(fontsize+3)*vcodeLen+10; this.vcodeLen=vcodeLen; } /** *@return字体大小 */ publicintgetFontsize(){ returnfontsize; } /** *设置字体大小 *@paramfontsize */ publicvoidsetFontsize(intfontsize){ this.widt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 验证 生成器