com.github.cage
Class Cage

java.lang.Object
  extended by com.github.cage.Cage
Direct Known Subclasses:
GCage, YCage

public class Cage
extends Object

Convenient entry class to control captcha generation. This class is thread safe. Example usage:

 
 Cage cage = new GCage(); // lets make some "G" template captchas
 // use new YCage() for "Y" template or configure it yourself with
 // one of the Cage constructors.
 
 // ...
 
 String token1 = cage.getTokenGenerator().next();
 String token2 = cage.getTokenGenerator().next();
 
 cage.draw(token1, someOutputstream1);
 cage.draw(token2, someOutputstream2);
 
 
 

Author:
akiraly

Field Summary
static Float DEFAULT_COMPRESS_RATIO
          Default compress ratio for image encoders.
static String DEFAULT_FORMAT
          Default image encoding format.
 
Constructor Summary
Cage()
          Default constructor.
Cage(Painter painter, IGenerator<Font> fonts, IGenerator<Color> foregrounds, String format, Float compressRatio, IGenerator<String> tokenGenerator, Random rnd)
          Constructor.
 
Method Summary
 byte[] draw(String text)
          Generate an image and return it in a byte array.
 void draw(String text, OutputStream ostream)
          Generate an image and serialize it to the output.
 BufferedImage drawImage(String text)
          Generates a captcha image.
 Float getCompressRatio()
           
 IGenerator<Font> getFonts()
           
 IGenerator<Color> getForegrounds()
           
 String getFormat()
           
 Painter getPainter()
           
 IGenerator<String> getTokenGenerator()
           
protected  void serialize(BufferedImage img, OutputStream ostream)
          Serializes an image to an OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_COMPRESS_RATIO

public static final Float DEFAULT_COMPRESS_RATIO
Default compress ratio for image encoders.


DEFAULT_FORMAT

public static final String DEFAULT_FORMAT
Default image encoding format.

See Also:
Constant Field Values
Constructor Detail

Cage

public Cage()
Default constructor. Calls Cage(Painter, IGenerator, IGenerator, String, Float, IGenerator, Random)


Cage

public Cage(Painter painter,
            IGenerator<Font> fonts,
            IGenerator<Color> foregrounds,
            String format,
            Float compressRatio,
            IGenerator<String> tokenGenerator,
            Random rnd)
Constructor.

Parameters:
painter - to be used for painting, can be null
fonts - generator used to generate fonts for texts, defaults to a random chooser from some predefined set of fonts, can be null
foregrounds - generator used to generate colors for texts, defaults to a random "not-bright-so-it-is-readable-on-white" color generator, can be null
format - output format, default "jpeg", can be null
compressRatio - a number in [0f, 1f] interval if compression should be used with the output format. The format must support compression (like jpeg and png). If null no compression is done.
tokenGenerator - a custom String token generator, can be null. If null is passed a default is created. It is not used by Cage it is only stored for convenience. Can be retrieved by getTokenGenerator().
rnd - random generator to be used, can be null
Method Detail

draw

public void draw(String text,
                 OutputStream ostream)
          throws IOException
Generate an image and serialize it to the output. This method can call OutputStream.close() on the supplied output stream.

Parameters:
text - to be drawn on the image
ostream - captcha image is serialized to this
Throws:
IOException - if IO error occurs.

draw

public byte[] draw(String text)
Generate an image and return it in a byte array.

Parameters:
text - to be drawn on the image
Returns:
byte array holding the serialized generated image

drawImage

public BufferedImage drawImage(String text)
Generates a captcha image.

Parameters:
text - to be drawn
Returns:
generated image

serialize

protected void serialize(BufferedImage img,
                         OutputStream ostream)
                  throws IOException
Serializes an image to an OutputStream. This method can call OutputStream.close() on the supplied output stream.

Parameters:
img - to be serialized
ostream - to be written to
Throws:
IOException - if IO error occurs.

getPainter

public Painter getPainter()
Returns:
object used to draw the image, not null

getFonts

public IGenerator<Font> getFonts()
Returns:
font generator used to choose a font, not null

getForegrounds

public IGenerator<Color> getForegrounds()
Returns:
foreground generator used to choose a text color, not null

getFormat

public String getFormat()
Returns:
used image encoding format, like "jpeg", not null

getCompressRatio

public Float getCompressRatio()
Returns:
compress ratio used by image encoding, can be null

getTokenGenerator

public IGenerator<String> getTokenGenerator()
Returns:
token generator to produce strings for the image, not null


Copyright © 2011. All Rights Reserved.