验证码的工具类源码

  1. package com.itheima.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.LinearGradientPaint;
  7. import java.awt.Paint;
  8. import java.awt.RenderingHints;
  9. import java.awt.geom.AffineTransform;
  10. import java.awt.image.BufferedImage;
  11. import java.io.File;
  12. import java.io.FileOutputStream;
  13. import java.io.IOException;
  14. import java.io.OutputStream;
  15. import java.util.Arrays;
  16. import java.util.Random;
  17. import javax.imageio.ImageIO;
  18. public class VerifyCodeUtils{
  19. public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  20. private static Random random = new Random();
  21. public static String generateVerifyCode(int verifySize){
  22. return generateVerifyCode(verifySize, VERIFY_CODES);
  23. }
  24. public static String generateVerifyCode(int verifySize, String sources){
  25. if(sources == null || sources.length() == 0){
  26. sources = VERIFY_CODES;
  27. }
  28. int codesLen = sources.length();
  29. Random rand = new Random(System.currentTimeMillis());
  30. StringBuilder verifyCode = new StringBuilder(verifySize);
  31. for(int i = 0; i < verifySize; i++){
  32. verifyCode.append(sources.charAt(rand.nextInt(codesLen-1)));
  33. }
  34. return verifyCode.toString();
  35. }
  36. public static String outputVerifyImage(int w, int h, File outputFile, int verifySize) throws IOException{
  37. String verifyCode = generateVerifyCode(verifySize);
  38. outputImage(w, h, outputFile, verifyCode);
  39. return verifyCode;
  40. }
  41. public static String outputVerifyImage(int w, int h, OutputStream os, int verifySize) throws IOException{
  42. String verifyCode = generateVerifyCode(verifySize);
  43. outputImage(w, h, os, verifyCode);
  44. return verifyCode;
  45. }
  46. public static void outputImage(int w, int h, File outputFile, String code) throws IOException{
  47. if(outputFile == null){
  48. return;
  49. }
  50. File dir = outputFile.getParentFile();
  51. if(!dir.exists()){
  52. dir.mkdirs();
  53. }
  54. try{
  55. outputFile.createNewFile();
  56. FileOutputStream fos = new FileOutputStream(outputFile);
  57. outputImage(w, h, fos, code);
  58. fos.close();
  59. } catch(IOException e){
  60. throw e;
  61. }
  62. }
  63. public static void outputImage(int w, int h, OutputStream os, String code) throws IOException{
  64. int verifySize = code.length();
  65. BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  66. Random rand = new Random();
  67. Graphics2D g2 = image.createGraphics();
  68. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
  69. Color[] colors = new Color[5];
  70. Color[] colorSpaces = new Color[] { Color.WHITE, Color.CYAN,
  71. Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
  72. Color.PINK, Color.YELLOW };
  73. float[] fractions = new float[colors.length];
  74. for(int i = 0; i < colors.length; i++){
  75. colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
  76. fractions[i] = rand.nextFloat();
  77. }
  78. Arrays.sort(fractions);
  79. g2.setColor(Color.GRAY);
  80. g2.fillRect(0, 0, w, h);
  81. Color c = getRandColor(200, 250);
  82. g2.setColor(c);
  83. g2.fillRect(0, 2, w, h-4);
  84. Random random = new Random();
  85. g2.setColor(getRandColor(160, 200));
  86. for (int i = 0; i < 20; i++) {
  87. int x = random.nextInt(w - 1);
  88. int y = random.nextInt(h - 1);
  89. int xl = random.nextInt(6) + 1;
  90. int yl = random.nextInt(12) + 1;
  91. g2.drawLine(x, y, x + xl + 40, y + yl + 20);
  92. }
  93. float yawpRate = 0.05f;
  94. int area = (int) (yawpRate * w * h);
  95. for (int i = 0; i < area; i++) {
  96. int x = random.nextInt(w);
  97. int y = random.nextInt(h);
  98. int rgb = getRandomIntColor();
  99. image.setRGB(x, y, rgb);
  100. }
  101. shear(g2, w, h, c);
  102. g2.setColor(getRandColor(100, 160));
  103. int fontSize = h-4;
  104. Font font = new Font("Algerian", Font.ITALIC, fontSize);
  105. g2.setFont(font);
  106. char[] chars = code.toCharArray();
  107. for(int i = 0; i < verifySize; i++){
  108. AffineTransform affine = new AffineTransform();
  109. affine.setToRotation(Math.PI / 4 * rand.nextDouble() * (rand.nextBoolean() ? 1 : -1), (w / verifySize) * i + fontSize/2, h/2);
  110. g2.setTransform(affine);
  111. g2.drawChars(chars, i, 1, ((w-10) / verifySize) * i + 5, h/2 + fontSize/2 - 10);
  112. }
  113. g2.dispose();
  114. ImageIO.write(image, "jpg", os);
  115. }
  116. private static Color getRandColor(int fc, int bc) {
  117. if (fc > 255)
  118. fc = 255;
  119. if (bc > 255)
  120. bc = 255;
  121. int r = fc + random.nextInt(bc - fc);
  122. int g = fc + random.nextInt(bc - fc);
  123. int b = fc + random.nextInt(bc - fc);
  124. return new Color(r, g, b);
  125. }
  126. private static int getRandomIntColor() {
  127. int[] rgb = getRandomRgb();
  128. int color = 0;
  129. for (int c : rgb) {
  130. color = color << 8;
  131. color = color | c;
  132. }
  133. return color;
  134. }
  135. private static int[] getRandomRgb() {
  136. int[] rgb = new int[3];
  137. for (int i = 0; i < 3; i++) {
  138. rgb[i] = random.nextInt(255);
  139. }
  140. return rgb;
  141. }
  142. private static void shear(Graphics g, int w1, int h1, Color color) {
  143. shearX(g, w1, h1, color);
  144. shearY(g, w1, h1, color);
  145. }
  146. private static void shearX(Graphics g, int w1, int h1, Color color) {
  147. int period = random.nextInt(2);
  148. boolean borderGap = true;
  149. int frames = 1;
  150. int phase = random.nextInt(2);
  151. for (int i = 0; i < h1; i++) {
  152. double d = (double) (period >> 1)
  153. * Math.sin((double) i / (double) period
  154. + (6.2831853071795862D * (double) phase)
  155. / (double) frames);
  156. g.copyArea(0, i, w1, 1, (int) d, 0);
  157. if (borderGap) {
  158. g.setColor(color);
  159. g.drawLine((int) d, i, 0, i);
  160. g.drawLine((int) d + w1, i, w1, i);
  161. }
  162. }
  163. }
  164. private static void shearY(Graphics g, int w1, int h1, Color color) {
  165. int period = random.nextInt(40) + 10; // 50;
  166. boolean borderGap = true;
  167. int frames = 20;
  168. int phase = 7;
  169. for (int i = 0; i < w1; i++) {
  170. double d = (double) (period >> 1)
  171. * Math.sin((double) i / (double) period
  172. + (6.2831853071795862D * (double) phase)
  173. / (double) frames);
  174. g.copyArea(i, 0, 1, h1, 0, (int) d);
  175. if (borderGap) {
  176. g.setColor(color);
  177. g.drawLine(i, (int) d, i, 0);
  178. g.drawLine(i, (int) d + h1, i, h1);
  179. }
  180. }
  181. }
  182. public static String finalOutputImage(OutputStream out) throws IOException{
  183. String verifyCode = generateVerifyCode(5).toLowerCase();
  184. int w = 300;
  185. int h = 90;
  186. outputImage(w,h,out,verifyCode);
  187. return verifyCode;
  188. }
  189. /*public static void main(String[] args) throws IOException{
  190. File dir = new File("F:/verifies");
  191. int w = 200, h = 80;
  192. for(int i = 0; i < 10; i++){
  193. String verifyCode = generateVerifyCode(5).toLowerCase();
  194. File file = new File(dir, verifyCode + ".jpg");
  195. outputImage(w, h, file, verifyCode);
  196. }
  197. }*/
  198. }





posted @ 2016-01-01 18:58  蓝蝶岳  阅读(208)  评论(0编辑  收藏  举报