Puneet Purohit
Puneet Purohit

Reputation: 1281

Change image style in simpleCaptcha

I am using simpleCatcha plugin for my java application. The captcha image that is generating is no readable so I want to change the image style. Is there any way to customize or change the style of the image. HTML is :

<img id="captcha" src="<c:url value="simpleCaptcha.jpg" />" width="150">

web.xml is :

<display-name>captcha</display-name>
<servlet>
    <servlet-name>SimpleCaptcha</servlet-name>
    <servlet-class>nl.captcha.servlet.SimpleCaptchaServlet</servlet-class>      
</servlet>
<servlet-mapping>
    <servlet-name>SimpleCaptcha</servlet-name>
    <url-pattern>/simpleCaptcha.jpg</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>simpleCaptcha.jsp</welcome-file>
</welcome-file-list>

And page import is :

<%@ page import="nl.captcha.Captcha"%>

And I have used this plugin for captcha

Upvotes: 1

Views: 4025

Answers (3)

Sharan Rajendran
Sharan Rajendran

Reputation: 3609

You can override the SimpleCaptchaServlet using your own servlet and use that instead. Then you should be able to remove and change the background or noise or borders

Example:

.addBackground(new FlatColorBackgroundProducer(Color.LIGHT_GRAY)) 

or

GradiatedBackgroundProducer bg = new GradiatedBackgroundProducer();
bg.setFromColor(Color.white);
bg.setToColor(Color.yellow);
.addBackground(bg)

Upvotes: 1

Gouranga Tarafder
Gouranga Tarafder

Reputation: 354

If someone is still looking for a solution, extend SimpleCaptchaServlet like below and map this new servlet in web.xml. This works for me

public class MySimpleCaptcha extends SimpleCaptchaServlet {

    private static final String PARAM_HEIGHT = "height";
    private static final String PARAM_WIDTH = "width";
    protected int _width = 200;
    protected int _height = 50;

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Captcha captcha = new Captcha.Builder(_width, _height)
            .addText()
            .addBackground(new GradiatedBackgroundProducer())

            // Add here whatever you need

            .addNoise()
            .gimp(new DropShadowGimpyRenderer())
            .addBorder()
            .build();

        CaptchaServletUtil.writeImage(resp, captcha.getImage());
        req.getSession().setAttribute(NAME, captcha);
     }

 }

Upvotes: 0

Vikas V
Vikas V

Reputation: 3196

If you check the source code for nl.captcha.servlet.SimpleCaptchaServlet, width , height and FontColors have been predefined. This can be found in simplecaptcha-1.1.1.jar. Below screen shot for your reference.

SimpleCaptchaServlet

Regarding the edges word renderers, this is handled in ColoredEdgesWordRenderer class & others which does some calculation on xBaseline, yBaseline , shape etc and arrives an angle at which the words of the captcha should be shown.

My bet is to achieve what you want, you either need to edit the source code and make a jar of your own and redeploy. This is because parameters are not taken from web.xml file. Or look out for some other captcha code which you think is easier to identify text. But, suggestion is, the more complicated your captcha looks, the more security it will add.

ColoredEdges

Upvotes: 1

Related Questions