1cb8fc414ddc484be4249020be07da236fb756f4.svn-base 7.5 KB

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