java生成图片验证码的示例代码
给大家分享一款java生成验证码的源码,可设置随机字符串,去掉了几个容易混淆的字符,还可以设置验证码位数,比如4位,6位。当然也可以根据前台验证码的位置大小,设置验证码图片的大小。下边是源码分享,直接看吧,很简单!
创建servlet类
importjava.io.IOException;
importjavax.servlet.Servlet;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importcom.sunjs.utils.CaptchaUtils;
publicclassAuthImageextendsHttpServletimplementsServlet{
staticfinallongserialVersionUID=1L;
publicvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
response.setContentType("image/jpeg");
//生成随机字串
Stringcaptcha=CaptchaUtils.generateCaptcha(4);
//存入会话session
HttpSessionsession=request.getSession(true);
session.setAttribute("rand",captcha.toLowerCase());
//生成图片
intw=200,h=80;
CaptchaUtils.outputImage(w,h,response.getOutputStream(),captcha);
}
}
创建工具类
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.RenderingHints;
importjava.awt.geom.AffineTransform;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.OutputStream;
importjava.util.Arrays;
importjava.util.Random;
importjavax.imageio.ImageIO;
/**
*验证码
*
*@authorsun
*@date2016年8月23日上午10:12:10
*/
publicclassCaptchaUtils{
//使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,L,o几个容易混淆的字符
publicstaticfinalStringCAPTCHA_CHARS="23456789ABCDEFGHJKMNPQRSTUVWXYZ";
privatestaticRandomrandom=newRandom();
/**
*使用系统默认字符源生成验证码
*
*@paramcaptchaSize
*验证码长度
*@return
*/
publicstaticStringgenerateCaptcha(intcaptchaSize){
returngenerateCaptcha(captchaSize,CAPTCHA_CHARS);
}
/**
*使用指定源生成验证码
*
*@paramcaptchaSize
*验证码长度
*@paramsources
*验证码字符源
*@return
*/
publicstaticStringgenerateCaptcha(intcaptchaSize,Stringsources){
if(sources==null||sources.length()==0){
sources=CAPTCHA_CHARS;
}
intcodesLen=sources.length();
Randomrand=newRandom(DateUtils.getCurrentTimeMillis());
StringBuildercaptcha=newStringBuilder(captchaSize);
for(inti=0;i255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc);
intg=fc+random.nextInt(bc-fc);
intb=fc+random.nextInt(bc-fc);
returnnewColor(r,g,b);
}
privatestaticintgetRandomIntColor(){
int[]rgb=getRandomRgb();
intcolor=0;
for(intc:rgb){
color=color<<8;
color=color|c;
}
returncolor;
}
privatestaticint[]getRandomRgb(){
int[]rgb=newint[3];
for(inti=0;i<3;i++){
rgb[i]=random.nextInt(255);
}
returnrgb;
}
privatestaticvoidshear(Graphicsg,intw1,inth1,Colorcolor){
shearX(g,w1,h1,color);
shearY(g,w1,h1,color);
}
privatestaticvoidshearX(Graphicsg,intw1,inth1,Colorcolor){
intperiod=random.nextInt(2);
booleanborderGap=true;
intframes=1;
intphase=random.nextInt(2);
for(inti=0;i>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.copyArea(0,i,w1,1,(int)d,0);
if(borderGap){
g.setColor(color);
g.drawLine((int)d,i,0,i);
g.drawLine((int)d+w1,i,w1,i);
}
}
}
privatestaticvoidshearY(Graphicsg,intw1,inth1,Colorcolor){
intperiod=random.nextInt(40)+10;//50;
booleanborderGap=true;
intframes=20;
intphase=7;
for(inti=0;i>1)
*Math.sin((double)i/(double)period
+(6.2831853071795862D*(double)phase)
/(double)frames);
g.copyArea(i,0,1,h1,0,(int)d);
if(borderGap){
g.setColor(color);
g.drawLine(i,(int)d,i,0);
g.drawLine(i,(int)d+h1,i,h1);
}
}
}
publicstaticvoidmain(String[]args)throwsIOException{
//Filedir=newFile("D:/verifies");
Filedir=newFile("/Users/sun/Documents/captcha");
intw=200,h=80;
for(inti=0;i<50;i++){
Stringcaptcha=generateCaptcha(4);
Filefile=newFile(dir,captcha+".jpg");
outputImage(w,h,file,captcha);
}
}
}
配置web.xml
AuthImage com.sunjs.controller.AuthImage AuthImage /authImage
当然也可以增加session的过期时间,不写的话,会有默认的失效时间
30
配置完成,直接启动项目访问,访问路径你们懂!!!
以上就是java生成图片验证码的示例代码的详细内容,更多关于java生成图片验证码的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。