java生成验证码图片的方法
本文实例为大家分享了java生成验证码图片的具体代码,供大家参考,具体内容如下
示例一:
importorg.apache.commons.codec.binary.Base64;
importorg.apache.commons.lang.RandomStringUtils;
importorg.apache.commons.lang.StringUtils;
importjavax.imageio.ImageIO;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpSession;
importjava.awt.*;
importjava.awt.image.BufferedImage;
importjava.io.ByteArrayOutputStream;
importjava.util.Random;
publicclassRandomVerifyCode{
privatestaticfinalStringRANDOM_NUM="23456789";
privatestaticfinalStringRANDOM_LETTER="ABCDEFGHJKMNPQRSTUVWXYZ";
privatestaticfinalStringRANDOM_LETTER_SMALL="abcdefghjkmnpqrstuvwxyz";
privatestaticfinalStringRANDOM_STRING=RANDOM_NUM+RANDOM_LETTER+RANDOM_LETTER_SMALL;
privateintwidth=95;//图片宽
privateintheight=25;//图片高
privateintlineSize=40;//干扰线数量
privateintstringNum=4;//随机产生字符数量
privateRandomrandom=newRandom();
/**
*获得字体
*/
privateFontgetFont(){
returnnewFont("Fixedsys",Font.CENTER_BASELINE,18);
}
/**
*获得颜色
*/
privateColorgetRandColor(intfc,intbc){
if(fc>255)
fc=255;
if(bc>255)
bc=255;
intr=fc+random.nextInt(bc-fc-16);
intg=fc+random.nextInt(bc-fc-14);
intb=fc+random.nextInt(bc-fc-18);
returnnewColor(r,g,b);
}
/**
*生成随机图片(BASE64格式)
*/
publicStringgetRandcode(HttpServletRequestrequest,StringBufferimgBuffer){
//BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类
BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphicsg=image.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作
g.fillRect(0,0,width,height);//图片大小
g.setFont(newFont("TimesNewRoman",Font.ROMAN_BASELINE,18));//字体大小
g.setColor(getRandColor(110,133));//字体颜色
//绘制干扰线
for(inti=0;i<=lineSize;i++){
drowLine(g);
}
//绘制随机字符
StringrandomString="";
for(inti=1;i<=stringNum;i++){
randomString=drowString(g,randomString,i);
}
g.dispose();
ByteArrayOutputStreamout=newByteArrayOutputStream();
Stringencode=null;
try{
//将内存中的图片通过流动形式输出到客户端
ImageIO.write(image,"JPEG",out);
encode="data:image/jpeg;base64,"+Base64.encodeBase64String(out.toByteArray());
out.close();
}catch(Exceptione){
LogHelper.error("登陆控制","验证码生成","将内存中生成的验证码图片输出为BASE64格式编码失败!");
}
imgBuffer.append(encode);
returnrandomString;
}
/**
*绘制字符串
*/
privateStringdrowString(Graphicsg,StringrandomString,inti){
g.setFont(getFont());
g.setColor(newColor(random.nextInt(101),random.nextInt(111),random
.nextInt(121)));
Stringrand=String.valueOf(getRandomString(random.nextInt(RANDOM_STRING.length())));
randomString+=rand;
g.translate(random.nextInt(3),random.nextInt(3));
g.drawString(rand,13*i,16);
returnrandomString;
}
/**
*绘制干扰线
*/
privatevoiddrowLine(Graphicsg){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(13);
intyl=random.nextInt(15);
g.drawLine(x,y,x+xl,y+yl);
}
/**
*获取随机的字符
*/
publicStringgetRandomString(intnum){
returnString.valueOf(RANDOM_STRING.charAt(num));
}
publicstaticStringrandomString(intlength){
returnRandomStringUtils.random(length,RANDOM_STRING.toCharArray());
}
}
示例二:
importjavax.imageio.ImageIO;
importjava.awt.*;
importjava.awt.image.BufferedImage;
importjava.awt.image.RenderedImage;
importjava.io.FileOutputStream;
importjava.io.OutputStream;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.Random;
publicclassCodeUtil{
privatestaticintwidth=90;//定义图片的width90
privatestaticintheight=20;//定义图片的height20
privatestaticintcodeCount=4;//定义图片上显示验证码的个数
privatestaticintxx=15;
privatestaticintfontHeight=18;
privatestaticintcodeY=16;
privatestaticchar[]codeSequence={'A','B','C','D','E','F','G','H','J','K','M','N','P','Q','R',
'S','T','U','V','W','X','Y','Z','2','3','4','5','6','7','8','9'};
publicstaticMapcodePicMap=newHashMap<>();
/**
*生成一个map集合
*code为生成的验证码
*codePic为生成的验证码BufferedImage对象
*
*@return
*/
publicstaticMapgenerateCodeAndPic(){
//定义图像buffer
BufferedImagebuffImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//Graphics2Dgd=buffImg.createGraphics();
//Graphics2Dgd=(Graphics2D)buffImg.getGraphics();
Graphicsgd=buffImg.getGraphics();
//创建一个随机数生成器类
Randomrandom=newRandom();
//将图像填充为蓝色
//gd.setColor(Color.WHITE);
gd.setColor(newColor(232,240,254));
gd.fillRect(0,0,width,height);
//创建字体,字体的大小应该根据图片的高度来定。
Fontfont=newFont("Fixedsys",Font.BOLD,fontHeight);
//设置字体。
gd.setFont(font);
//画边框。
gd.setColor(Color.BLACK);
gd.drawRect(0,0,width-1,height-1);
//随机产生40条干扰线,使图象中的认证码不易被其它程序探测到。
gd.setColor(Color.BLACK);
for(inti=0;i<10;i++){
intx=random.nextInt(width);
inty=random.nextInt(height);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
gd.drawLine(x,y,x+xl,y+yl);
}
//randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
StringBufferrandomCode=newStringBuffer();
intred=0,green=0,blue=0;
//随机产生codeCount数字的验证码。
for(inti=0;imap=newHashMap<>();
//存放验证码
map.put("code",randomCode);
//存放生成的验证码BufferedImage对象
map.put("codePic",buffImg);
returnmap;
}
publicstaticvoidmain(String[]args)throwsException{
//创建文件输出流对象
OutputStreamout=newFileOutputStream("D://img/"+System.currentTimeMillis()+".jpg");
Mapmap=CodeUtil.generateCodeAndPic();
ImageIO.write((RenderedImage)map.get("codePic"),"jpeg",out);
System.out.println("验证码的值为:"+map.get("code"));
}
}    
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。
