简单生成网页上验证码,本例使用Servlet写的简例
package com.againfly.demo; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CodeDemo extends HttpServlet { private int width = 160;// 图片的宽度。 private int height = 40;// 图片的高度。 private int codeCount = 4;// 验证码字符个数 private int lineCount = 20;// 验证码干扰线数 Random random = new Random(); @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/png"); response.setHeader("Pragma", "no-cache");//禁止图像缓存。 response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); String code = genCode(codeCount); ImageIO.write(creatImage(code), "png", response.getOutputStream()); } private BufferedImage creatImage(String code) { int fontWidth = width / codeCount;// 字体的宽度 int fontHeight = height - 5;// 字体的高度 int codeY = height - 8; // 图像buffer BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = buffImg.getGraphics(); g.setColor(getRandColor(200, 250)); // 设置背景色 g.fillRect(0, 0, width, height); // 设置干扰线 for (int i = 0; i < lineCount; i++) { int xs = random.nextInt(width); int ys = random.nextInt(height); int xe = xs + random.nextInt(width); int ye = ys + random.nextInt(height); g.setColor(getRandColor(1, 255)); g.drawLine(xs, ys, xe, ye); } // 添加噪点 float yawpRate = 0.01f;// 噪声率 int area = (int) (yawpRate * width * height); for (int i = 0; i < area; i++) { int x = random.nextInt(width); int y = random.nextInt(height); buffImg.setRGB(x, y, random.nextInt(255)); } for(int i = 0 ; i < code.length(); i++){ g.setColor(getRandColor(1, 255)); g.setFont(getFont(fontHeight)); g.drawString(code.charAt(i) + "", i * fontWidth + 3, codeY); } return buffImg; } // 得到随机字符 private String genCode(int n) { String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"; StringBuilder builder = new StringBuilder(); for (int i = 0; i < n; i++) { //验证码长度 builder.append((char) s.charAt(random.nextInt(s.length()))); } return builder.toString(); } //得到随机颜色 private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色 if (fc > 255) fc = 255; if (fc < 0) fc = 0; if (bc > 255) bc = 255; if (bc < 0) bc = 0; 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); } //随机字体 private Font getFont(int size) { Random random = new Random(); Font font[] = new Font[5]; font[0] = new Font("Ravie", Font.PLAIN, size); font[1] = new Font("Antique Olive Compact", Font.PLAIN, size); font[2] = new Font("Fixedsys", Font.PLAIN, size); font[3] = new Font("Wide Latin", Font.PLAIN, size); font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size); return font[random.nextInt(5)]; } }
显示效果如下:
最新评论