Class AbstractCaptchaGenerator

java.lang.Object
cloud.opencode.base.captcha.generator.AbstractCaptchaGenerator
Direct Known Subclasses:
ArithmeticCaptchaGenerator, AudioCaptchaGenerator, ChineseCaptchaGenerator, GifCaptchaGenerator, ImageCaptchaGenerator, JigsawCaptchaGenerator, PowCaptchaGenerator, SpecCaptchaGenerator

public abstract class AbstractCaptchaGenerator extends Object
Abstract Captcha Generator - Base class for CAPTCHA generators 抽象验证码生成器 - 验证码生成器的基类

This abstract class provides common functionality for CAPTCHA generation.

此抽象类提供验证码生成的通用功能。

Features | 主要功能:

  • BufferedImage creation and graphics setup - BufferedImage 创建和图形设置
  • Noise drawing utilities - 噪点绘制工具
  • Image-to-bytes conversion - 图像到字节转换
  • ID generation and metadata creation - ID 生成和元数据创建

Usage Examples | 使用示例:

public class CustomGenerator extends AbstractCaptchaGenerator implements CaptchaGenerator {
    public Captcha generate(CaptchaConfig config) {
        BufferedImage image = createImage(config);
        Graphics2D g = createGraphics(image, config);
        // custom rendering...
        byte[] data = toBytes(image);
        return buildCaptcha(CaptchaType.ALPHANUMERIC, data, code, config);
    }
}

Security | 安全性:

  • Thread-safe: Yes (stateless, no shared mutable state) - 线程安全: 是(无状态,无共享可变状态)
  • Null-safe: No (parameters must be non-null) - 空值安全: 否(参数不能为空)
Since:
JDK 25, opencode-base-captcha V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • AbstractCaptchaGenerator

      public AbstractCaptchaGenerator()
  • Method Details

    • createImage

      protected BufferedImage createImage(CaptchaConfig config)
      Creates a buffered image with the specified configuration. 使用指定配置创建缓冲图像。
      Parameters:
      config - the configuration | 配置
      Returns:
      the buffered image | 缓冲图像
    • createGraphics

      protected Graphics2D createGraphics(BufferedImage image, CaptchaConfig config)
      Creates graphics for the image. 为图像创建图形。
      Parameters:
      image - the image | 图像
      config - the configuration | 配置
      Returns:
      the graphics | 图形
    • drawNoise

      protected void drawNoise(Graphics2D g, CaptchaConfig config)
      Draws noise on the image. 在图像上绘制噪点。
      Parameters:
      g - the graphics | 图形
      config - the configuration | 配置
    • toBytes

      protected byte[] toBytes(BufferedImage image)
      Converts image to PNG bytes. 将图像转换为 PNG 字节。
      Parameters:
      image - the image | 图像
      Returns:
      the PNG bytes | PNG 字节
    • generateId

      protected String generateId()
      Generates a unique CAPTCHA ID. 生成唯一的验证码 ID。
      Returns:
      the unique ID | 唯一 ID
    • createMetadata

      protected Map<String,Object> createMetadata(CaptchaConfig config)
      Creates metadata for the CAPTCHA. 为验证码创建元数据。
      Parameters:
      config - the configuration | 配置
      Returns:
      the metadata | 元数据
    • buildCaptcha

      protected Captcha buildCaptcha(CaptchaType type, byte[] imageData, String answer, CaptchaConfig config)
      Builds a CAPTCHA from generated data. 从生成的数据构建验证码。
      Parameters:
      type - the CAPTCHA type | 验证码类型
      imageData - the image data | 图像数据
      answer - the answer | 答案
      config - the configuration | 配置
      Returns:
      the CAPTCHA | 验证码