475d9a03e735caec9bd97f5894dd5f31bbbeba8f.svn-base 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.iamberry.app.api.util;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.image.BufferedImage;
  6. import java.util.Random;
  7. import com.iamberry.wechat.tools.NameUtils;
  8. /**
  9. * 验证码生成器
  10. * @company 深圳爱贝源科技有限公司
  11. * @website www.iamberry.com
  12. * @author 献
  13. * @tel 18271840547
  14. * @date 2016年12月13日
  15. */
  16. public class VerifyCodeUtil {
  17. private VerifyCodeUtil(){}
  18. /**
  19. * 生成随机颜色
  20. */
  21. private static Color generateRandomColor() {
  22. Random random = new Random();
  23. return new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255));
  24. }
  25. /**
  26. * 生成图片验证码
  27. * @param type 验证码类型,参见本类的静态属性
  28. * @param length 验证码字符长度,要求大于0的整数
  29. * @param excludeString 需排除的特殊字符
  30. * @param width 图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度)
  31. * @param height 图片高度
  32. * @param interLine 图片中干扰线的条数
  33. * @param randomLocation 每个字符的高低位置是否随机
  34. * @param backColor 图片颜色,若为null则表示采用随机颜色
  35. * @param foreColor 字体颜色,若为null则表示采用随机颜色
  36. * @param lineColor 干扰线颜色,若为null则表示采用随机颜色
  37. * @return 图片缓存对象
  38. */
  39. public static BufferedImage generateImageCode(int type, int length, String excludeString, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){
  40. String textCode = generateTextCode(type, length, excludeString);
  41. return generateImageCode(textCode, width, height, interLine, randomLocation, backColor, foreColor, lineColor);
  42. }
  43. /**
  44. * 生成验证码字符串
  45. * @param type 验证码类型,参见本类的静态属性
  46. * @param length 验证码长度,要求大于0的整数
  47. * @param excludeString 需排除的特殊字符(无需排除则为null)
  48. * @return 验证码字符串
  49. */
  50. public static String generateTextCode(int type, int length, String excludeString){
  51. if(length <= 0){
  52. return "";
  53. }
  54. StringBuffer verifyCode = new StringBuffer();
  55. int i = 0;
  56. Random random = new Random();
  57. switch(type){
  58. case NameUtils.TYPE_NUM_ONLY:
  59. while(i < length){
  60. int t = random.nextInt(10);
  61. //排除特殊字符
  62. if(null==excludeString || excludeString.indexOf(t+"")<0) {
  63. verifyCode.append(t);
  64. i++;
  65. }
  66. }
  67. break;
  68. case NameUtils.TYPE_LETTER_ONLY:
  69. while(i < length){
  70. int t = random.nextInt(123);
  71. if((t>=97 || (t>=65&&t<=90)) && (null==excludeString||excludeString.indexOf((char)t)<0)){
  72. verifyCode.append((char)t);
  73. i++;
  74. }
  75. }
  76. break;
  77. case NameUtils.TYPE_ALL_MIXED:
  78. while(i < length){
  79. int t = random.nextInt(123);
  80. if((t>=97 || (t>=65&&t<=90) || (t>=48&&t<=57)) && (null==excludeString||excludeString.indexOf((char)t)<0)){
  81. verifyCode.append((char)t);
  82. i++;
  83. }
  84. }
  85. break;
  86. case NameUtils.TYPE_NUM_UPPER:
  87. while(i < length){
  88. int t = random.nextInt(91);
  89. if((t>=65 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){
  90. verifyCode.append((char)t);
  91. i++;
  92. }
  93. }
  94. break;
  95. case NameUtils.TYPE_NUM_LOWER:
  96. while(i < length){
  97. int t = random.nextInt(123);
  98. if((t>=97 || (t>=48&&t<=57)) && (null==excludeString || excludeString.indexOf((char)t)<0)){
  99. verifyCode.append((char)t);
  100. i++;
  101. }
  102. }
  103. break;
  104. case NameUtils.TYPE_UPPER_ONLY:
  105. while(i < length){
  106. int t = random.nextInt(91);
  107. if((t >= 65) && (null==excludeString||excludeString.indexOf((char)t)<0)){
  108. verifyCode.append((char)t);
  109. i++;
  110. }
  111. }
  112. break;
  113. case NameUtils.TYPE_LOWER_ONLY:
  114. while(i < length){
  115. int t = random.nextInt(123);
  116. if((t>=97) && (null==excludeString||excludeString.indexOf((char)t)<0)){
  117. verifyCode.append((char)t);
  118. i++;
  119. }
  120. }
  121. break;
  122. }
  123. return verifyCode.toString();
  124. }
  125. /**
  126. * 已有验证码,生成验证码图片
  127. * @param textCode 文本验证码
  128. * @param width 图片宽度(注意此宽度若过小,容易造成验证码文本显示不全,如4个字符的文本可使用85到90的宽度)
  129. * @param height 图片高度
  130. * @param interLine 图片中干扰线的条数
  131. * @param randomLocation 每个字符的高低位置是否随机
  132. * @param backColor 图片颜色,若为null则表示采用随机颜色
  133. * @param foreColor 字体颜色,若为null则表示采用随机颜色
  134. * @param lineColor 干扰线颜色,若为null则表示采用随机颜色
  135. * @return 图片缓存对象
  136. */
  137. public static BufferedImage generateImageCode(String textCode, int width, int height, int interLine, boolean randomLocation, Color backColor, Color foreColor, Color lineColor){
  138. //创建内存图像
  139. BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  140. //获取图形上下文
  141. Graphics graphics = bufferedImage.getGraphics();
  142. //画背景图
  143. graphics.setColor(null==backColor ? generateRandomColor() : backColor);
  144. graphics.fillRect(0, 0, width, height);
  145. //画干扰线
  146. Random random = new Random();
  147. if(interLine > 0){
  148. int x = 0, y = 0, x1 = width, y1 = 0;
  149. for(int i=0; i<interLine; i++){
  150. graphics.setColor(null==lineColor ? generateRandomColor() : lineColor);
  151. y = random.nextInt(height);
  152. y1 = random.nextInt(height);
  153. graphics.drawLine(x, y, x1, y1);
  154. }
  155. }
  156. //字体大小为图片高度的80%
  157. int fsize = (int)(height * 0.8);
  158. int fx = height - fsize;
  159. int fy = fsize;
  160. //设定字体
  161. graphics.setFont(new Font("Default", Font.PLAIN, fsize));
  162. //写验证码字符
  163. for(int i=0; i<textCode.length(); i++){
  164. fy = randomLocation ? (int)((Math.random()*0.3+0.6)*height) : fy;
  165. graphics.setColor(null==foreColor ? generateRandomColor() : foreColor);
  166. //将验证码字符显示到图象中
  167. graphics.drawString(textCode.charAt(i)+"", fx, fy);
  168. fx += fsize * 0.9;
  169. }
  170. graphics.dispose();
  171. return bufferedImage;
  172. }
  173. }