?? mvncaptchaservice.java
字號:
/*
* $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/MVNCaptchaService.java,v 1.2 2004/03/09 18:31:18 minhnn Exp $
* $Author: minhnn $
* $Revision: 1.2 $
* $Date: 2004/03/09 18:31:18 $
*
* ====================================================================
*
* Copyright (C) 2002-2004 by MyVietnam.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
*
* All copyright notices regarding mvnForum MUST remain intact
* in the scripts and in the outputted HTML.
* The "powered by" text/logo with a link back to
* http://www.mvnForum.com and http://www.MyVietnam.net in the
* footer of the pages MUST remain visible when the pages
* are viewed on the internet or intranet.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Support can be obtained from support forums at:
* http://www.mvnForum.com/mvnforum/index
*
* Correspondence and Marketing Questions can be sent to:
* info@MyVietnam.net
*
* @author: Minh Nguyen minhnn@MyVietnam.net
* @author: Mai Nguyen mai.nh@MyVietnam.net
*/
package com.mvnforum.common;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.awt.image.BufferedImage;
import com.mvnforum.auth.AuthenticationException;
import com.mvnforum.auth.OnlineUserManager;
import com.octo.captcha.image.ImageCaptchaFactory;
import com.octo.captcha.image.ListImageCaptchaEngine;
import com.octo.captcha.image.gimpy.*;
import com.octo.captcha.image.gimpy.wordgenerator.RandomWordGenerator;
import com.octo.captcha.image.gimpy.wordtoimage.*;
import com.octo.captcha.image.gimpy.wordtoimage.backgroundgenerator.*;
import com.octo.captcha.image.gimpy.wordtoimage.fontgenerator.TwistedAndShearedRandomFontGenerator;
import com.octo.captcha.image.gimpy.wordtoimage.textpaster.RandomTextPaster;
import com.octo.captcha.image.gimpy.wordtoimage.textpaster.SimpleTextPaster;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import net.myvietnam.mvncore.exception.AssertionException;
import net.myvietnam.mvncore.exception.DatabaseException;
public class MVNCaptchaService extends ListImageCaptchaEngine {
private static final Integer MIN_WORD_LENGTH = new Integer(6);
private static final Integer MAX_WORD_LENGTH = new Integer(8);
private static final Integer IMAGE_WIDTH = new Integer(380);
private static final Integer IMAGE_HEIGHT = new Integer(80);
private static final Integer MIN_FONT_SIZE = new Integer(44);
private static final Integer MAX_FONT_SIZE = new Integer(50);
private static final String NUMERIC_CHARS = "123456789";// No numeric 0
private static final String UPPER_ASCII_CHARS= "ABCDEFGHJKLMNPQRSTUVWXYZ";// No upper O I
private static final String LOWER_ASCII_CHARS= "abcdefghjklmnpqrstuvwxyz";// No lower o i
/**
* Singleton instance of this class
*/
private static MVNCaptchaService instance = new MVNCaptchaService();
private ArrayList textPasterList;
private ArrayList backgroundGeneratorList;
private ArrayList fontGeneratorList;
/**
* Private constructor to prevent instantiation
*/
private MVNCaptchaService() {
}
public static MVNCaptchaService getInstance() {
return instance;
}
protected void buildInitialFactories() {
textPasterList = new ArrayList();
backgroundGeneratorList = new ArrayList();
fontGeneratorList = new ArrayList();
textPasterList.add(new SimpleTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, Color.GREEN));
textPasterList.add(new RandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, Color.GREEN));
textPasterList.add(new SimpleTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, Color.RED));
textPasterList.add(new RandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, Color.RED));
textPasterList.add(new SimpleTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, Color.BLUE));
textPasterList.add(new RandomTextPaster(MIN_WORD_LENGTH, MAX_WORD_LENGTH, Color.BLUE));
backgroundGeneratorList.add(new EllipseBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT));
backgroundGeneratorList.add(new UniColorBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT));
backgroundGeneratorList.add(new MultipleShapeBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT));
backgroundGeneratorList.add(new FunkyBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT));
backgroundGeneratorList.add(new GradientBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, Color.WHITE, Color.BLACK));
backgroundGeneratorList.add(new GradientBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, Color.BLACK, Color.WHITE));
backgroundGeneratorList.add(new GradientBackgroundGenerator(IMAGE_WIDTH, IMAGE_HEIGHT, Color.ORANGE, Color.MAGENTA));
fontGeneratorList.add(new TwistedAndShearedRandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE));
//fontGeneratorList.add(new TwistedRandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE));// link character too much
//fontGeneratorList.add(new RandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE));// to easy to read
//fontGeneratorList.add(new DeformedRandomFontGenerator(MIN_FONT_SIZE, MAX_FONT_SIZE));// to small font
// no char upper O, char lower o and numerric 0 because user cannot answer
WordGenerator words = new RandomWordGenerator(NUMERIC_CHARS + UPPER_ASCII_CHARS);
for (Iterator fontIter = fontGeneratorList.iterator(); fontIter.hasNext(); ) {
FontGenerator font = (FontGenerator)fontIter.next();
for (Iterator backIter = backgroundGeneratorList.iterator(); backIter.hasNext(); ) {
BackgroundGenerator back = (BackgroundGenerator)backIter.next();
for (Iterator textIter = textPasterList.iterator(); textIter.hasNext(); ) {
TextPaster parser = (TextPaster)textIter.next();
WordToImage word2image = new ComposedWordToImage(font, back, parser);
ImageCaptchaFactory factory = new GimpyFactory(words, word2image);
addFactory(factory);
}
}
}
}
/**
* Write the captcha image of current user to the servlet response
*
* @param request HttpServletRequest
* @param response HttpServletResponse
* @throws IOException
*/
public void writeCaptchaImage(HttpServletRequest request, HttpServletResponse response)
throws IOException, AssertionException, DatabaseException, AuthenticationException {
BufferedImage image = OnlineUserManager.getInstance().getOnlineUser(request).getCurrentCaptchaImage();
if (image == null) {
return;
}
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
outputStream.flush();
outputStream.close();
outputStream = null;// no close twice
} catch (IOException ex) {
throw ex;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) { }
}
}
}
/*
public void writeTestCaptchaImage(HttpServletRequest request, HttpServletResponse response)
throws IOException {
ImageCaptcha imageCaptcha = getNextImageCaptcha();
BufferedImage image = (BufferedImage)imageCaptcha.getChallenge();
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outputStream);
encoder.encode(image);
outputStream.flush();
outputStream.close();
outputStream = null;// no close twice
} catch (IOException ex) {
throw ex;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) { }
}
imageCaptcha.disposeChallenge();
}
}
private void testCaptchaImage(String folder) {
if (folder == null) {
folder = "c:\\";
}
if (folder.endsWith("\\") == false) {
folder = folder + "\\";
}
WordGenerator words = new RandomWordGenerator("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
for (Iterator fontIter = fontGeneratorList.iterator(); fontIter.hasNext(); ) {
FontGenerator font = (FontGenerator)fontIter.next();
for (Iterator backIter = backgroundGeneratorList.iterator(); backIter.hasNext(); ) {
BackgroundGenerator back = (BackgroundGenerator)backIter.next();
for (Iterator textIter = textPasterList.iterator(); textIter.hasNext(); ) {
TextPaster parser = (TextPaster)textIter.next();
WordToImage word2image = new ComposedWordToImage(font, back, parser);
ImageCaptchaFactory factory = new GimpyFactory(words, word2image);
ImageCaptcha imageCaptcha = factory.getImageCaptcha();
String filename = "TestCaptcha_" + getClassName(font) + "_" + getClassName(back) + "_" + getClassName(parser) + ".jpg";
try {
ImageUtil.writeJpegImage_Sun((BufferedImage) imageCaptcha.getChallenge(), folder + filename);
} catch (IOException ex) {
ex.printStackTrace();
return;
}
}
}
}
}
private String getClassName(Object obj) {
String fullName = obj.getClass().getName();
int dotIndex = fullName.lastIndexOf('.');
if (dotIndex == -1) {
return fullName;
}
return fullName.substring(dotIndex + 1);
}
public static void main(String[] args) throws IOException {
MVNCaptchaService.getInstance().testCaptchaImage("c:\\temp\\testcaptcha");
}
*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -