图形校验码

 
  
  1. public void verifyCode() throws IOException{
  2. HttpServletRequest request = ServletActionContext.getRequest();
  3. HttpServletResponse response = ServletActionContext.getResponse();
  4. ValidatorCode codeUtil = ValidatorCodeUtil.getCode();
  5. System.out.println("code="+codeUtil.getCode());
  6. request.getSession().setAttribute("code", codeUtil.getCode());
  7. // 禁止图像缓存。
  8. response.setHeader("Pragma", "no-cache");
  9. response.setHeader("Cache-Control", "no-cache");
  10. response.setDateHeader("Expires", 0);
  11. response.setContentType("image/jpeg");
  12. ServletOutputStream sos = null;
  13. try {
  14. // 将图像输出到Servlet输出流中。
  15. sos = response.getOutputStream();
  16. JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
  17. encoder.encode(codeUtil.getImage());
  18. sos.flush();
  19. sos.close();
  20. } catch (Exception e) {
  21. } finally {
  22. if (null != sos) {
  23. try {
  24. sos.close();
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }
  30. }
 
   
  1. package com.w658.util;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.util.Random;
  7. public class ValidatorCodeUtil
  8. {
  9. public static ValidatorCode getCode()
  10. {
  11. int width = 80;
  12. int height = 30;
  13. BufferedImage buffImg = new BufferedImage(width, height, 1);
  14. Graphics2D g = buffImg.createGraphics();
  15. // 创建一个随机数生成器类
  16. Random random = new Random();
  17. // 将图像填充为白色
  18. g.setColor(Color.WHITE);
  19. g.fillRect(0, 0, width, height);
  20. // 创建字体,字体的大小
  21. Font font = new Font("微软雅黑", 2, 28);
  22. // 设置字体。
  23. g.setFont(font);
  24. // 画边框。
  25. g.setColor(Color.LIGHT_GRAY);
  26. g.drawRect(0, 0, width - 1, height - 1);
  27. // 随机产生10条干扰线,使图象中的认证码不易被其它程序探测到。
  28. //g.setColor(Color.lightGray);
  29. g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
  30. for (int i = 0; i < 10; i++) {
  31. int x = random.nextInt(width);
  32. int y = random.nextInt(height);
  33. int xl = random.nextInt(12);
  34. int yl = random.nextInt(12);
  35. g.drawLine(x, y, x + xl, y + yl);
  36. }
  37. StringBuffer randomCode = new StringBuffer();
  38. int length = 4;
  39. String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  40. int size = base.length();
  41. // 随机产生codeCount数字的验证码。
  42. for (int i = 0; i < length; i++)
  43. {
  44. int start = random.nextInt(size);
  45. String strRand = base.substring(start, start + 1);
  46. // 用随机产生的颜色将验证码绘制到图像中。
  47. g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
  48. g.drawString(strRand, 15 * i + 6, 24);
  49. // 将产生的四个随机数组合在一起。
  50. randomCode.append(strRand);
  51. }
  52. //清空缓存
  53. g.dispose();
  54. ValidatorCode code = new ValidatorCode();
  55. code.image = buffImg;
  56. code.code = randomCode.toString();
  57. return code;
  58. }
  59. static Color getRandColor(int fc, int bc)
  60. {
  61. Random random = new Random();
  62. if (fc > 255)
  63. fc = 255;
  64. if (bc > 255)
  65. bc = 255;
  66. int r = fc + random.nextInt(bc - fc);
  67. int g = fc + random.nextInt(bc - fc);
  68. int b = fc + random.nextInt(bc - fc);
  69. return new Color(r, g, b);
  70. }
  71. public static class ValidatorCode
  72. {
  73. private BufferedImage image;
  74. private String code;
  75. public BufferedImage getImage()
  76. {
  77. return this.image;
  78. }
  79. public String getCode()
  80. {
  81. return this.code;
  82. }
  83. }
  84. }
 
    
  1. package com.w658.util;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics2D;
  5. import java.awt.image.BufferedImage;
  6. import java.util.Random;
  7. public class ValidatorCodeUtil
  8. {
  9. public static ValidatorCode getCode()
  10. {
  11. int width = 80;
  12. int height = 30;
  13. BufferedImage buffImg = new BufferedImage(width, height, 1);
  14. Graphics2D g = buffImg.createGraphics();
  15. // 创建一个随机数生成器类
  16. Random random = new Random();
  17. // 将图像填充为白色
  18. g.setColor(Color.WHITE);
  19. g.fillRect(0, 0, width, height);
  20. // 创建字体,字体的大小
  21. Font font = new Font("微软雅黑", 2, 28);
  22. // 设置字体。
  23. g.setFont(font);
  24. // 画边框。
  25. g.setColor(Color.LIGHT_GRAY);
  26. g.drawRect(0, 0, width - 1, height - 1);
  27. // 随机产生10条干扰线,使图象中的认证码不易被其它程序探测到。
  28. //g.setColor(Color.lightGray);
  29. g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
  30. for (int i = 0; i < 10; i++) {
  31. int x = random.nextInt(width);
  32. int y = random.nextInt(height);
  33. int xl = random.nextInt(12);
  34. int yl = random.nextInt(12);
  35. g.drawLine(x, y, x + xl, y + yl);
  36. }
  37. StringBuffer randomCode = new StringBuffer();
  38. int length = 4;
  39. String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  40. int size = base.length();
  41. // 随机产生codeCount数字的验证码。
  42. for (int i = 0; i < length; i++)
  43. {
  44. int start = random.nextInt(size);
  45. String strRand = base.substring(start, start + 1);
  46. // 用随机产生的颜色将验证码绘制到图像中。
  47. g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
  48. g.drawString(strRand, 15 * i + 6, 24);
  49. // 将产生的四个随机数组合在一起。
  50. randomCode.append(strRand);
  51. }
  52. //清空缓存
  53. g.dispose();
  54. ValidatorCode code = new ValidatorCode();
  55. code.image = buffImg;
  56. code.code = randomCode.toString();
  57. return code;
  58. }
  59. static Color getRandColor(int fc, int bc)
  60. {
  61. Random random = new Random();
  62. if (fc > 255)
  63. fc = 255;
  64. if (bc > 255)
  65. bc = 255;
  66. int r = fc + random.nextInt(bc - fc);
  67. int g = fc + random.nextInt(bc - fc);
  68. int b = fc + random.nextInt(bc - fc);
  69. return new Color(r, g, b);
  70. }
  71. public static class ValidatorCode
  72. {
  73. private BufferedImage image;
  74. private String code;
  75. public BufferedImage getImage()
  76. {
  77. return this.image;
  78. }
  79. public String getCode()
  80. {
  81. return this.code;
  82. }
  83. }
  84. }





转载于:https://www.cnblogs.com/wx491592452/p/95494cde582487eef6cce21b817efdcc.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值