Module bus.extra

Class AbstractProvider

java.lang.Object
org.miaixz.bus.extra.captcha.AbstractProvider
All Implemented Interfaces:
Serializable, org.miaixz.bus.core.Provider, CaptchaProvider
Direct Known Subclasses:
CircleProvider, ClickWordProvider, GifProvider, LineProvider, PuzzleProvider, ShearProvider

public abstract class AbstractProvider extends Object implements CaptchaProvider
Abstract CAPTCHA. This abstract class implements CAPTCHA string generation, verification, and image writing. Implementations generate the image object by implementing the createImage(String) method.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Field Details

    • width

      protected int width
      The width of the image.
    • height

      protected int height
      The height of the image.
    • interfereCount

      protected int interfereCount
      The number of interfering elements in the CAPTCHA.
    • font

      protected Font font
      The font.
    • code

      protected String code
      The CAPTCHA code.
    • imageBytes

      protected byte[] imageBytes
      The CAPTCHA image bytes.
    • generator

      protected CodeStrategy generator
      The CAPTCHA code generator.
    • background

      protected Color background
      The background color.
    • textAlpha

      protected AlphaComposite textAlpha
      The text transparency.
  • Constructor Details

    • AbstractProvider

      public AbstractProvider(int width, int height, int codeCount, int interfereCount)
      Constructor, uses a random CAPTCHA generator.
      Parameters:
      width - Image width.
      height - Image height.
      codeCount - Number of characters.
      interfereCount - Number of interfering elements.
    • AbstractProvider

      public AbstractProvider(int width, int height, CodeStrategy generator, int interfereCount)
      Constructor.
      Parameters:
      width - Image width.
      height - Image height.
      generator - CAPTCHA code generator.
      interfereCount - Number of interfering elements.
    • AbstractProvider

      public AbstractProvider(int width, int height, CodeStrategy generator, int interfereCount, float sizeBaseHeight)
      Constructor.
      Parameters:
      width - Image width.
      height - Image height.
      generator - CAPTCHA code generator.
      interfereCount - Number of interfering elements.
      sizeBaseHeight - Font size as a multiplier of the height.
  • Method Details

    • create

      public void create()
      Description copied from interface: CaptchaProvider
      Creates a CAPTCHA. Implementations should generate both a random CAPTCHA string and a CAPTCHA image.
      Specified by:
      create in interface CaptchaProvider
    • generateCode

      protected void generateCode()
      Generates the CAPTCHA code string.
    • createImage

      protected abstract Image createImage(String code)
      Creates the CAPTCHA image based on the generated code.
      Parameters:
      code - The CAPTCHA code.
      Returns:
      The CAPTCHA image.
    • get

      public String get()
      Description copied from interface: CaptchaProvider
      Retrieves the text content of the generated CAPTCHA.
      Specified by:
      get in interface CaptchaProvider
      Returns:
      The text content of the CAPTCHA.
    • verify

      public boolean verify(String userInputCode)
      Description copied from interface: CaptchaProvider
      Verifies if the user-provided CAPTCHA input matches the generated CAPTCHA text. It is recommended to perform a case-insensitive comparison.
      Specified by:
      verify in interface CaptchaProvider
      Parameters:
      userInputCode - The CAPTCHA code entered by the user.
      Returns:
      true if the user input matches the generated CAPTCHA, false otherwise.
    • write

      public void write(String path) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes the CAPTCHA to a file.
      Parameters:
      path - The file path.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public void write(File file) throws org.miaixz.bus.core.lang.exception.InternalException
      Writes the CAPTCHA to a file.
      Parameters:
      file - The file.
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - if an I/O error occurs.
    • write

      public void write(OutputStream out)
      Description copied from interface: CaptchaProvider
      Writes the CAPTCHA image to the target output stream.
      Specified by:
      write in interface CaptchaProvider
      Parameters:
      out - The target output stream to which the CAPTCHA image will be written.
    • getImageBytes

      public byte[] getImageBytes()
      Gets the graphic CAPTCHA image bytes.
      Returns:
      The graphic CAPTCHA image bytes.
    • getImage

      public BufferedImage getImage()
      Gets the CAPTCHA image. Note: After using the returned BufferedImage, you need to call Image.flush() to release resources.
      Returns:
      The CAPTCHA image.
    • getImageBase64

      public String getImageBase64()
      Gets the Base64 representation of the image.
      Returns:
      The Base64 of the image.
    • getImageBase64Data

      public String getImageBase64Data()
      Gets the Base64 of the image with the file format.
      Returns:
      The Base64 of the image with the file format.
    • setFont

      public void setFont(Font font)
      Sets a custom font.
      Parameters:
      font - The font.
    • getGenerator

      public CodeStrategy getGenerator()
      Gets the CAPTCHA code generator.
      Returns:
      The CAPTCHA code generator.
    • setGenerator

      public void setGenerator(CodeStrategy generator)
      Sets the CAPTCHA code generator.
      Parameters:
      generator - The CAPTCHA code generator.
    • setBackground

      public void setBackground(Color background)
      Sets the background color. null means a transparent background.
      Parameters:
      background - The background color.
    • setTextAlpha

      public void setTextAlpha(float textAlpha)
      Sets the text transparency.
      Parameters:
      textAlpha - The text transparency, a value from 0 to 1, where 1 is opaque.