com.github.cage.image
Class Painter

java.lang.Object
  extended by com.github.cage.image.Painter

public class Painter
extends Object

This class does most of the captcha drawing. This class is thread safe.

Author:
akiraly

Nested Class Summary
static class Painter.Quality
          Enumeration for different image quality levels.
 
Field Summary
static int DEFAULT_HEIGHT
          Default image height.
static int DEFAULT_WIDTH
          Default image width.
 
Constructor Summary
Painter()
          Constructor.
Painter(int width, int height, Color bGround, Painter.Quality quality, EffectConfig effectConfig, Random rnd)
          Constructor.
Painter(Random rnd)
          Constructor.
 
Method Summary
protected  Graphics2D configureGraphics(Graphics g, Font font, Color fGround)
          Configures graphics object before drawing text on it.
protected  void configureGraphicsQuality(Graphics2D g2)
          Sets quality related hints based on the quality field of this object.
protected  BufferedImage createImage()
          Creates a new image to draw upon.
 BufferedImage draw(Font font, Color fGround, String text)
          Generates a new captcha image.
protected  void draw(Graphics2D g, String text)
          Does some of the text transformation (calls transform(Graphics2D, String, GlyphVector)), scales, transforms and draws the result (also the outline if needed).
protected  void fillBlurArray(float[] array)
          Generates a random probability distribution.
 Color getBackground()
           
 EffectConfig getEffectConfig()
           
 int getHeight()
           
 Painter.Quality getQuality()
           
 int getWidth()
           
protected  BufferedImage postProcess(BufferedImage img)
          Does some post processing on the generated image if needed.
protected  void transform(Graphics2D g, String text, GlyphVector v)
          Does some of the text transformation (like rotation and symbol crowding).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_WIDTH

public static final int DEFAULT_WIDTH
Default image width.

See Also:
Constant Field Values

DEFAULT_HEIGHT

public static final int DEFAULT_HEIGHT
Default image height.

See Also:
Constant Field Values
Constructor Detail

Painter

public Painter()
Constructor.


Painter

public Painter(Random rnd)
Constructor.

Parameters:
rnd - random generator to be used, can be null

Painter

public Painter(int width,
               int height,
               Color bGround,
               Painter.Quality quality,
               EffectConfig effectConfig,
               Random rnd)
Constructor.

Parameters:
width - captcha image width, default DEFAULT_WIDTH
height - captcha image height, default DEFAULT_HEIGHT
bGround - background color of captcha image, default white, can be null
quality - captcha image quality, default Painter.Quality.MAX, should use max it does not have measurable speed penalty on modern jvm-s (1.6u23), can be null
effectConfig - used to define what effects should be used, can be null
rnd - random generator to be used, can be null
Method Detail

draw

public BufferedImage draw(Font font,
                          Color fGround,
                          String text)
Generates a new captcha image.

Parameters:
font - will be used for text, not null
fGround - will be used for text, not null
text - this will be rendered on the image, not null, not 0 length
Returns:
the generated image

createImage

protected BufferedImage createImage()
Creates a new image to draw upon.

Returns:
new image, not null

configureGraphics

protected Graphics2D configureGraphics(Graphics g,
                                       Font font,
                                       Color fGround)
Configures graphics object before drawing text on it.

Parameters:
g - to be configured must be an instance of Graphics2D, not null
font - to be used for the text, not null
fGround - to be used for the text, not null
Returns:
g casted to Graphics2D or throws exception if g is not instance of Graphics2D.

configureGraphicsQuality

protected void configureGraphicsQuality(Graphics2D g2)
Sets quality related hints based on the quality field of this object.

Parameters:
g2 - to be configured, not null

draw

protected void draw(Graphics2D g,
                    String text)
Does some of the text transformation (calls transform(Graphics2D, String, GlyphVector)), scales, transforms and draws the result (also the outline if needed).

Parameters:
g - to be drawn upon
text - to be drawn

transform

protected void transform(Graphics2D g,
                         String text,
                         GlyphVector v)
Does some of the text transformation (like rotation and symbol crowding).

Parameters:
g - to be drawn upon
text - to be drawn
v - graphical representation of text, to be transformed

postProcess

protected BufferedImage postProcess(BufferedImage img)
Does some post processing on the generated image if needed. Like rippling (waving) and blurring.

Parameters:
img - to be post prosessed.
Returns:
the finished image, maybe the same as the input

fillBlurArray

protected void fillBlurArray(float[] array)
Generates a random probability distribution. Used by blurring.

Parameters:
array - filled with random values. The values in array sum up to 1.

getWidth

public int getWidth()
Returns:
width of the image

getHeight

public int getHeight()
Returns:
height of the image

getBackground

public Color getBackground()
Returns:
background color of the image, not null

getQuality

public Painter.Quality getQuality()
Returns:
quality level of the image, not null

getEffectConfig

public EffectConfig getEffectConfig()
Returns:
configuration for effects, not null


Copyright © 2011. All Rights Reserved.