资源描述:
《java验证图片》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、packagecn.td;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphics;importjava.awt.Graphics2D;importjava.awt.image.BufferedImage;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Random;importjavax.imageio.ImageIO;importjavax.servlet.Servle
2、tException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;//输出一张随机图片publicclassResponseDemo2extendsHttpServlet{publicstaticfinalintWIDTH=120;publicstaticfinalintHEIGHT=25;publicvoiddoGet(Ht
3、tpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{BufferedImageimage=newBufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);Graphicsg=image.getGraphics();//1、设置背景颜色setBackGround(g);//2、设置边框serBorder(g);//3、画干扰线drawRandomLine(g);
4、//画躁点drawRandomPoint(g);//4、写随机数drawRandomNum((Graphics2D)g);//控制浏览器不要缓存response.setDateHeader("expries",-1);response.setHeader("Cache-Control","no-cache");response.setHeader("Prama","no-cache");//5、图形写给浏览器response.setContentType("image/jpeg");ImageIO.write(image,"jpg
5、",response.getOutputStream());}//画躁点privatevoiddrawRandomPoint(Graphicsg){//TODOAuto-generatedmethodstubg.setColor(Color.ORANGE);for(inti=0;i<20;i++){intx1=newRandom().nextInt(WIDTH);inty1=newRandom().nextInt(HEIGHT);g.drawLine(x1,y1,x1+1,y1+1);}g.setColor(Color.YELLO
6、W);for(inti=0;i<20;i++){intx1=newRandom().nextInt(WIDTH);inty1=newRandom().nextInt(HEIGHT);g.drawLine(x1,y1,x1+1,y1+1);}}//写入字符privatevoiddrawRandomNum(Graphics2Dg){//TODOAuto-generatedmethodstubg.setColor(Color.RED);g.setFont(newFont("宋体",Font.BOLD,20));Stringdata="
7、u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca
8、1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u5