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
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 Summary
FieldsModifier and TypeFieldDescriptionprotected ColorThe background color.protected StringThe CAPTCHA code.protected FontThe font.protected CodeStrategyThe CAPTCHA code generator.protected intThe height of the image.protected byte[]The CAPTCHA image bytes.protected intThe number of interfering elements in the CAPTCHA.protected AlphaCompositeThe text transparency.protected intThe width of the image. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractProvider(int width, int height, int codeCount, int interfereCount) Constructor, uses a random CAPTCHA generator.AbstractProvider(int width, int height, CodeStrategy generator, int interfereCount) Constructor.AbstractProvider(int width, int height, CodeStrategy generator, int interfereCount, float sizeBaseHeight) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidcreate()Creates a CAPTCHA.protected abstract ImagecreateImage(String code) Creates the CAPTCHA image based on the generated code.protected voidGenerates the CAPTCHA code string.get()Retrieves the text content of the generated CAPTCHA.Gets the CAPTCHA code generator.getImage()Gets the CAPTCHA image.Gets the Base64 representation of the image.Gets the Base64 of the image with the file format.byte[]Gets the graphic CAPTCHA image bytes.voidsetBackground(Color background) Sets the background color.voidSets a custom font.voidsetGenerator(CodeStrategy generator) Sets the CAPTCHA code generator.voidsetTextAlpha(float textAlpha) Sets the text transparency.booleanVerifies if the user-provided CAPTCHA input matches the generated CAPTCHA text.voidWrites the CAPTCHA to a file.voidwrite(OutputStream out) Writes the CAPTCHA image to the target output stream.voidWrites the CAPTCHA to a file.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.miaixz.bus.extra.captcha.CaptchaProvider
type
-
Field Details
-
width
protected int widthThe width of the image. -
height
protected int heightThe height of the image. -
interfereCount
protected int interfereCountThe number of interfering elements in the CAPTCHA. -
font
The font. -
code
The CAPTCHA code. -
imageBytes
protected byte[] imageBytesThe CAPTCHA image bytes. -
generator
The CAPTCHA code generator. -
background
The background color. -
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
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:CaptchaProviderCreates a CAPTCHA. Implementations should generate both a random CAPTCHA string and a CAPTCHA image.- Specified by:
createin interfaceCaptchaProvider
-
generateCode
protected void generateCode()Generates the CAPTCHA code string. -
createImage
Creates the CAPTCHA image based on the generated code.- Parameters:
code- The CAPTCHA code.- Returns:
- The CAPTCHA image.
-
get
Description copied from interface:CaptchaProviderRetrieves the text content of the generated CAPTCHA.- Specified by:
getin interfaceCaptchaProvider- Returns:
- The text content of the CAPTCHA.
-
verify
Description copied from interface:CaptchaProviderVerifies if the user-provided CAPTCHA input matches the generated CAPTCHA text. It is recommended to perform a case-insensitive comparison.- Specified by:
verifyin interfaceCaptchaProvider- Parameters:
userInputCode- The CAPTCHA code entered by the user.- Returns:
trueif the user input matches the generated CAPTCHA,falseotherwise.
-
write
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
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
Description copied from interface:CaptchaProviderWrites the CAPTCHA image to the target output stream.- Specified by:
writein interfaceCaptchaProvider- 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
Gets the CAPTCHA image. Note: After using the returnedBufferedImage, you need to callImage.flush()to release resources.- Returns:
- The CAPTCHA image.
-
getImageBase64
Gets the Base64 representation of the image.- Returns:
- The Base64 of the image.
-
getImageBase64Data
Gets the Base64 of the image with the file format.- Returns:
- The Base64 of the image with the file format.
-
setFont
Sets a custom font.- Parameters:
font- The font.
-
getGenerator
Gets the CAPTCHA code generator.- Returns:
- The CAPTCHA code generator.
-
setGenerator
Sets the CAPTCHA code generator.- Parameters:
generator- The CAPTCHA code generator.
-
setBackground
Sets the background color.nullmeans 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.
-