Class CaptchaNoiseUtil

java.lang.Object
cloud.opencode.base.captcha.support.CaptchaNoiseUtil

public final class CaptchaNoiseUtil extends Object
Captcha Noise Utility - Noise generation utilities 验证码噪点工具 - 噪点生成工具

This class provides noise and distortion utilities for CAPTCHA.

此类提供验证码的噪点和干扰工具。

Features | 主要功能:

  • Noise dot generation - 噪点生成
  • Interference line drawing - 干扰线绘制
  • Curve distortion - 曲线扭曲

Usage Examples | 使用示例:

CaptchaNoiseUtil.drawNoise(graphics, config);
CaptchaNoiseUtil.drawLines(graphics, config);

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
  • Null-safe: No (graphics and config must not be null) - 空值安全: 否(图形和配置不能为null)

Performance | 性能特性:

  • Time complexity: O(k) where k is the configured noise count - 时间复杂度: O(k),k 为配置的噪点数量
  • Space complexity: O(1) - no intermediate data structures - 空间复杂度: O(1) 无中间数据结构
Since:
JDK 25, opencode-base-captcha V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • drawNoiseLines

      public static void drawNoiseLines(Graphics2D g, CaptchaConfig config)
      Draws noise lines on the graphics. 在图形上绘制干扰线。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawCurveLines

      public static void drawCurveLines(Graphics2D g, CaptchaConfig config)
      Draws curve lines on the graphics. 在图形上绘制曲线。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawCubicCurveLines

      public static void drawCubicCurveLines(Graphics2D g, CaptchaConfig config)
      Draws cubic curve lines on the graphics. 在图形上绘制三次曲线。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawNoiseDots

      public static void drawNoiseDots(Graphics2D g, CaptchaConfig config)
      Draws noise dots on the graphics. 在图形上绘制干扰点。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawBackgroundNoise

      public static void drawBackgroundNoise(Graphics2D g, CaptchaConfig config)
      Draws background noise. 绘制背景噪点。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • shear

      public static void shear(Graphics2D g, CaptchaConfig config)
      Draws a shear transform effect. 绘制剪切变换效果。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawGradientBackground

      public static void drawGradientBackground(Graphics2D g, CaptchaConfig config)
      Draws background with gradient. 绘制渐变背景。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawInterferencePattern

      public static void drawInterferencePattern(Graphics2D g, CaptchaConfig config)
      Draws interference pattern. 绘制干扰图案。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • drawBezierNoise

      public static void drawBezierNoise(Graphics2D g, int width, int height, int count)
      Draws Bezier curve noise that passes through the character area for anti-OCR. 绘制穿过字符区域的贝塞尔曲线噪声,用于抗 OCR。

      Unlike drawCubicCurveLines(Graphics2D, CaptchaConfig), this method forces all curves to pass through the character center area (20%-80% of height), making OCR recognition more difficult.

      drawCubicCurveLines(Graphics2D, CaptchaConfig) 不同,此方法强制所有曲线 穿过字符中心区域(高度的 20%-80%),使 OCR 识别更加困难。

      Parameters:
      g - the graphics | 图形
      width - the image width | 图像宽度
      height - the image height | 图像高度
      count - the number of curves to draw | 要绘制的曲线数量
      Since:
      JDK 25, opencode-base-captcha V1.0.3
    • applySineWarp

      public static BufferedImage applySineWarp(BufferedImage image, double amplitude, double period)
      Applies sine wave warp distortion to an image. 对图像施加正弦波变形扭曲。

      Each row of pixels is shifted horizontally by a sine function, creating a wave-like distortion effect. Pixels shifted beyond the image boundary are filled with the background color (sampled from the top-left corner of the source image).

      每行像素按正弦函数水平偏移,产生波浪状扭曲效果。超出图像边界的像素 用背景色(取自源图像左上角像素颜色)填充。

      Parameters:
      image - the source image | 源图像
      amplitude - the wave amplitude in pixels | 波浪振幅(像素)
      period - the wave period in pixels | 波浪周期(像素)
      Returns:
      the warped image | 变形后的图像
      Since:
      JDK 25, opencode-base-captcha V1.0.3
    • drawOutlineShadow

      public static void drawOutlineShadow(Graphics2D g, String text, Font font, int x, int y, Color shadowColor)
      Draws character outline shadow for anti-OCR. 绘制字符轮廓阴影,用于抗 OCR。

      Renders semi-transparent copies of the text offset by 1-2 pixels in four directions (up, down, left, right), creating a blurred outline that disrupts OCR segmentation.

      在上下左右四个方向各偏移 1-2 像素绘制半透明文本副本,产生模糊轮廓 以干扰 OCR 分割。

      Parameters:
      g - the graphics | 图形
      text - the text to draw shadow for | 要绘制阴影的文本
      font - the font | 字体
      x - the x position | x 坐标
      y - the y position | y 坐标
      shadowColor - the shadow color | 阴影颜色
      Since:
      JDK 25, opencode-base-captcha V1.0.3
    • calculateOverlapSpacing

      public static int calculateOverlapSpacing(int totalWidth, int charCount, float fontSize, float overlapRatio)
      Calculates character spacing with overlap for anti-OCR. 计算带重叠的字符间距,用于抗 OCR。

      Computes the horizontal spacing between character start positions, allowing adjacent characters to overlap by a configurable ratio. This makes character segmentation harder for OCR engines.

      计算字符起始位置之间的水平间距,允许相邻字符按可配置比例重叠。 这使 OCR 引擎更难进行字符分割。

      Parameters:
      totalWidth - the total available width | 总可用宽度
      charCount - the number of characters | 字符数
      fontSize - the font size | 字体大小
      overlapRatio - the overlap ratio (0.0 to 0.5) | 重叠比例(0.0 到 0.5)
      Returns:
      the spacing between character start positions | 字符起始位置间距
      Since:
      JDK 25, opencode-base-captcha V1.0.3