c5a267e9c256e779ec0a7c926db766e7f431bac6.svn-base 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package com.yihu.utils;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Graphics2D;
  6. import java.awt.RenderingHints;
  7. import java.awt.geom.AffineTransform;
  8. import java.awt.image.BufferedImage;
  9. import java.io.File;
  10. import java.io.FileOutputStream;
  11. import java.io.IOException;
  12. import java.io.OutputStream;
  13. import java.util.Arrays;
  14. import java.util.Random;
  15. import javax.imageio.ImageIO;
  16. public class VerifyCodeUtils{
  17. //使用到Algerian字体,系统里没有的话需要安装字体,字体只显示大写,去掉了1,0,i,o几个容易混淆的字符
  18. public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  19. private static Random random = new Random();
  20. /**
  21. * 验证码对象
  22. * @author zhou-baicheng
  23. *
  24. */
  25. public static class Verify{
  26. private String code;//如 1 + 2
  27. private Integer value;//如 3
  28. public String getCode() {
  29. return code;
  30. }
  31. public void setCode(String code) {
  32. this.code = code;
  33. }
  34. public Integer getValue() {
  35. return value;
  36. }
  37. public void setValue(Integer value) {
  38. this.value = value;
  39. }
  40. }
  41. /**
  42. * 使用系统默认字符源生成验证码
  43. * @param verifySize 验证码长度
  44. * @return
  45. */
  46. public static Verify generateVerify(){
  47. int number1 = new Random().nextInt(10) + 1;;
  48. int number2 = new Random().nextInt(10) + 1;;
  49. Verify entity = new Verify();
  50. entity.setCode(number1 + " x " + number2);
  51. entity.setValue(number1 + number2);
  52. return entity;
  53. }
  54. /**
  55. * 使用系统默认字符源生成验证码
  56. * @param verifySize 验证码长度
  57. * @return
  58. */
  59. public static String generateVerifyCode(int verifySize){
  60. return generateVerifyCode(verifySize, VERIFY_CODES);
  61. }
  62. /**
  63. * 使用指定源生成验证码
  64. * @param verifySize 验证码长度
  65. * @param sources 验证码字符源
  66. * @return
  67. */
  68. public static String generateVerifyCode(int verifySize, String sources){
  69. if(sources == null || sources.length() == 0){
  70. sources = VERIFY_CODES;
  71. }
  72. int codesLen = sources.length();
  73. Random rand = new Random(System.currentTimeMillis());
  74. StringBuilder verifyCode = new StringBuilder(verifySize);
  75. for(int i = 0; i < verifySize; i++){
  76. verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
  77. }
  78. return verifyCode.toString();
  79. }
  80. /**
  81. * 生成随机验证码文件,并返回验证码值
  82. * @param w
  83. * @param h
  84. * @param outputFile
  85. * @param verifySize
  86. * @return
  87. * @throws IOException
  88. */
  89. public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException{
  90. String verifyCode = generateVerifyCode(verifySize);
  91. outputImage(w, h, outputFile, verifyCode);
  92. return verifyCode;
  93. }
  94. /**
  95. * 输出随机验证码图片流,并返回验证码值
  96. * @param w
  97. * @param h
  98. * @param os
  99. * @param verifySize
  100. * @return
  101. * @throws IOException
  102. */
  103. public static String outputVerifyImage(int w, int h, OutputStream os, int verifySize) throws IOException{
  104. String verifyCode = generateVerifyCode(verifySize);
  105. outputImage(w, h, os, verifyCode);
  106. return verifyCode;
  107. }
  108. /**
  109. * 生成指定验证码图像文件
  110. * @param w
  111. * @param h
  112. * @param outputFile
  113. * @param code
  114. * @throws IOException
  115. */
  116. public static void outputImage(int w, int h, File outputFile, String code) throws IOException{
  117. if(outputFile == null){
  118. return;
  119. }
  120. File dir = outputFile.getParentFile();
  121. if(!dir.exists()){
  122. dir.mkdirs();
  123. }
  124. try{
  125. outputFile.createNewFile();
  126. FileOutputStream fos = new FileOutputStream(outputFile);
  127. outputImage(w, h, fos, code);
  128. fos.close();
  129. } catch(IOException e){
  130. throw e;
  131. }
  132. }
  133. /**
  134. * 输出指定验证码图片流
  135. * @param w
  136. * @param h
  137. * @param os
  138. * @param code
  139. * @throws IOException
  140. */
  141. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException{
  142. int verifySize = code.length();
  143. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  144. Random rand = new Random();
  145. Graphics2D g2 = image.createGraphics();
  146. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  147. Color[] colors = new Color[5];
  148. Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
  149. Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
  150. Color.PINK, Color.YELLOW };
  151. float[] fractions = new float[colors.length];
  152. for(int i = 0; i < colors.length; i++){
  153. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
  154. fractions[i] = rand.nextFloat();
  155. }
  156. Arrays.sort(fractions);
  157. g2.setColor(Color.GRAY);// 设置边框色
  158. g2.fillRect(0, 0, w, h);
  159. Color c = getRandColor(200, 250);
  160. g2.setColor(c);// 设置背景色
  161. g2.fillRect(0, 2, w, h-4);
  162. //绘制干扰线
  163. Random random = new Random();
  164. g2.setColor(getRandColor(160, 200));// 设置线条的颜色
  165. for (int i = 0; i < 20; i++) {
  166. int x = random.nextInt(w - 1);
  167. int y = random.nextInt(h - 1);
  168. int xl = random.nextInt(6) + 1;
  169. int yl = random.nextInt(12) + 1;
  170. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
  171. }
  172. // 添加噪点
  173. float yawpRate = 0.05f;// 噪声率
  174. int area = (int) (yawpRate * w * h);
  175. for (int i = 0; i < area; i++) {
  176. int x = random.nextInt(w);
  177. int y = random.nextInt(h);
  178. int rgb = getRandomIntColor();
  179. image.setRGB(x, y, rgb);
  180. }
  181. shear(g2, w, h, c);// 使图片扭曲
  182. g2.setColor(getRandColor(100, 160));
  183. int fontSize = h-4;
  184. Font font = new Font("Algerian", Font.ITALIC, fontSize);
  185. g2.setFont(font);
  186. char[] chars = code.toCharArray();
  187. for(int i = 0; i < verifySize; i++){
  188. AffineTransform affine = new AffineTransform();
  189. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);
  190. g2.setTransform(affine);
  191. g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);
  192. }
  193. g2.dispose();
  194. ImageIO.write(image, "jpg", os);
  195. }
  196. private static Color getRandColor(int fc, int bc) {
  197. if (fc > 255)
  198. fc = 255;
  199. if (bc > 255)
  200. bc = 255;
  201. int r = fc + random.nextInt(bc - fc);
  202. int g = fc + random.nextInt(bc - fc);
  203. int b = fc + random.nextInt(bc - fc);
  204. return new Color(r, g, b);
  205. }
  206. private static int getRandomIntColor() {
  207. int[] rgb = getRandomRgb();
  208. int color = 0;
  209. for (int c : rgb) {
  210. color = color << 8;
  211. color = color | c;
  212. }
  213. return color;
  214. }
  215. private static int[] getRandomRgb() {
  216. int[] rgb = new int[3];
  217. for (int i = 0; i < 3; i++) {
  218. rgb[i] = random.nextInt(255);
  219. }
  220. return rgb;
  221. }
  222. private static void shear(Graphics g, int w1, int h1, Color color) {
  223. shearX(g, w1, h1, color);
  224. shearY(g, w1, h1, color);
  225. }
  226. private static void shearX(Graphics g, int w1, int h1, Color color) {
  227. int period = random.nextInt(2);
  228. boolean borderGap = true;
  229. int frames = 1;
  230. int phase = random.nextInt(2);
  231. for (int i = 0; i < h1; i++) {
  232. double d = (double) (period >> 1)
  233. * Math.sin((double) i / (double) period
  234. + (6.2831853071795862D * (double) phase)
  235. / (double) frames);
  236. g.copyArea(0, i, w1, 1, (int) d, 0);
  237. if (borderGap) {
  238. g.setColor(color);
  239. g.drawLine((int) d, i, 0, i);
  240. g.drawLine((int) d + w1, i, w1, i);
  241. }
  242. }
  243. }
  244. private static void shearY(Graphics g, int w1, int h1, Color color) {
  245. int period = random.nextInt(40) + 10; // 50;
  246. boolean borderGap = true;
  247. int frames = 20;
  248. int phase = 7;
  249. for (int i = 0; i < w1; i++) {
  250. double d = (double) (period >> 1)
  251. * Math.sin((double) i / (double) period
  252. + (6.2831853071795862D * (double) phase)
  253. / (double) frames);
  254. g.copyArea(i, 0, 1, h1, 0, (int) d);
  255. if (borderGap) {
  256. g.setColor(color);
  257. g.drawLine(i, (int) d, i, 0);
  258. g.drawLine(i, (int) d + h1, i, h1);
  259. }
  260. }
  261. }
  262. public static void main(String[] args) throws IOException{
  263. File dir = new File("F:/verifies");
  264. int w = 200, h = 80;
  265. for(int i = 0; i < 50; i++){
  266. String verifyCode = generateVerifyCode(4);
  267. File file = new File(dir, verifyCode + ".jpg");
  268. outputImage(w, h, file, verifyCode);
  269. }
  270. }
  271. }