The Captcha Generator
is a utility for generating captcha images with customizable configurations. Captchas are commonly used to prevent automated bots from accessing or submitting data on web forms. This class provides the functionality to generate captcha images with random codes, random fonts, and noise to increase security and make it challenging for bots to decipher.
Add the following dependency to your pom.xml
file:
<dependency>
<groupId>com.mewebstudio</groupId>
<artifactId>captcha</artifactId>
<version>0.1.3</version>
</dependency>
Add the following dependency to your build.gradle
file:
implementation 'com.mewebstudio:captcha:0.1.3'
To use the Captcha
class to generate captcha images, follow these steps:
-
Instantiate the Captcha:
You can create an instance of the
Captcha
class with or without a custom configuration. If you want to use the default configuration, create an instance without any arguments:Captcha captcha = new Captcha();
If you want to use a custom configuration, create a
Config
instance and pass it as an argument to the constructor:Config customConfig = new Config(); // Customize the configuration properties as needed customConfig.setFonts(new String[]{ "src/main/resources/fonts/Roboto-Regular.ttf", "src/main/resources/fonts/OpenSans-Regular.ttf", "src/main/resources/fonts/Lato-Regular.ttf" }); customConfig.setDark(true); Captcha captcha = new Captcha(customConfig);
or
captcha.getConfig().setFonts(new String[]{ "src/main/resources/fonts/Roboto-Regular.ttf", "src/main/resources/fonts/OpenSans-Regular.ttf", "src/main/resources/fonts/Lato-Regular.ttf" }); captcha.getConfig().setDark(true);
-
Generate a Captcha:
To generate a captcha image, call the
generate()
method on theCaptcha
instance:GeneratedCaptcha generatedCaptcha = captcha.generate(); BufferedImage captchaImage = generatedCaptcha.getImage(); String captchaCode = generatedCaptcha.getCode();
The
generate()
method returns aGeneratedCaptcha
object containing the captcha image (BufferedImage
) and the corresponding captcha code (String
). -
Display the Captcha:
You can use the generated captcha image in your application to display it to users. For example, in a web application, you can embed the image in an HTML page.
-
Verify the Captcha Response:
When the user submits the form or action that contains the captcha, you need to verify the user's response with the captcha code. Compare the captcha code entered by the user with the actual captcha code to validate whether the user is human:
String userEnteredCode = "ABC123"; // Replace this with the user's entered captcha code if (userEnteredCode.equals(captchaCode)) { // Captcha code matches, user is verified // Proceed with the desired action } else { // Captcha code does not match, display an error message or take appropriate action }
The Captcha
class allows you to customize various aspects of the generated captcha images through the Config
class:
-
Width and Height: Set the dimensions of the captcha image using
config.setWidth(int width)
andconfig.setHeight(int height)
. -
Code Length: Adjust the length of the captcha code with
config.setLength(int length)
. -
Noise: Add noise to the captcha image to make it more challenging for bots to interpret. You can control the noise level with
config.setNoise(int noise)
. -
Dark or Light Mode: Choose between dark or light mode for the captcha image. Set the mode with
config.setDark(boolean dark)
. -
Font Styles and Fonts: Customize the font styles and fonts used in the captcha image. You can set custom font files using
config.setFonts(String[] fonts)
. -
Color Palette: Define the color palette for the captcha image in both dark and light modes. Adjust the palettes with
config.setLightPalette(Color[] palettes)
andconfig.setDarkPalette(Color[] palette)
. -
Background Color: Set the background color of the captcha image for both dark and light modes. Use
config.setLightBackgroundColor(Color color)
andconfig.setDarkBackgroundColor(Color color)
.
The Captcha
class may throw a FontLoadException
if it fails to load custom fonts specified in the configuration. Handle this exception appropriately to handle font loading failures.
The Captcha
class does not have any external dependencies other than standard Java libraries.
Here's a basic example of how to use the Captcha
class to generate and display a captcha image in a Java application:
public class CaptchaExample {
public static void main(String[] args) {
// Create a new Captcha instance
Captcha captcha = new Captcha();
captcha.getConfig().setDark(true);
// Generate a captcha image
GeneratedCaptcha generatedCaptcha = captcha.generate();
BufferedImage captchaImage = generatedCaptcha.getImage();
String captchaCode = generatedCaptcha.getCode();
// Display the captcha code and image (In a real application, you would display the image in a GUI or web page)
System.out.println("Captcha Code: " + captchaCode);
System.out.println("Captcha Image: " + captchaImage);
// In a real application, you would provide the captcha image to users for validation
// Users will enter the captcha code, and you can verify it with the generated captcha code.
}
}
This is a basic overview of the Captcha Generator
License - MIT