java随机产生验证码

java随机产生验证码

ID:6031950

大小:39.50 KB

页数:5页

时间:2017-12-31

java随机产生验证码_第1页
java随机产生验证码_第2页
java随机产生验证码_第3页
java随机产生验证码_第4页
java随机产生验证码_第5页
资源描述:

《java随机产生验证码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、packagecom.cstp.lottery.image;/***@authorsky9413@sina.com**创建图形验证码*/importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics2D;importjava.awt.image.BufferedImage;importjava.io.File;importjava.util.Date;importjava.util.LinkedList;importjava.util.Random;importjavax.imageio.ImageIO;i

2、mportjavax.servlet.http.HttpServletResponse;importcom.sun.image.codec.jpeg.JPEGCodec;importcom.sun.image.codec.jpeg.JPEGImageEncoder;publicclassRandomImage{publicStringsRand="";publicRandomImage(){}publicColorgetRandColor(intfc,intbc){//给定范围获得随机颜色Randomrandom=newRandom();if(fc>255)fc=255;

3、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);}/***通过文件创建图像*格式为jpg类型*/publicvoidcreatImage(StringfileName,Stringcontent){//在内存中创建图象intwidth=60,height=20;BufferedImageimage=newBufferedImage(width,height,Buff

4、eredImage.TYPE_INT_RGB);//获取图形上下文Graphics2Dg=image.createGraphics();//Graphicsg=image.getGraphics();//生成随机类Randomrandom=newRandom();//设定背景色g.setColor(getRandColor(200,250));g.fillRect(0,0,width,height);//设定字体g.setFont(newFont("TimesNewRoman",Font.PLAIN,18));g.setColor(Color.black);//黑色文字g

5、.drawString(content,10,15);g.dispose();try{Filef=newFile(fileName);if(!f.exists()){f.createNewFile();}else{Thread.sleep(200);f.delete();f.createNewFile();}ImageIO.write(image,"jpg",f);}catch(Exceptione){e.printStackTrace();}}/***创建图像*格式为jpg类型**@paramcontent-String图片输出内容*@returnjava.awt.im

6、age.BufferedImage*@since2005-7-19*/publicBufferedImagegetBufferedImage(Stringcontent){intwidth=60,height=20;returngetBufferedImage(content,width,height);}/***创建图像*格式为jpg类型**@paramcontent-String图片输出内容*@paramwidth-int图片宽度*@paramheight-int图片高度*@returnjava.awt.image.BufferedImage*@since2005-7

7、-19*/publicBufferedImagegetBufferedImage(Stringcontent,intwidth,intheight){//在内存中创建图象BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//获取图形上下文Graphics2Dg=image.createGraphics();//Graphicsg=image.getGraphics();//生成随机类Randomrandom=newRando

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。