Captcha API

Package Overview

The library is organised under the base package com.onixbyte.captcha with the following structure:

PackageDescription
com.onixbyte.captchaCore interfaces
com.onixbyte.captcha.implDefault implementations
com.onixbyte.captcha.textText producer and renderer interfaces
com.onixbyte.captcha.text.implText producer and renderer impls
com.onixbyte.captcha.text.enumsText-related enums
com.onixbyte.captcha.backgroundBackground producer interface
com.onixbyte.captcha.background.implBackground producer implementations
com.onixbyte.captcha.noiseNoise producer interface
com.onixbyte.captcha.noise.implNoise producer implementations
com.onixbyte.captcha.gimpyDistortion engine interface
com.onixbyte.captcha.gimpy.implDistortion engine implementations

Producer

The top-level interface responsible for creating CAPTCHA images with text drawn on them.

Producer captcha = DefaultCaptchaProducer.builder()
    .textProducer(textProducer)
    .wordRenderer(wordRenderer)
    .gimpyEngine(gimpyEngine)
    .backgroundProducer(backgroundProducer)
    .width(200)
    .height(50)
    .borderDrawn(true)
    .borderColour(Color.BLACK)
    .borderThickness(1)
    .build();

String text = captcha.createText();
BufferedImage image = captcha.createImage(text);

DefaultCaptchaProducer Builder

MethodDefault ValueDescription
textProducerDefaultTextProducer.builder().build()The text producer to use
wordRendererDefaultWordRenderer.builder().build()The word renderer to use
gimpyEngineWaterRipple.builder().build()The distortion engine to use
backgroundProducerDefaultBackgroundProducer.builder().build()The background producer to use
width200Width of the CAPTCHA image
height50Height of the CAPTCHA image
borderDrawntrueWhether a border is drawn
borderColourColor.BLACKThe colour of the border
borderThickness1The thickness of the border

Text

TextProducer

Interface for creating CAPTCHA text strings.

public interface TextProducer {
    String getText();
}

DefaultTextProducer

Generates random text with configurable length and character set.

DefaultTextProducer textProducer = DefaultTextProducer.builder()
    .length(6)                                    // default: 6
    .chars("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray()) // default: a-z, A-Z, 0-9
    .build();
MethodDefault ValueDescription
length6Length of generated text
charsa-z, A-Z, 0-9 (62 characters)Characters used for generation

WordRenderer

Interface for rendering text onto an image.

public interface WordRenderer {
    BufferedImage renderWord(String word, int width, int height);
}

DefaultWordRenderer

Renders text with configurable font, colour, and spacing.

DefaultWordRenderer wordRenderer = DefaultWordRenderer.builder()
    .fontSize(40)                          // default: 40
    .fonts("Arial", "Courier")             // default: Arial, Courier
    .fontColour(Color.BLACK)               // default: Color.BLACK
    .charSpace(2)                          // default: 2
    .fontStyle(FontStyle.BOLD)             // default: FontStyle.BOLD
    .build();
MethodDefault ValueDescription
fontSize40Font size in pixels
fonts["Arial", "Courier"]Font families to use (randomly chosen)
fontColourColor.BLACKFont colour
charSpace2Space between characters in pixels
fontStyleFontStyle.BOLDFont style to apply

FontStyle

Defines the supported font styles for rendering CAPTCHA text.

| Value | Corresponding AWT Constant | Description | | ------------- | -------------------------- | ------------ | ------------------------ | | PLAIN | java.awt.Font.PLAIN | Plain style | | BOLD | java.awt.Font.BOLD | Bold style | | ITALIC | java.awt.Font.ITALIC | Italic style | | BOLD_ITALIC | java.awt.Font.BOLD | Font.ITALIC | Bold and italic combined |


GimpyEngine

Interface for applying image distortion effects.

public interface GimpyEngine {
    BufferedImage getDistortedImage(BufferedImage baseImage);
}

WaterRipple

Applies a water ripple distortion effect. Extends AbstractGimpyEngine and adds noise automatically.

WaterRipple gimpy = WaterRipple.builder()
    .noiseProducer(DefaultNoiseProducer.builder().build())  // default: DefaultNoiseProducer
    .build();
MethodDefault ValueDescription
noiseProducerDefaultNoiseProducer.builder().build()The noise producer to use

FishEyeGimpy

Applies a fish-eye distortion effect with horizontal and vertical lines over the image.

FishEyeGimpy gimpy = FishEyeGimpy.builder()
    .build();

ShadowGimpy

Applies a shadow and ripple effect. Extends AbstractGimpyEngine and adds noise automatically.

ShadowGimpy gimpy = ShadowGimpy.builder()
    .noiseProducer(DefaultNoiseProducer.builder().build())  // default: DefaultNoiseProducer
    .build();
MethodDefault ValueDescription
noiseProducerDefaultNoiseProducer.builder().build()The noise producer to use

Noise

NoiseProducer

Interface for adding noise to an image.

public interface NoiseProducer {
    void makeNoise(BufferedImage image, float factorOne, float factorTwo,
                   float factorThree, float factorFour);
}

DefaultNoiseProducer

Adds noise curves with configurable colour.

DefaultNoiseProducer noise = DefaultNoiseProducer.builder()
    .noiseColour(Color.BLACK) // default: Color.BLACK
    .build();
MethodDefault ValueDescription
noiseColourColor.BLACKThe noise colour

NoNoiseProducer

A no-op implementation that does not add any noise to the image.

NoiseProducer noise = NoNoiseProducer.builder()
    .build();

Background

BackgroundProducer

Interface for adding background to an image.

public interface BackgroundProducer {
    BufferedImage addBackground(BufferedImage image);
}

DefaultBackgroundProducer

Creates a gradient background with configurable start and end colours.

DefaultBackgroundProducer background = DefaultBackgroundProducer.builder()
    .colourFrom(Color.WHITE)       // default: Color.LIGHT_GRAY
    .colourTo(Color.LIGHT_GRAY)    // default: Color.WHITE
    .build();
MethodDefault ValueDescription
colourFromColor.LIGHT_GRAYThe starting colour of gradient
colourToColor.WHITEThe ending colour of gradient