验证码图片生成工具类——Captcha.java
验证码图片生成工具,使用JAVA生成的图片验证码,调用getRandcode方法获取图片验证码,以流的方式传输到前端页面。
源码如下:(点击下载 Captcha.java)
1 import java.awt.Color; 2 import java.awt.Font; 3 import java.awt.Graphics; 4 import java.awt.image.BufferedImage; 5 import java.util.Random; 6 import javax.imageio.ImageIO; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import javax.servlet.http.HttpSession; 10 11 /** 12 * 验证码图片生成工具 13 * 使用JAVA生成的图片验证码,调用getRandcode方法获取图片验证码,以流的方式传输到前端页面。 14 * 15 * 16 * 作者: zhoubang 17 * 日期:2015年8月7日 上午10:41:05 18 */ 19 public class Captcha { 20 21 public static final String RANDOMCODEKEY = "sess_captcha";// 放到session中的key 22 private Random random = new Random(); 23 private String randString = "0123456789";// 随机产生的字符串 24 25 private int width = 80;// 图片宽 26 private int height = 26;// 图片高 27 private int lineSize = 40;// 干扰线数量 28 private int stringNum = 4;// 随机产生字符数量 29 30 /* 31 * 获得字体 32 */ 33 private Font getFont() { 34 return new Font("Fixedsys", Font.CENTER_BASELINE, 18); 35 } 36 37 /* 38 * 获得颜色 39 */ 40 private Color getRandColor(int fc, int bc) { 41 if (fc > 255) 42 fc = 255; 43 if (bc > 255) 44 bc = 255; 45 int r = fc + random.nextInt(bc - fc - 16); 46 int g = fc + random.nextInt(bc - fc - 14); 47 int b = fc + random.nextInt(bc - fc - 18); 48 return new Color(r, g, b); 49 } 50 51 /* 52 * 绘制字符串 53 */ 54 private String drowString(Graphics g, String randomString, int i) { 55 g.setFont(getFont()); 56 g.setColor(new Color(random.nextInt(101), random.nextInt(111), random.nextInt(121))); 57 String rand = String.valueOf(getRandomString(random.nextInt(randString.length()))); 58 randomString += rand; 59 g.translate(random.nextInt(3), random.nextInt(3)); 60 g.drawString(rand, 13 * i, 16); 61 return randomString; 62 } 63 64 /* 65 * 绘制干扰线 66 */ 67 private void drowLine(Graphics g) { 68 int x = random.nextInt(width); 69 int y = random.nextInt(height); 70 int xl = random.nextInt(13); 71 int yl = random.nextInt(15); 72 g.drawLine(x, y, x + xl, y + yl); 73 } 74 75 /* 76 * 获取随机的字符 77 */ 78 public String getRandomString(int num) { 79 return String.valueOf(randString.charAt(num)); 80 } 81 82 83 /** 84 * 生成随机图片 85 */ 86 public void getRandcode(HttpServletRequest request, HttpServletResponse response) { 87 HttpSession session = request.getSession(); 88 // BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类 89 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); 90 Graphics g = image.getGraphics();// 产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作 91 g.fillRect(0, 0, width, height); 92 g.setFont(new Font("Times New Roman", Font.ROMAN_BASELINE, 18)); 93 g.setColor(getRandColor(110, 133)); 94 // 绘制干扰线 95 for (int i = 0; i <= lineSize; i++) { 96 drowLine(g); 97 } 98 // 绘制随机字符 99 String randomString = ""; 100 for (int i = 1; i <= stringNum; i++) { 101 randomString = drowString(g, randomString, i); 102 } 103 session.removeAttribute(RANDOMCODEKEY); 104 session.setAttribute(RANDOMCODEKEY, randomString); 105 g.dispose(); 106 try { 107 ImageIO.write(image, "JPEG", response.getOutputStream());// 将内存中的图片通过流动形式输出到客户端 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 }
简单使用方法(可自行设计,此方法为servlet调用):
1 import java.io.IOException; 2 import javax.servlet.ServletException; 3 import javax.servlet.annotation.WebServlet; 4 import javax.servlet.http.HttpServlet; 5 import javax.servlet.http.HttpServletRequest; 6 import javax.servlet.http.HttpServletResponse; 7 8 @WebServlet(description = "图片验证码生成", urlPatterns = { "/ImageServlet" }) 9 public class ImageServlet extends HttpServlet { 10 private static final long serialVersionUID = 1L; 11 12 public ImageServlet() { 13 super(); 14 } 15 16 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 17 response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片 18 response.setHeader("Pragma", "No-cache");// 设置响应头信息,告诉浏览器不要缓存此内容 19 response.setHeader("Cache-Control", "no-cache"); 20 response.setDateHeader("Expire", 0); 21 Captcha randomValidateCode = new Captcha(); 22 try { 23 randomValidateCode.getRandcode(request, response);// 输出图片方法 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 } 28 29 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 30 doGet(request, response); 31 } 32 }
版权声明:本文为zhoubang521原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。