jfiedler
Joined: 24/01/2013 12:17:11
Messages: 1
Offline
|
I used simplecaptcha-1.2.1.jar
see http://simplecaptcha.sourceforge.net/index.html
#################### XML ###############################
<t:row id="g_49" rendered="#{d.CreateAccountUI.renderCaptcha}" >
<t:coldistance id="g_50" width="154" />
<t:image id="g_51" image="#{d.CreateAccountUI.captchaImage}" width="150" />
<t:link id="g_52" actionListener="#{d.CreateAccountUI.onRefreshCaptcha}" image="/images/icons/refresh.png" />
<t:coldistance id="g_53" width="30" />
<t:image id="g_54" image="/images/icons/key.png" />
<t:coldistance id="g_55" width="10" />
<t:field id="g_56" bgpaint="#{d.CreateAccountUI.bgpaintCaptchaCode}" flush="true" flushtimer="500" text="#{d.CreateAccountUI.captchaCode}" width="220" />
<t:coldistance id="g_57" width="100%" />
</t:row>
####################### UI ###############################
public CreateAccountUI( )
{
createNewCaptcha( );
setInfoTextPasswordLengthOK( false );
}
private void createNewCaptcha( )
{
CaptchaTool captchaTool = new CaptchaTool( );
byte[] imageBytes = captchaTool.getImageHexCode( );
m_captchaImage = "/hex(" + ValueManager.encodeHexString( imageBytes ) + ")";
m_captcha = captchaTool.getCaptcha( );
setCaptchaCode( null );
}
############### CaptchaTool ################################
public class CaptchaTool
{
private char[] srcChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'!', '?', '$', '&', '#', '+', '%',
};
private List<java.awt.Color> textColors = Arrays.asList(Color.GRAY, Color.BLUE);
private List<java.awt.Font> textFonts = Arrays.asList( new Font("Arial", Font.ITALIC, 30),
new Font("Courier", Font.ITALIC, 30),
new Font("Arial", Font.BOLD, 30),
new Font("Courier", Font.BOLD, 30),
new Font("Arial", Font.PLAIN, 30),
new Font("Courier", Font.PLAIN, 30)
);
private java.awt.Color backgroundColor = Color.WHITE;
private Captcha.Builder captchaBuilder;
private Captcha captcha;
/**
*
*/
public CaptchaTool( )
{
// http://simplecaptcha.sourceforge.net/index.html
// http://simplecaptcha.sourceforge.net/javadocs/index.html
captchaBuilder = new Captcha.Builder(120, 40);
DefaultTextProducer defaultTextProducer = new DefaultTextProducer( 6, srcChars );
FlatColorBackgroundProducer bgProducer = new FlatColorBackgroundProducer(backgroundColor);
DefaultWordRenderer wordRenderer = new DefaultWordRenderer(textColors, textFonts);
NoiseProducer noiseProducer = new CurvedLineNoiseProducer( Color.GRAY, 2 );
//NoiseProducer noiseProducer = new StraightLineNoiseProducer( Color.GRAY, 2 );
//GimpyRenderer gimpyRenderer = new BlockGimpyRenderer( 20 );
//GimpyRenderer gimpyRenderer = new DropShadowGimpyRenderer( 10, 10 );
//GimpyRenderer gimpyRenderer = new FishEyeGimpyRenderer( Color.BLUE, Color.BLACK );
//GimpyRenderer gimpyRenderer = new FishEyeGimpyRenderer( );
//GimpyRenderer gimpyRenderer = new RippleGimpyRenderer( );
//GimpyRenderer gimpyRenderer = new ShearGimpyRenderer( );
GimpyRenderer gimpyRenderer = new StretchGimpyRenderer( 1.04d, 1.0d );
captchaBuilder.addText( defaultTextProducer, wordRenderer );
captchaBuilder.addBackground( bgProducer );
// Add noise using the default NoiseProducer (a CurvedLineNoiseProducer).
captchaBuilder.addNoise( noiseProducer );
// Gimp the image using the default GimpyRenderer (a RippleGimpyRenderer).
captchaBuilder.gimp( gimpyRenderer );
captcha = captchaBuilder.build( );
}
public byte[] getImageHexCode ( )
{
byte[] imageInByte = null;
BufferedImage buffImage = captcha.getImage( );
ByteArrayOutputStream baos = new ByteArrayOutputStream( );
try
{
ImageIO.write( buffImage, "jpg", baos );
baos.flush( );
imageInByte = baos.toByteArray( );
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageInByte;
}
/**
* @return
*/
public Captcha getCaptcha( )
{
return captcha;
}
############## Have a lot of fun! ###################
|