/*
* Copyright (c) JForum Team
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* 1) Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* 2) Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* 3) Neither the name of "Rafael Steil" nor
* the names of its contributors may be used to endorse
* or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* This file creation date: Nov 13, 2004 / 17:17:09
* The JForum Project
* http://www.jforum.net
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator;
import com.octo.captcha.component.image.fontgenerator.FontGenerator;
import com.octo.captcha.component.image.fontgenerator.TwistedAndShearedRandomFontGenerator;
import com.octo.captcha.component.image.textpaster.RandomTextPaster;
import com.octo.captcha.component.image.textpaster.TextPaster;
import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.wordgenerator.RandomWordGenerator;
import com.octo.captcha.component.wordgenerator.WordGenerator;
import com.octo.captcha.engine.image.ListImageCaptchaEngine;
import com.octo.captcha.image.ImageCaptchaFactory;
import com.octo.captcha.image.gimpy.GimpyFactory;
/**
* @author James Yong
* @version $Id: Captcha.java,v 1.13 2007/08/06 15:38:01 rafaelsteil Exp $
*/
public class Captcha extends ListImageCaptchaEngine {
private static Captcha classInstance;
private CaptchaStyle style;
public Captcha() {
this(null);
}
public Captcha(CaptchaStyle style) {
super();
this.style = style;
buildInitialFactories();
}
public static void setCaptcha(Captcha classInstance) {
Captcha.classInstance = classInstance;
}
public static Captcha getInstance() {
return classInstance;
}
public CaptchaStyle getStyle() {
return style;
}
protected void buildInitialFactories() {
if (style == null) {
style = new CaptchaStyle();
}
List<GradientBackgroundGenerator> backgroundGeneratorList = new ArrayList<GradientBackgroundGenerator>();
List<RandomTextPaster> textPasterList = new ArrayList<RandomTextPaster>();
List<TwistedAndShearedRandomFontGenerator> fontGeneratorList = new ArrayList<TwistedAndShearedRandomFontGenerator>();
int width = style.width;
int height = style.height;
int minWords = style.minWords;
int maxWords = style.maxWords;
int minFontSize = style.minFontSize;
int maxFontSize = style.maxFontSize;
backgroundGeneratorList.add(new GradientBackgroundGenerator(
new Integer(width), new Integer(height), Color.PINK,
Color.LIGHT_GRAY));
backgroundGeneratorList
.add(new GradientBackgroundGenerator(new Integer(width),
new Integer(height), Color.WHITE, Color.RED));
backgroundGeneratorList.add(new GradientBackgroundGenerator(
new Integer(width), new Integer(height), Color.ORANGE,
Color.LIGHT_GRAY));
backgroundGeneratorList.add(new GradientBackgroundGenerator(
new Integer(width), new Integer(height), Color.CYAN,
Color.LIGHT_GRAY));
// this.backgroundGeneratorList.add(new FunkyBackgroundGenerator(new
// Integer(250), new Integer(50)));
textPasterList.add(new RandomTextPaster(new Integer(minWords),
new Integer(maxWords), Color.DARK_GRAY));
textPasterList.add(new RandomTextPaster(new Integer(minWords),
new Integer(maxWords), Color.BLUE));
textPasterList.add(new RandomTextPaster(new Integer(minWords),
new Integer(maxWords), Color.GREEN));
textPasterList.add(new RandomTextPaster(new Integer(minWords),
new Integer(maxWords), Color.MAGENTA));
textPasterList.add(new RandomTextPaster(new Integer(minWords),
new Integer(maxWords), Color.BLACK));
textPasterList.add(new RandomTextPaster(new Integer(minWords),
new Integer(maxWords), Color.WHITE));
fontGeneratorList.add(new TwistedAndShearedRandomFontGenerator(
new Integer(minFontSize), new Integer(maxFontSize)));
// Create a random word generator
WordGenerator words = new RandomWordGenerator(style.charsInUse);
for (Iterator fontIter = fontGeneratorList.iterator(); fontIter
.hasNext();) {
FontGenerator fontGeny = (FontGenerator) fontIter.next();
for (Iterator backIter = backgroundGeneratorList.iterator(); backIter
.hasNext();) {
BackgroundGenerator bkgdGeny = (BackgroundGenerator) backIter
.next();
for (Iterator textIter = textPasterList.iterator(); textIter
.hasNext();) {
TextPaster textPaster = (TextPaster) textIter.next();
WordToImage word2image = new ComposedWordToImage(fontGeny,
bkgdGeny, textPaster);
// Creates a ImageCaptcha Factory
ImageCaptchaFactory factory = new GimpyFactory(words,
word2image);
// Add a factory to the gimpy list (A Gimpy is a
// ImagCaptcha)
addFactory(factory);
}
}
}
}
public void writeCaptchaImage(BufferedImage image, OutputStream os,
boolean isCloseOutputStream) throws IOException {
if (os == null || image == null) {
throw new NullPointerException();
}
try {
ImageIO.write(image, "jpg", os);
} catch (IOException ex) {
throw ex;
} finally {
if (isCloseOutputStream) {
try {
os.close();
} catch (Exception e) {
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
创建验证码工具类(源码)
共10个文件
class:3个
java:3个
prefs:1个
需积分: 3 18 下载量 88 浏览量
2009-07-09
16:08:47
上传
评论
收藏 270KB ZIP 举报
温馨提示
使用jcaptcha生成验证码图片,简化了设置。 里面的test类为示例
资源推荐
资源详情
资源评论
收起资源包目录
CreateRandomImage.zip (10个子文件)
CreateRandomImage
.project 393B
bin
test.class 1KB
Captcha.class 5KB
CaptchaStyle.class 2KB
.settings
org.eclipse.jdt.core.prefs 629B
src
CaptchaStyle.java 1KB
test.java 697B
Captcha.java 7KB
jcaptcha-all-1.0-RC2.0.1.jar 299KB
.classpath 366B
共 10 条
- 1
资源评论
THORLST
- 粉丝: 3
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功