KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > form > CmsCaptchaEngine


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/form/CmsCaptchaEngine.java,v $
3  * Date : $Date: 2006/03/27 14:52:20 $
4  * Version: $Revision: 1.5 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2004 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.frontend.templateone.form;
33
34 import org.opencms.main.OpenCms;
35
36 import java.awt.image.ImageFilter JavaDoc;
37 import java.util.Locale JavaDoc;
38
39 import com.jhlabs.image.WaterFilter;
40 import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
41 import com.octo.captcha.component.image.backgroundgenerator.FileReaderRandomBackgroundGenerator;
42 import com.octo.captcha.component.image.backgroundgenerator.UniColorBackgroundGenerator;
43 import com.octo.captcha.component.image.deformation.ImageDeformation;
44 import com.octo.captcha.component.image.deformation.ImageDeformationByFilters;
45 import com.octo.captcha.component.image.fontgenerator.FontGenerator;
46 import com.octo.captcha.component.image.fontgenerator.RandomFontGenerator;
47 import com.octo.captcha.component.image.textpaster.BaffleRandomTextPaster;
48 import com.octo.captcha.component.image.textpaster.TextPaster;
49 import com.octo.captcha.component.image.wordtoimage.DeformedComposedWordToImage;
50 import com.octo.captcha.component.image.wordtoimage.WordToImage;
51 import com.octo.captcha.component.wordgenerator.RandomWordGenerator;
52 import com.octo.captcha.component.wordgenerator.WordGenerator;
53 import com.octo.captcha.engine.image.ImageCaptchaEngine;
54 import com.octo.captcha.image.ImageCaptcha;
55 import com.octo.captcha.image.ImageCaptchaFactory;
56 import com.octo.captcha.image.gimpy.GimpyFactory;
57
58 /**
59  * A captcha engine using a Gimpy factory to create captchas.
60  * <p>
61  *
62  * @author Thomas Weckert
63  *
64  * @author Achim Westermann
65  *
66  * @version $Revision: 1.5 $
67  */

68 public class CmsCaptchaEngine extends ImageCaptchaEngine {
69
70     /** The configured image captcha factory. */
71     private ImageCaptchaFactory m_factory;
72
73     /** The settings for this captcha engine. */
74     private CmsCaptchaSettings m_settings;
75
76     /**
77      * Creates a new Captcha engine.
78      * <p>
79      *
80      * @param captchaSettings the settings to render captcha images
81      */

82     public CmsCaptchaEngine(CmsCaptchaSettings captchaSettings) {
83
84         super();
85
86         m_settings = captchaSettings;
87         initGimpyFactory();
88     }
89
90     /**
91      * @see com.octo.captcha.engine.image.ImageCaptchaEngine#getNextImageCaptcha()
92      */

93     public ImageCaptcha getNextImageCaptcha() {
94
95         return m_factory.getImageCaptcha();
96     }
97
98     /**
99      * @see com.octo.captcha.engine.image.ImageCaptchaEngine#getNextImageCaptcha(java.util.Locale)
100      */

101     public ImageCaptcha getNextImageCaptcha(Locale JavaDoc locale) {
102
103         return m_factory.getImageCaptcha(locale);
104     }
105
106     /**
107      * Sets the settings.
108      * <p>
109      *
110      * @param settings the settings to set
111      */

112     public void setSettings(CmsCaptchaSettings settings) {
113
114         m_settings = settings;
115         initGimpyFactory();
116     }
117
118     /**
119      * Initializes a Gimpy captcha factory.
120      * <p>
121      */

122     protected void initGimpyFactory() {
123
124         WaterFilter water = new WaterFilter();
125         water.setAmplitude(m_settings.getFilterAmplitude());
126         water.setAntialias(true);
127         water.setPhase(0);
128         water.setWavelength(m_settings.getFilterWaveLength());
129
130         ImageDeformation backgroundDeformation = new ImageDeformationByFilters(new ImageFilter JavaDoc[] {});
131         ImageDeformation textDeformation = new ImageDeformationByFilters(new ImageFilter JavaDoc[] {});
132         ImageDeformation postDeformation = new ImageDeformationByFilters(new ImageFilter JavaDoc[] {water});
133
134         // FileDictionnary will be renamed correctly in next release! The argument denotes a
135
// java.util.ResourceBundle properies file: toddlist.properties e.g. in root of jcaptcha jar
136
// WordGenerator dictionary = new ComposeDictionaryWordGenerator(new
137
// FileDictionnary("toddlist"));
138
WordGenerator randomWords = new RandomWordGenerator(m_settings.getCharacterPool());
139
140         TextPaster paster = new BaffleRandomTextPaster(
141             new Integer JavaDoc(m_settings.getMinPhraseLength()),
142             new Integer JavaDoc(m_settings.getMaxPhraseLength()),
143             m_settings.getFontColor(),
144             m_settings.getHolesPerGlyph(),
145             m_settings.getBackgroundColor());
146
147         BackgroundGenerator background;
148         if (m_settings.isUseBackgroundImage()) {
149             background = new FileReaderRandomBackgroundGenerator(new Integer JavaDoc(m_settings.getImageWidth()), new Integer JavaDoc(
150                 m_settings.getImageHeight()), OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebApplication(
151                 "resources/captchabackgrounds/"));
152
153         } else {
154             background = new UniColorBackgroundGenerator(new Integer JavaDoc(m_settings.getImageWidth()), new Integer JavaDoc(
155                 m_settings.getImageHeight()), m_settings.getBackgroundColor());
156         }
157
158         FontGenerator font = new RandomFontGenerator(new Integer JavaDoc(m_settings.getMinFontSize()), new Integer JavaDoc(
159             m_settings.getMaxFontSize()));
160
161         WordToImage wordToImage = new DeformedComposedWordToImage(
162             font,
163             background,
164             paster,
165             backgroundDeformation,
166             textDeformation,
167             postDeformation);
168
169         m_factory = new GimpyFactory(randomWords, wordToImage);
170     }
171
172 }
173
Popular Tags