home configuration implementation extending download about


extend:

background
text
distortion
rendering


creating your own textproducer.

What is a textproducer ? Well the text producer produces the captcha text that is eventually rendered by the word renderer. The default implementation creates a random string from a character array.



Creating your own text producer should be really simple. It can be done by either directly implementing the interface nl.captcha.text.TextProducer or by extending the nl.captcha.text.imp.DefaultTextCreator class. And overriding the getText() method.

Lets just work with an example. Lets say you are Chinese and want to provide your users with a Chinese capcha text (thus limiting the users of your site to Chinese nationals in stead of limiting it to people who can read us-ascii).

note 1: the Chinese chars might not render properly if you don't' have proper fonts installed.
note 2: deformation might not be a good idea because it can change the characters meaning

package nl.captcha.sandbox;

import nl.captcha.text.TextProducer;
import nl.captcha.text.imp.DefaultTextCreator;

/**
 *
@author you
 *
 *
TextProducer Implementation that will return chinese characters..
 *
*/

public class ChineseTextProducer extends DefaultTextCreator {

          private String[] simplfiedC = new String[] {

                  "包括焦点","新道消点","服分目搜","索姓名電",
                  "子郵件信","主旨請回","電子郵件","給我所有",
                  "討論區明","發表新文","章此討論","區所有文",
                  "章回主題","樹瀏覽搜"
         };

         public String getText(){
                  return simplfiedC[generator.nextInt(simplfiedC.length) + 1];
         }

}



In order to make this work you need a system font that is able to display simplified Chinese characters. And you are going to have to save your source code in utf-8 encoding. Otherwise you will get question marks instead of Chinese chars.

Now for configuration:

<!--change the cap.text.producer from the default to your to class you just created-->
<
init-param>
         <param-name>cap.text.producer</param-name>
         <param-value>nl.captcha.sandbox.ChineseTextProducer</param-value>
         <!--<param-value>nl.captcha.text.imp.FiveLetterFirstNameTextCreator</param-value>-->
</init-param>

<!--make sure you can reder your nice chinese characters and specify a font that can-->
<
init-param>
         <param-name>cap.font.arr</param-name>
         <param-value>Kochi Mincho</param-value>
         <!--<param-value>Arial,Helvetica,Courier,TimesRoman</param-value>-->
</init-param>

 

 

 



 

 

 

SourceForge Logo