Advertisement
Guest User

Untitled

a guest
Mar 17th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. package com.szlabsun.wqimc.web.common.action;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Graphics;
  6. import java.awt.image.BufferedImage;
  7. import java.io.BufferedInputStream;
  8. import java.io.ByteArrayInputStream;
  9. import java.io.ByteArrayOutputStream;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.OutputStream;
  15. import java.net.URLEncoder;
  16. import java.util.Random;
  17.  
  18. import javax.imageio.ImageIO;
  19.  
  20. /**
  21. * User: L
  22. * Date: 18-1-16
  23. * Time: 上午9:26
  24. */
  25. public class ValidateCodeUtil { // 配置文件服务类
  26. private Integer width; // 验证码宽度
  27. private Integer height; // 验证码高度
  28. private char[] str = "123456789qwertyuipasdfghjklzxcvbnm".toCharArray(); // 验证码最近出现的字符,已去掉0和o
  29. private InputStream inputStream;
  30. private String validateCode;
  31.  
  32. /**
  33. * 创建随机函数随机出现四位验证码
  34. * @return String
  35. */
  36. private String generateCode(Integer length,Graphics g)
  37. {
  38. // 得到随机产生的验证码数字。
  39. StringBuilder code = new StringBuilder();
  40. Random random = new Random();
  41. for(int i = 0;i < length;i++)
  42. {
  43. char rand = str[random.nextInt(str.length - 1)];
  44. // 产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。
  45.  
  46. int red = random.nextInt(255);
  47. int green = random.nextInt(165);
  48. int blue = random.nextInt(1);
  49. // 用随机产生的颜色将验证码绘制到图像中。
  50. g.setColor(new Color(red, green, blue));
  51. g.drawString(String.valueOf(rand), (i+1)*(width - 4) / (length + 1), height - 7);
  52. // 将产生的四个随机数组合在一起。
  53. code.append(rand);
  54. }
  55. return code.toString();
  56. }
  57.  
  58. /**
  59. * 生成干扰点
  60. */
  61. private void generateSpot(Graphics graphics){
  62. Random random = new Random();
  63. for(int i=0;i<250;i++){
  64. int x = (int) (Math.random() * width);
  65. int y = (int) (Math.random() * height);
  66. graphics.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
  67. graphics.drawOval(x, y, 1, 0);
  68. }
  69. }
  70.  
  71. /**
  72. * 生成图
  73. * @param g
  74. * @return
  75. */
  76. private Graphics generateGraphics(Graphics g){
  77. // 将图形填充为白色
  78. g.setColor(Color.LIGHT_GRAY);
  79. g.fillRect(0, 0, width, height);
  80. // 创建字体,字体的大小应该根据图片的高度来定。
  81. g.setFont(new Font("Fixedsys", Font.PLAIN, height - 10));
  82. // 画边框。
  83. g.setColor(Color.WHITE);
  84. g.drawRect(0, 0, width - 1, height - 1);
  85. return g;
  86. }
  87. /**
  88. * 生成验证码
  89. * @return
  90. */
  91. public String validateCode() throws IOException
  92. {
  93.  
  94. if(width == null){
  95. width = 87;
  96. }
  97. if(height == null){
  98. height = 37;
  99. }
  100. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  101. //生成图
  102. Graphics g = generateGraphics(image.getGraphics());
  103. // 随机产生干扰点,使图象中的认证码不易被其它程序探测到。
  104. generateSpot(g);
  105. // randomCode用于保存随机产生的验证码,以便用户登录后进行验证。
  106. String randCode = generateCode(4, g);
  107. ByteArrayOutputStream sos = new ByteArrayOutputStream();
  108. ImageIO.write(image, "jpeg", sos);
  109. inputStream = new ByteArrayInputStream(sos.toByteArray());
  110. sos.close();
  111. return null;
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement