KaptchaNoise.java 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package net.lab1024.smartadmin.common.kaptcha;
  2. import com.google.code.kaptcha.NoiseProducer;
  3. import com.google.code.kaptcha.util.Configurable;
  4. import java.awt.*;
  5. import java.awt.image.BufferedImage;
  6. import java.util.Random;
  7. /**
  8. * [ 验证码加噪处理 ]
  9. *
  10. * @author yandanyang
  11. * @version 1.0
  12. * @company 1024lab.net
  13. * @copyright (c) 2018 1024lab.netInc. All rights reserved.
  14. * @date 2019/7/6 0006 上午 10:47
  15. * @since JDK1.8
  16. */
  17. public class KaptchaNoise extends Configurable implements NoiseProducer {
  18. public KaptchaNoise() {
  19. }
  20. @Override
  21. public void makeNoise(BufferedImage image, float factorOne, float factorTwo, float factorThree, float factorFour) {
  22. int width = image.getWidth();
  23. int height = image.getHeight();
  24. Graphics2D graph = (Graphics2D)image.getGraphics();
  25. graph.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
  26. graph.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL));
  27. Random random = new Random();
  28. int noiseLineNum = random.nextInt(3);
  29. if(noiseLineNum == 0){
  30. noiseLineNum = 1;
  31. }
  32. for (int i = 0; i < noiseLineNum; i++){
  33. graph.setColor(KaptchaColor.getColor());
  34. graph.drawLine(random.nextInt(width), random.nextInt(height), 10 + random.nextInt(20), 10 + random.nextInt(20));
  35. }
  36. graph.dispose();
  37. }
  38. }