You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by praveenhomkar <pr...@yahoo.com> on 2011/08/15 11:48:12 UTC

Re: How to Ajax refresh captcha image (a image backed by DynamicImageResource)?

Hi I am able to reload the captcha image in the following way(Using JCaptcha)

package com.sybase365.mobiliser.web.consumer.pages.signup;
1>Write a class for dynamic image as follows.

public abstract class CaptchaImage extends NonCachingImage {
    public CaptchaImage(String id, final String challengeId) {
	super(id);
	setImageResource(new DynamicImageResource() {
	    protected byte[] getImageData() {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		String challengeId = String.valueOf(System.currentTimeMillis());
		BufferedImage challenge = getImageCaptchaService()
			.getImageChallengeForID(challengeId,
				Session.get().getLocale());
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
		try {
		    encoder.encode(challenge);
		    return os.toByteArray();
		} catch (Exception e) {
		    throw new RuntimeException(e);
		}
	    }	   	});    }
    protected abstract ImageCaptchaService getImageCaptchaService();
}

2> add it to form and provide a link to refresh it

img = generateCaptcha();// see this code in 3rd step
	form.add(img);

	form.add(new AjaxLink("captchLink") {
	    @Override
	    public void onClick(AjaxRequestTarget target) {
		// counter++;

		// target.addComponent(generateCaptcha());
		target.addComponent(form.replace(generateCaptcha()));

	    }
	});

3>public NonCachingImage generateCaptcha() {
	String challengeId = String.valueOf(System.currentTimeMillis());
	img = new CaptchaImage("captchaImage", challengeId) {
	    @Override
	    protected ImageCaptchaService getImageCaptchaService() {
		return captchaService;
	    }
	};
	img.setOutputMarkupId(true);
	return img;

    }
4> html
<p> </p>
				 # click me 


5> Don't forget to add the following in application-context.xml if u r using
spring injection

<bean
class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService"
id="imageCaptchaService"/>

For any queries mail me praveenhomkar@yahoo.com

Thanks
Praveen Homkar




--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-to-Ajax-refresh-captcha-image-a-image-backed-by-DynamicImageResource-tp1858686p3744243.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org