2013-05-30 60 views
1

我为我的java应用程序使用simpleCatcha插件。生成的验证码图片不可读,所以我想更改图片样式。有什么方法可以自定义或更改图像的样式。 HTML是:在simpleCaptcha中更改图像样式

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

的web.xml是:

<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> 

和页面导入:

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

而且我已经使用this插件验证码

回答

1

如果检查源代码为nl.captcha.servlet.SimpleCaptchaServlet,width,heightFontColors已被预定义。这可以在simplecaptcha-1.1.1.jar中找到。屏幕截图下方供您参考。

SimpleCaptchaServlet

关于边缘字渲染器,这在ColoredEdgesWordRenderer类被处理&他人这确实上xBaselineyBaselineshape等一些计算并到达其应显示的验证码的字的角度。我敢打赌,要达到你想要的,你需要编辑源代码并制作一个你自己的jar并重新部署。这是因为参数不是从web.xml文件中获取的。 或者注意一些您认为更容易识别文本的其他验证码。但是,建议是,您的验证码看起来越复杂,它将添加的安全性就越高。

ColoredEdges

1

您可以用您自己的servlet并使用它覆盖SimpleCaptchaServlet。然后,你应该能够删除和改变背景或噪声或边框

实施例: .addBackground(新FlatColorBackgroundProducer(Color.LIGHT_GRAY))

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

0

如果有人仍在寻找解决方案,请像下面那样扩展SimpleCaptchaServlet,并将这个新的servlet映射到web.xml中。这适用于我

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); 
    } 

}