Java代码随机生成图片验证码

[java] view plain copy
 
  1. package com.rchm.util.images;  
  2.   
  3.   
  4. import java.awt.Color;  
  5.   
  6.   
  7. import java.awt.Font;  
  8. import java.awt.Graphics2D;  
  9. import java.awt.image.BufferedImage;  
  10. import java.io.FileOutputStream;  
  11. import java.io.IOException;  
  12. import java.io.OutputStream;  
  13. import java.util.Random;  
  14.   
  15.   
  16. import javax.imageio.ImageIO;  
  17.   
  18.   
  19. /** 
  20.  * 验证码生成器 
  21.  */  
  22. public class ValidateCode {  
  23.   
  24. // 图片的宽度。  
  25. private int width = 160;  
  26. // 图片的高度。  
  27. private int height = 40;  
  28. // 验证码字符个数  
  29. private int codeCount = 5;  
  30. // 验证码干扰线数  
  31. private int lineCount = 150;  
  32. // 验证码  
  33. private static String code = null;  
  34. // 验证码图片Buffer  
  35. private BufferedImage buffImg=null;  
  36.   
  37.   
  38. private char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L',   
  39. 'M', 'N',  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y',   
  40. 'Z',  '1', '2', '3', '4', '5', '6', '7', '8', '9' };  
  41.   
  42.   
  43. public  ValidateCode() {  
  44. this.createCode();  
  45. }  
  46.   
  47.   
  48. /** 
  49. *  
  50. * @param width 图片宽 
  51. * @param height 图片高 
  52. */  
  53. public  ValidateCode(int width,int height) {  
  54. this.width=width;  
  55. this.height=height;  
  56. this.createCode();  
  57. }  
  58.   
  59. /** 
  60. *  
  61. * @param width 图片宽 
  62. * @param height 图片高 
  63. * @param codeCount 字符个数 
  64. * @param lineCount 干扰线条数 
  65. */  
  66. public  ValidateCode(int width,int height,int codeCount,int lineCount) {  
  67. this.width=width;  
  68. this.height=height;  
  69. this.codeCount=codeCount;  
  70. this.lineCount=lineCount;  
  71. this.createCode();  
  72. }  
  73.   
  74. public void createCode() {  
  75. int x = 0,fontHeight=0,codeY=0;  
  76. int red = 0, green = 0, blue = 0;  
  77.   
  78. x = width / (codeCount +2);//每个字符的宽度  
  79. fontHeight = height - 2;//字体的高度  
  80. codeY = height - 4;  
  81.   
  82. // 图像buffer  
  83. buffImg = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  
  84. Graphics2D g = buffImg.createGraphics();  
  85. // 生成随机数  
  86. Random random = new Random();  
  87. // 将图像填充为白色  
  88. g.setColor(Color.WHITE);  
  89. g.fillRect(0, 0, width, height);  
  90. // 创建字体  
  91. ImgFontByte imgFont=new ImgFontByte();  
  92. Font font =imgFont.getFont(fontHeight);  
  93. g.setFont(font);  
  94.   
  95. for (int i = 0; i < lineCount; i++) {  
  96. int xs = random.nextInt(width);  
  97. int ys = random.nextInt(height);  
  98. int xe = xs+random.nextInt(width/8);  
  99. int ye = ys+random.nextInt(height/8);  
  100. red = random.nextInt(255);  
  101. green = random.nextInt(255);  
  102. blue = random.nextInt(255);  
  103. g.setColor(new Color(red, green, blue));  
  104. g.drawLine(xs, ys, xe, ye);  
  105. }  
  106.   
  107. // randomCode记录随机产生的验证码  
  108. StringBuffer randomCode = new StringBuffer();  
  109. // 随机产生codeCount个字符的验证码。  
  110. for (int i = 0; i < codeCount; i++) {  
  111. String strRand = String.valueOf(codeSequence[random.nextInt(codeSequence.length)]);  
  112. // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。  
  113. red = random.nextInt(255);  
  114. green = random.nextInt(255);  
  115. blue = random.nextInt(255);  
  116. g.setColor(new Color(red, green, blue));  
  117. g.drawString(strRand, (i + 1) * x, codeY);  
  118. // 将产生的四个随机数组合在一起。  
  119. randomCode.append(strRand);  
  120. }  
  121. // 将四位数字的验证码保存到Session中。  
  122. code = randomCode.toString();  
  123. }  
  124.   
  125. public void write(String path) throws IOException {  
  126. OutputStream sos = new FileOutputStream(path);  
  127. this.write(sos);  
  128. }  
  129.   
  130. public void write(OutputStream sos) throws IOException {  
  131. ImageIO.write(buffImg, "png", sos);  
  132. sos.close();  
  133. }  
  134.   
  135. public BufferedImage getBuffImg() {  
  136. return buffImg;  
  137. }  
  138.   
  139. public static String getCode() {  
  140. return code;  
  141. }  
  142.   
  143. }  



在 servlet 中使用该类:

[java] view plain copy
 
  1. package com.rchm.util.images;  
  2.   
  3.   
  4. import java.io.IOException;  
  5.   
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11. import javax.servlet.http.HttpSession;  
  12.   
  13.   
  14. public class ValidateCodeServlet extends HttpServlet {  
  15.   
  16. private static final long serialVersionUID = 1L;  
  17.   
  18.   
  19. protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {  
  20. response.setContentType("image/jpeg");  
  21. response.setHeader("Pragma", "no-cache");  
  22. response.setHeader("Cache-Control", "no-cache");  
  23. response.setDateHeader("Expires", 0);  
  24. ValidateCode vCode = new ValidateCode(100,30,4,100);  
  25. HttpSession session = request.getSession();  
  26. session.removeAttribute("validateCode");  
  27. vCode.write(response.getOutputStream());  
  28. session.setAttribute("validateCode", vCode.getCode());  
  29. vCode.write(response.getOutputStream());  
  30. }  
  31.   
  32. }  



在 web.xml配置Servlet访问路径:

[plain] view plain copy
 
 在CODE上查看代码片派生到我的代码片
    1. <servlet>  
    2. <servlet-name>validateCodeServlet</servlet-name>  
    3. <servlet-class>com.rchm.util.images.ValidateCodeServlet</servlet-class>  
    4. </servlet>   
    5. <servlet-mapping>  
    6. <servlet-name>validateCodeServlet</servlet-name>  
    7. <url-pattern>code.images</url-pattern>  
原文地址:https://www.cnblogs.com/but009/p/6542768.html