Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

my captcha font and background are not good

 
Ramkumar Subburaj
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can aybody tell how to change font and background? i got captcha but font and back ground are not good .if any body have source code please give to me.


my source code is

ImageCaptchaServlet.java is

package slj.members.servlet;
import com.octo.captcha.service.CaptchaServiceException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


public class ImageCaptchaServlet extends HttpServlet {


public void init(ServletConfig servletConfig) throws ServletException {

super.init(servletConfig);

}
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {

byte[] captchaChallengeAsJpeg = null;
// the output stream to render the captcha image as jpeg into
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
try {
// get the session id that will identify the generated captcha.
//the same id must be used to validate the response, the session id is a good candidate!
String captchaId = httpServletRequest.getSession().getId();
// call the ImageCaptchaService getChallenge method
BufferedImage challenge =
CaptchaServiceSingleton.getInstance().getImageChallengeForID(captchaId,
httpServletRequest.getLocale());

// a jpeg encoder
JPEGImageEncoder jpegEncoder =
JPEGCodec.createJPEGEncoder(jpegOutputStream);
jpegEncoder.encode(challenge);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (CaptchaServiceException e) {
httpServletResponse.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

// flush it in the response
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream =
httpServletResponse.getOutputStream();
responseOutputStream.write(captchaChallengeAsJpeg);
responseOutputStream.flush();
responseOutputStream.close();
}
}


CaptchaServiceSingleton.java is


package slj.members.servlet;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.octo.captcha.service.image.DefaultManageableImageCaptchaService;

public class CaptchaServiceSingleton {

private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();

public static ImageCaptchaService getInstance(){
return instance;
}
}
<tr>
<td align="left" valign="middle" >Enter these letters:<br/>
<img src="../../jcaptcha" align="middle" alt="Enter the characters appearing in this image" border="1"/>
</td>
<td align="left" valign="middle"><input type="text" name="txtverificationcode" class="textbox2" value="" maxlength="50" ></td>
</tr>


web.xml

<servlet>
<servlet-name>jcaptcha</servlet-name>
<servlet-class>slj.members.servlet.ImageCaptchaServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>jcaptcha</servlet-name>
<url-pattern>/jcaptcha</url-pattern>
</servlet-mapping>
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your singleton (CaptchaServiceSingleton in your case), you define a custom ImageCaptchaEngine of some sort. We extend ListImagecaptchaEngine and provide our own settings. See the API for more options.



Note this isn't the most secure, but is the best our users are able to manage.

Once you have your engine, you plug it into your Engine in a static initialiser:
 
Ps Se
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class CaptchaServiceSingleton {
private static ImageCaptchaService instance = new DefaultManageableImageCaptchaService();
public static ImageCaptchaService getInstance(){
instance.setCaptchaEngine(new MyCaptchaEngine());
return instance;
}
}

I tried the above hint. But setCaptchaEngine is not a method of DefaultManageableImageCaptchaService.
Is there any other way to change the font properties etc without using spring config
Can someone help with it.
Appreciate your help!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the API disagrees, what version are you using?
There should be both setCaptchaEngine() and setCaptchaEngineClass()
http://www.docjar.com/docs/api/com/octo/captcha/service/image/DefaultManageableImageCaptchaService.html
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic