jcaptcha 自定义验证码例子

jar包: commons-collections.jar commons-logging.jar jcaptcha-1.0-all.jar public class MyImageSingleton { private static ImageCaptchaService instance =new DefaultManageableImageCaptchaService(); static { instance = new DefaultManageableImageCaptchaService( new FastHashMapCaptchaStore(), new MyImageCaptchaEngine(), 180, 100000, 75000); } public static ImageCaptchaService getMyInstance(){ return instance; } } ======================== public class MyImageCaptchaEngine extends ListImageCaptchaEngine { // 随机生成的字符组成 private static final String RANDOM_WORD_GENERATOR = "0123456789"; // 验证码图片上显示的字符个数 private static final int TEXT_SIZE = 4; //验证码图片上显示的字符的大小设置 private static final int TEXT_WIDTH = 12; private static final int TEXT_HEIGHT = 13; // 验证码图片的大小设置 private static final int IMAGE_CAPTCHA_WIDTH = 50; private static final int IMAGE_CAPTCHA_HEIGHT = 20; // protected void buildInitialFactories() { // // 随机生成的字符 // WordGenerator wgen = new RandomWordGenerator(RANDOM_WORD_GENERATOR); // RandomRangeColorGenerator cgen = new RandomRangeColorGenerator( // new int[]{ 0, 100 }, new int[]{ 0, 100 }, new int[]{ 0, 100 }); // // 文字显示的个数 // TextPaster textPaster = new RandomTextPaster(new Integer(TEXT_SIZE), // new Integer(TEXT_SIZE), cgen, Boolean.TRUE); // // 图片的大小 // BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator( // new Integer(IMAGE_CAPTCHA_WIDTH), new Integer( // IMAGE_CAPTCHA_HEIGHT)); // // 字体格式 // Font[] fontsList = new Font[]{ new Font("Arial", 0, 12), // new Font("Tahoma", 0, 12), new Font("Verdana", 0, 12), }; // // 文字的大小 // FontGenerator fontGenerator = new RandomFontGenerator(new Integer( // TEXT_WIDTH), new Integer(TEXT_HEIGHT), fontsList); // // WordToImage wordToImage = new ComposedWordToImage(fontGenerator, // backgroundGenerator, textPaster); // this.addFactory(new GimpyFactory(wgen, wordToImage)); // } protected void buildInitialFactories() { WordGenerator wgen = new RandomWordGenerator("abcdefghijklmnopquvwxyz123456789"); RandomRangeColorGenerator cgen = new RandomRangeColorGenerator( new int[] {0, 100}, new int[] {0, 100}, new int[] {0, 100}); //文字显示4个数 TextPaster textPaster = new RandomTextPaster(new Integer(4), new Integer(4), cgen, true); //图片的大小 BackgroundGenerator backgroundGenerator = new FunkyBackgroundGenerator(new Integer(200), new Integer(45)); Font[] fontsList = new Font[] { new Font("Arial", 0, 10), new Font("Tahoma", 0, 10), new Font("Verdana", 0, 10), }; FontGenerator fontGenerator = new RandomFontGenerator(new Integer(20), new Integer(30), fontsList); WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster); this.addFactory(new GimpyFactory(wgen, wordToImage)); } ================================== public class MyImageServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) 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 = request.getSession().getId(); // call the ImageCaptchaService getChallenge method BufferedImage challenge = MyImageSingleton.getMyInstance().getImageChallengeForID(captchaId, request.getLocale()); // a jpeg encoder JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream); jpegEncoder.encode(challenge); } catch (IllegalArgumentException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } catch (CaptchaServiceException e) { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } captchaChallengeAsJpeg = jpegOutputStream.toByteArray(); // flush it in the response response.setHeader("Cache-Control", "no-store"); response.setHeader("Pragma", "no-cache"); response.setDateHeader("Expires", 0); //输出JPEG response.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = response.getOutputStream(); responseOutputStream.write(captchaChallengeAsJpeg); responseOutputStream.flush(); responseOutputStream.close(); } =========================== web xml <servlet> <servlet-name>MyImageServlet</servlet-name> <servlet-class>com.lch.test.checkimg.MyImageServlet</servlet-class> </servlet> <servlet> <servlet-name>MyCheckImg</servlet-name> <servlet-class>com.lch.test.checkimg.MyCheckImg</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyImageServlet</servlet-name> <url-pattern>/MyImageServlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyCheckImg</servlet-name> <url-pattern>/MyCheckImg</url-pattern> </servlet-mapping> ========================jsp页面 <center> <form action="MyCheckImg" method="post"> <img src="MyImageServlet" mce_src="MyImageServlet" /> <input type="text" name="myword" value="" /> <input type="submit"/> </form> <center> ======================提交页面验证Servlet public class MyCheckImg extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try{ String sessionID=request.getSession().getId(); System.out.println(sessionID); String myword=request.getParameter("myword"); System.out.println(myword); PrintWriter pw= response.getWriter(); StringBuffer sb=new StringBuffer(); ///check boolean flg=CaptchaServiceSingleton.getInstance().validateResponseForID(sessionID, myword); if(flg){ sb.append("<font color='red'>check success!</font>"); System.out.println("验证成功!"); }else{ sb.append("<font color='red'>check lose!</font>"); System.out.println("验证失败!"); } pw.print(sb.toString()); pw.close(); }catch(Exception e){ System.out.println(); } }

你可能感兴趣的:(image,String,Integer,Random,Class,generator)