MicroCaptcha.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
  3. */
  4. package com.crunii.micro.common.captcha;
  5. import com.github.bingoohuang.patchca.background.BackgroundFactory;
  6. import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;
  7. import com.github.bingoohuang.patchca.filter.ConfigurableFilterFactory;
  8. import com.github.bingoohuang.patchca.filter.library.AbstractImageOp;
  9. import com.github.bingoohuang.patchca.filter.library.WobbleImageOp;
  10. import com.github.bingoohuang.patchca.font.FontFactory;
  11. import com.github.bingoohuang.patchca.service.Captcha;
  12. import com.github.bingoohuang.patchca.text.renderer.BestFitTextRenderer;
  13. import com.github.bingoohuang.patchca.word.RandomWordFactory;
  14. import com.github.bingoohuang.patchca.word.WordFactory;
  15. import org.springframework.beans.factory.InitializingBean;
  16. import javax.imageio.ImageIO;
  17. import java.awt.*;
  18. import java.awt.image.BufferedImage;
  19. import java.awt.image.BufferedImageOp;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Random;
  25. /**
  26. * @author 田平 create 2018年10月30日下午2:44:28
  27. */
  28. public class MicroCaptcha extends ConfigurableCaptchaService implements InitializingBean {
  29. /**
  30. * 取消0 o i 1
  31. */
  32. private String words = "abcdefghkmnpqstwxyzABCDEFGHKMNPQSTWXYZ23456789";
  33. private int wordLength = 4;
  34. /**
  35. * @return the wordLength
  36. */
  37. public int getWordLength() {
  38. return wordLength;
  39. }
  40. /**
  41. * @param wordLength the wordLength to set
  42. */
  43. public void setWordLength(int wordLength) {
  44. this.wordLength = wordLength;
  45. }
  46. /**
  47. * @return the words
  48. */
  49. public String getWords() {
  50. return words;
  51. }
  52. /**
  53. * @param words the words to set
  54. */
  55. public void setWords(String words) {
  56. this.words = words;
  57. }
  58. public MicroCaptcha() {
  59. }
  60. @Override
  61. public void afterPropertiesSet() throws Exception {
  62. RandomWordFactory wordFactory = new RandomWordFactory();
  63. wordFactory.setCharacters(words);
  64. wordFactory.setMaxLength(wordLength);
  65. wordFactory.setMinLength(wordLength);
  66. setWordFactory(wordFactory);
  67. // 兼容linux的字体
  68. FontFactory fontFactory = new FontFactory() {
  69. @Override
  70. public Font getFont(int arg0) {
  71. return new Font("STIX", Font.PLAIN, 36);
  72. }
  73. @Override
  74. public void setWord(String word) {
  75. }
  76. @Override
  77. public void setWordFactory(WordFactory wordFactory) {
  78. }
  79. };
  80. setFontFactory(fontFactory);
  81. CustomBackgroundFactory backgroundFactory = new CustomBackgroundFactory();
  82. setBackgroundFactory(backgroundFactory);
  83. // 图片滤镜设置
  84. ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();
  85. List<BufferedImageOp> filters = new ArrayList<BufferedImageOp>();
  86. WobbleImageOp wobbleImageOp = new WobbleImageOp();
  87. wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);
  88. wobbleImageOp.setxAmplitude(2.0);
  89. wobbleImageOp.setyAmplitude(1.0);
  90. filters.add(wobbleImageOp);
  91. filterFactory.setFilters(filters);
  92. setFilterFactory(filterFactory);
  93. // 文字渲染器设置
  94. BestFitTextRenderer textRenderer = new BestFitTextRenderer();
  95. textRenderer.setBottomMargin(3);
  96. textRenderer.setTopMargin(3);
  97. setTextRenderer(textRenderer);
  98. }
  99. private class CustomBackgroundFactory implements BackgroundFactory {
  100. private Random random = new Random();
  101. public void fillBackground(BufferedImage image) {
  102. Graphics graphics = image.getGraphics();
  103. // 验证码图片的宽高
  104. int imgWidth = image.getWidth();
  105. int imgHeight = image.getHeight();
  106. // 填充为白色背景
  107. graphics.setColor(Color.WHITE);
  108. graphics.fillRect(0, 0, imgWidth, imgHeight);
  109. // 画100个噪点(颜色及位置随机)
  110. for (int i = 0; i < 100; i++) {
  111. // 随机颜色
  112. int rInt = random.nextInt(255);
  113. int gInt = random.nextInt(255);
  114. int bInt = random.nextInt(255);
  115. graphics.setColor(new Color(rInt, gInt, bInt));
  116. // 随机位置
  117. int xInt = random.nextInt(imgWidth - 3);
  118. int yInt = random.nextInt(imgHeight - 2);
  119. // 随机旋转角度
  120. int sAngleInt = random.nextInt(360);
  121. int eAngleInt = random.nextInt(360);
  122. // 随机大小
  123. int wInt = random.nextInt(6);
  124. int hInt = random.nextInt(6);
  125. graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);
  126. // 画5条干扰线
  127. if (i % 20 == 0) {
  128. int xInt2 = random.nextInt(imgWidth);
  129. int yInt2 = random.nextInt(imgHeight);
  130. graphics.drawLine(xInt, yInt, xInt2, yInt2);
  131. }
  132. }
  133. }
  134. }
  135. public static void main(String[] args) throws Exception {
  136. MicroCaptcha microCaptcha = new MicroCaptcha();
  137. microCaptcha.afterPropertiesSet();
  138. microCaptcha.setWidth(250);
  139. microCaptcha.setHeight(60);
  140. Captcha captcha = microCaptcha.getCaptcha();
  141. System.out.println(captcha.getChallenge());
  142. File file = new File("D:\\captcha.png");
  143. try (FileOutputStream out = new FileOutputStream(file);) {
  144. ImageIO.write(captcha.getImage(), "png", out);
  145. }
  146. }
  147. }