1979e4df293ca14e9eeae573b5d8e2656a946acd.svn-base 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package cn.com.goldenwater.dcproj.utils;
  2. import cn.com.goldenwater.dcproj.util.Base64Util;
  3. import javax.imageio.ImageIO;
  4. import java.awt.*;
  5. import java.awt.geom.AffineTransform;
  6. import java.awt.image.BufferedImage;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.util.Arrays;
  11. import java.util.Random;
  12. public class VerifyCodeUtils {
  13. //public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  14. public static final String VERIFY_CODES = "123456789";//方便录入只留数字
  15. //public static final String VERIFY_CODES = "111111111";//方便新疆做第三方测试只录入1111
  16. private static Random random = new Random();
  17. /**
  18. * 使用系统默认字符源生成验证码
  19. *
  20. * @param verifySize 验证码长度
  21. * @return
  22. */
  23. public static String generateVerifyCode(int verifySize) {
  24. return generateVerifyCode(verifySize, VERIFY_CODES);
  25. }
  26. /**
  27. * 使用指定源生成验证码
  28. *
  29. * @param verifySize 验证码长度
  30. * @param sources 验证码字符源
  31. * @return
  32. */
  33. public static String generateVerifyCode(int verifySize, String sources) {
  34. if (sources == null || sources.length() == 0) {
  35. sources = VERIFY_CODES;
  36. }
  37. int codesLen = sources.length();
  38. Random rand = new Random(System.currentTimeMillis());
  39. StringBuilder verifyCode = new StringBuilder(verifySize);
  40. for (int i = 0; i < verifySize; i++) {
  41. verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
  42. }
  43. return verifyCode.toString();
  44. }
  45. /**
  46. * 输出指定验证码图片流
  47. *
  48. * @param w
  49. * @param h
  50. * @param os
  51. * @param code
  52. * @throws IOException
  53. */
  54. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException {
  55. int verifySize = code.length();
  56. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  57. Random rand = new Random();
  58. Graphics2D g2 = image.createGraphics();
  59. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  60. Color[] colors = new Color[5];
  61. Color[] colorSpaces = new Color[]{Color.WHITE, Color.CYAN,
  62. Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
  63. Color.PINK, Color.YELLOW};
  64. float[] fractions = new float[colors.length];
  65. for (int i = 0; i < colors.length; i++) {
  66. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
  67. fractions[i] = rand.nextFloat();
  68. }
  69. Arrays.sort(fractions);
  70. g2.setColor(Color.GRAY);// 设置边框色
  71. g2.fillRect(0, 0, w, h);
  72. Color c = getRandColor(200, 250);
  73. g2.setColor(c);// 设置背景色
  74. g2.fillRect(0, 2, w, h - 4);
  75. //绘制干扰线
  76. Random random = new Random();
  77. g2.setColor(getRandColor(160, 200));// 设置线条的颜色
  78. for (int i = 0; i < 20; i++) {
  79. int x = random.nextInt(w - 1);
  80. int y = random.nextInt(h - 1);
  81. int xl = random.nextInt(6) + 1;
  82. int yl = random.nextInt(12) + 1;
  83. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
  84. }
  85. // 添加噪点
  86. float yawpRate = 0.05f;// 噪声率
  87. int area = (int) (yawpRate * w * h);
  88. for (int i = 0; i < area; i++) {
  89. int x = random.nextInt(w);
  90. int y = random.nextInt(h);
  91. int rgb = getRandomIntColor();
  92. image.setRGB(x, y, rgb);
  93. }
  94. shear(g2, w, h, c);// 使图片扭曲
  95. g2.setColor(getRandColor(100, 160));
  96. int fontSize = h - 4;
  97. Font font = new Font("Algerian", Font.ITALIC, fontSize);
  98. g2.setFont(font);
  99. char[] chars = code.toCharArray();
  100. for (int i = 0; i < verifySize; i++) {
  101. AffineTransform affine = new AffineTransform();
  102. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize / 2, h / 2);
  103. g2.setTransform(affine);
  104. g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + fontSize / 2 - 10);
  105. }
  106. g2.dispose();
  107. ImageIO.write(image, "jpg", os);
  108. }
  109. private static Color getRandColor(int fc, int bc) {
  110. if (fc > 255) {
  111. fc = 255;
  112. }
  113. if (bc > 255) {
  114. bc = 255;
  115. }
  116. int r = fc + random.nextInt(bc - fc);
  117. int g = fc + random.nextInt(bc - fc);
  118. int b = fc + random.nextInt(bc - fc);
  119. return new Color(r, g, b);
  120. }
  121. private static int getRandomIntColor() {
  122. int[] rgb = getRandomRgb();
  123. int color = 0;
  124. for (int c : rgb) {
  125. color = color << 8;
  126. color = color | c;
  127. }
  128. return color;
  129. }
  130. private static int[] getRandomRgb() {
  131. int[] rgb = new int[3];
  132. for (int i = 0; i < 3; i++) {
  133. rgb[i] = random.nextInt(255);
  134. }
  135. return rgb;
  136. }
  137. private static void shear(Graphics g, int w1, int h1, Color color) {
  138. shearX(g, w1, h1, color);
  139. shearY(g, w1, h1, color);
  140. }
  141. private static void shearX(Graphics g, int w1, int h1, Color color) {
  142. int period = random.nextInt(2);
  143. boolean borderGap = true;
  144. int frames = 1;
  145. int phase = random.nextInt(2);
  146. for (int i = 0; i < h1; i++) {
  147. double d = (double) (period >> 1)
  148. * Math.sin((double) i / (double) period
  149. + (6.2831853071795862D * (double) phase)
  150. / (double) frames);
  151. g.copyArea(0, i, w1, 1, (int) d, 0);
  152. if (borderGap) {
  153. g.setColor(color);
  154. g.drawLine((int) d, i, 0, i);
  155. g.drawLine((int) d + w1, i, w1, i);
  156. }
  157. }
  158. }
  159. private static void shearY(Graphics g, int w1, int h1, Color color) {
  160. int period = random.nextInt(40) + 10; // 50;
  161. boolean borderGap = true;
  162. int frames = 20;
  163. int phase = 7;
  164. for (int i = 0; i < w1; i++) {
  165. double d = (double) (period >> 1)
  166. * Math.sin((double) i / (double) period
  167. + (6.2831853071795862D * (double) phase)
  168. / (double) frames);
  169. g.copyArea(i, 0, 1, h1, 0, (int) d);
  170. if (borderGap) {
  171. g.setColor(color);
  172. g.drawLine(i, (int) d, i, 0);
  173. g.drawLine(i, (int) d + h1, i, h1);
  174. }
  175. }
  176. }
  177. /**
  178. * 获取随机验证码及其加密图片
  179. *
  180. * @param size 验证码长度
  181. * @return
  182. * @throws IOException
  183. */
  184. public static ImgResult VerifyCode(int size) throws IOException {
  185. ImgResult rs = new ImgResult();
  186. String code = generateVerifyCode(size).toLowerCase();
  187. rs.setCode(Base64Util.encodeStr(code));
  188. ByteArrayOutputStream data = new ByteArrayOutputStream();
  189. outputImage(200, 40, data, code);
  190. rs.setImg(Base64Util.encodeStr(data.toByteArray()));
  191. return rs;
  192. }
  193. }