package sanitation.Util;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.awt.*;import java.awt.image.*;import java.util.*;import javax.imageio.*; public class AuthImg extends HttpServlet{ /** * 验证码图片的处理*/ private static final long serialVersionUID = 1L; private Font mFont = new Font("Arial Black", Font.PLAIN, 16); // 根据指定名称、样式和磅值大小,创建一个新 Font public void init() throws ServletException { super.init(); } //随机产生指定区间颜色 Color getRandColor(int fc,int bc) { Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return new Color(r,g,b); } public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); System.out.println("i产生的"); int width=80, height=18; /** * 构造一个类型为预定义图像类型之一的 BufferedImage,三个参数分别为:a.所创建图像的宽度 *b.所创建图像的高度 c.所创建图像的类型 * */ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //创建图形上下文 Graphics g = image.getGraphics(); Random random = new Random(); //将此图形上下文的当前颜色设置为指定颜色 g.setColor(getRandColor(200,250)); //填充指定的矩形,fillRect(int x, int y, int width, int height),该矩形左边缘和右边缘分别位于 x 和 x + width - 1。上边缘和下边缘分别位于 y 和 y + height - 1,宽度为width,高度为height g.fillRect(1, 1, width-1, height-1); g.setColor(new Color(102,102,102)); //绘制指定矩形的边框。 g.drawRect(0, 0, width-1, height-1); //将此图形上下文的字体设置为指定字体 g.setFont(mFont); g.setColor(getRandColor(160,200)); for (int i=0;i<155;i++) { int x = random.nextInt(width - 1); int y = random.nextInt(height - 1); int xl = random.nextInt(6) + 1; int yl = random.nextInt(12) + 1; //drawLine(int x1, int y1, int x2, int y2)在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。 g.drawLine(x,y,x + xl,y + yl); } for (int i = 0;i < 70;i++) { int x = random.nextInt(width - 1); int y = random.nextInt(height - 1); int xl = random.nextInt(12) + 1; int yl = random.nextInt(6) + 1; g.drawLine(x,y,x - xl,y - yl); } /** * 以下是产生4位随机数(包括大写字母,小写字母,数字) * */ String sRand=""; for (int i=0;i<4;i++) { //返回一个String的字符 String tmp = getRandomChar(); //下面是为了不让0与o产生歧义,而进行处理 if(tmp.equals("O") || tmp.equals("o")){ tmp="A"; } if(tmp=="0"){ tmp="B"; } sRand += tmp; g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); //使用此图形上下文的当前字体和颜色绘制由指定 string 给定的文本。最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处 g.drawString(tmp,15*i+10,15); } System.out.println("产生的验证码"+sRand.toUpperCase()); HttpSession session = request.getSession(true); session.setAttribute("rand",sRand); //释放此图形的上下文以及它使用的所有系统资源 g.dispose(); /** * 到此图形创建结束,接着借助ImageIO.write(RenderedImage im, String formatName, OutputStream output)将一个图像写入 OutputStream * */ ImageIO.write(image, "JPEG", response.getOutputStream()); } /** * 获取相应的大写字母,小写字母或是数字 * */ private String getRandomChar() { //产生小于2的随机数并进行四舍五入 int rand = (int)Math.round(Math.random() * 2); long itmp = 0; char ctmp = '\u0000'; switch (rand) { case 1: //产生65-90的Ascii itmp = Math.round(Math.random() * 25 + 65); //强制转化为char,获取相应的大写字母 ctmp = (char)itmp; //下面有了return就可以省略break return String.valueOf(ctmp); case 2: //获取97-122的Ascii,获取小写字母 itmp = Math.round(Math.random() * 25 + 97); ctmp = (char)itmp; return String.valueOf(ctmp); default : //获取数字 itmp = Math.round(Math.random() * 9); return String.valueOf(itmp); } }}}