A Java Sample For jCaptcha
Captcha? is very easy to used, i write a little java?sample to proved its powerful feature!
So first download the jcaptcha-all-1.0-RC3.jar From http://jcaptcha.sourceforge.net/
Then Run the following sample.
/*
?*Copyright???2006?David.turing
?*Email:?securex@163.com
?*QQ群:14966586
?*Blog:?openssl.blogjava.net
? */
package ?org.dev2dev.image.captcha;
import ?java.awt.Color;
import ?java.awt.Font;
import ?java.awt.font.TextAttribute;
import ?java.awt.image.BufferedImage;
import ?java.io.File;
import ?java.io.IOException;
import ?java.text.AttributedString;
import ?javax.imageio.ImageIO;
import ?com.octo.captcha.CaptchaException;
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.RandomFontGenerator;
import ?com.octo.captcha.component.image.textpaster.SimpleTextPaster;
import ?com.octo.captcha.component.image.textpaster.TextPaster;
public ? class ?CaptchaHelper?{
???? private ?FontGenerator?fontGenerator;
???? private ?BackgroundGenerator?background;
???? private ?TextPaster?textPaster;
???? public ? void ?init(FontGenerator?fontGenerator,
????????????BackgroundGenerator?background,?TextPaster?textPaster)?{
???????? this .background? = ?background;
???????? this .fontGenerator? = ?fontGenerator;
???????? this .textPaster? = ?textPaster;
????}
???? public ?BufferedImage?getImage(String?word)? throws ?CaptchaException?{
???????? int ?wordLength;
???????? // Check?your?word?Lenth
????????wordLength? = ?checkWordLength(word);
???????? // Process?font?for?word
????????AttributedString?attributedWord? = ?getAttributedString(word,?wordLength);
???????? // Contruct?your?Backgroud
????????BufferedImage?background? = ?getBackround();
???????? // Contruct?your?captcha?image?include?your?word!
???????? return ?pasteText(background,?attributedWord);
????}
???? public ?AttributedString?getAttributedString(String?word,? int ?wordLength)?{
????????AttributedString?attributedWord? = ? new ?AttributedString(word);
???????? // Here,?we?provide?each?char?of?the?word?a?kind?of?font,?so,??n?char,?n?font,?hehe?
???????? for ?( int ?i? = ? 0 ;?i? < ?wordLength;?i ++ )?{
????????????Font?font? = ?getFont(); // get?the?new?font?for?next?character
????????????attributedWord.addAttribute(TextAttribute.FONT,?font,?i,?i? + ? 1 );
????????}
???????? return ?attributedWord;
????}
???? int ?checkWordLength(String?word)? throws ?CaptchaException?{
???????? int ?wordLength;
???????? if ?(word? == ? null )?{
???????????? throw ? new ?CaptchaException( " CaptchaHelper:null?word " );
????????}? else ?{
????????????wordLength? = ?word.length();
???????????? if ?(wordLength? > ? this .getMaxAcceptedWordLength()
???????????????????? || ?wordLength? < ?getMinAcceptedWordLength())?{
???????????????? throw ? new ?CaptchaException( " CaptchaHelper:invalid?length?word " );
????????????}
????????}
???????? return ?wordLength;
????}
???? /**
?????*? @return ?the?max?word?length?accepted?by?this?word2image?service
????? */
???? public ? int ?getMaxAcceptedWordLength()?{
???????? return ?textPaster.getMaxAcceptedWordLength();
????}
???? /**
?????*? @return ?the?min?word?length?accepted?by?this?word2image?service
????? */
???? public ? int ?getMinAcceptedWordLength()?{
???????? return ?textPaster.getMinAcceptedWordLength();
????}
???? /**
?????*? @return ?the?generated?image?height
????? */
???? public ? int ?getImageHeight()?{
???????? return ?background.getImageHeight();
????}
???? /**
?????*? @return ?teh?generated?image?width
????? */
???? public ? int ?getImageWidth()?{
???????? return ?background.getImageWidth();
????}
???? /**
?????*? @return ?the?min?font?size?for?the?generated?image
????? */
???? public ? int ?getMinFontSize()?{
???????? return ?fontGenerator.getMinFontSize();
????}
???? /**
?????*?Method?from?imageFromWord?method?to?apply?font?to?String.?Implementations?must?take?into?account?the?minFontSize
?????*?and?the?MaxFontSize.
?????*
?????*? @return ?a?Font
????? */
????Font?getFont()?{
???????? return ?fontGenerator.getFont();
????}
???? /**
?????*?Generates?a?backround?image?on?wich?text?will?be?paste.?Implementations?must?take?into?account?the?imageHeigt?and
?????*?imageWidth.
?????*
?????*? @return ?the?background?image
????? */
????BufferedImage?getBackround()?{
???????? return ?background.getBackground();
????}
???? /**
?????*?Pastes?the?attributed?string?on?the?backround?image?and?return?the?final?image.?Implementation?must?take?into
?????*?account?the?fact?that?the?text?must?be?readable?by?human?and?non?by?programs
?????*
?????*? @return ?the?final?image
?????*
?????*? @throws ?CaptchaException?if?any?exception?accurs?during?paste?routine.
????? */
????BufferedImage?pasteText(BufferedImage?background,
????????????AttributedString?attributedWord)? throws ?CaptchaException?{
???????? return ?textPaster.pasteText(background,?attributedWord);
????}
???? /**
?????*? @param ?args
????? */
???? public ? static ? void ?main(String[]?args)?{
????????Integer?minAcceptedWordLength? = ? new ?Integer( 5 );
????????Integer?maxAcceptedWordLength? = ? new ?Integer( 30);
????????Integer?imageHeight?=?new?Integer(100);
????????Integer?imageWidth?=?new?Integer(400);
????????Integer?minFontSize?=?new?Integer(30);
????????Integer?maxFontSize?=?new?Integer(30);
????????BackgroundGenerator?background?=?new?GradientBackgroundGenerator(
????????????????imageWidth,?imageHeight,?Color.white,?Color.white);
????????FontGenerator?fontGenerator?=?new?RandomFontGenerator(minFontSize,
????????????????maxFontSize);
????????TextPaster?textPaster?=?new?SimpleTextPaster(minAcceptedWordLength,
????????????????maxAcceptedWordLength,?Color.blue);
????????CaptchaHelper?chelper?=?new?CaptchaHelper();
????????chelper.init(fontGenerator,?background,?textPaster);
????????BufferedImage?test?=?chelper.getImage("openssl.blogjava.net");
????????if?(test?!=?null)
????????????System.out.println("width="?+?test.getWidth());
????????File?testfile?=?new?File("c:\\a.png");
????????try?{
????????????//?well?the?captcha?picture?is?generate,?open?the?png?file!
????????????ImageIO.write(test,?"PNG",?testfile);
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}
}
?*Copyright???2006?David.turing
?*Email:?securex@163.com
?*QQ群:14966586
?*Blog:?openssl.blogjava.net
? */
package ?org.dev2dev.image.captcha;
import ?java.awt.Color;
import ?java.awt.Font;
import ?java.awt.font.TextAttribute;
import ?java.awt.image.BufferedImage;
import ?java.io.File;
import ?java.io.IOException;
import ?java.text.AttributedString;
import ?javax.imageio.ImageIO;
import ?com.octo.captcha.CaptchaException;
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.RandomFontGenerator;
import ?com.octo.captcha.component.image.textpaster.SimpleTextPaster;
import ?com.octo.captcha.component.image.textpaster.TextPaster;
public ? class ?CaptchaHelper?{
???? private ?FontGenerator?fontGenerator;
???? private ?BackgroundGenerator?background;
???? private ?TextPaster?textPaster;
???? public ? void ?init(FontGenerator?fontGenerator,
????????????BackgroundGenerator?background,?TextPaster?textPaster)?{
???????? this .background? = ?background;
???????? this .fontGenerator? = ?fontGenerator;
???????? this .textPaster? = ?textPaster;
????}
???? public ?BufferedImage?getImage(String?word)? throws ?CaptchaException?{
???????? int ?wordLength;
???????? // Check?your?word?Lenth
????????wordLength? = ?checkWordLength(word);
???????? // Process?font?for?word
????????AttributedString?attributedWord? = ?getAttributedString(word,?wordLength);
???????? // Contruct?your?Backgroud
????????BufferedImage?background? = ?getBackround();
???????? // Contruct?your?captcha?image?include?your?word!
???????? return ?pasteText(background,?attributedWord);
????}
???? public ?AttributedString?getAttributedString(String?word,? int ?wordLength)?{
????????AttributedString?attributedWord? = ? new ?AttributedString(word);
???????? // Here,?we?provide?each?char?of?the?word?a?kind?of?font,?so,??n?char,?n?font,?hehe?
???????? for ?( int ?i? = ? 0 ;?i? < ?wordLength;?i ++ )?{
????????????Font?font? = ?getFont(); // get?the?new?font?for?next?character
????????????attributedWord.addAttribute(TextAttribute.FONT,?font,?i,?i? + ? 1 );
????????}
???????? return ?attributedWord;
????}
???? int ?checkWordLength(String?word)? throws ?CaptchaException?{
???????? int ?wordLength;
???????? if ?(word? == ? null )?{
???????????? throw ? new ?CaptchaException( " CaptchaHelper:null?word " );
????????}? else ?{
????????????wordLength? = ?word.length();
???????????? if ?(wordLength? > ? this .getMaxAcceptedWordLength()
???????????????????? || ?wordLength? < ?getMinAcceptedWordLength())?{
???????????????? throw ? new ?CaptchaException( " CaptchaHelper:invalid?length?word " );
????????????}
????????}
???????? return ?wordLength;
????}
???? /**
?????*? @return ?the?max?word?length?accepted?by?this?word2image?service
????? */
???? public ? int ?getMaxAcceptedWordLength()?{
???????? return ?textPaster.getMaxAcceptedWordLength();
????}
???? /**
?????*? @return ?the?min?word?length?accepted?by?this?word2image?service
????? */
???? public ? int ?getMinAcceptedWordLength()?{
???????? return ?textPaster.getMinAcceptedWordLength();
????}
???? /**
?????*? @return ?the?generated?image?height
????? */
???? public ? int ?getImageHeight()?{
???????? return ?background.getImageHeight();
????}
???? /**
?????*? @return ?teh?generated?image?width
????? */
???? public ? int ?getImageWidth()?{
???????? return ?background.getImageWidth();
????}
???? /**
?????*? @return ?the?min?font?size?for?the?generated?image
????? */
???? public ? int ?getMinFontSize()?{
???????? return ?fontGenerator.getMinFontSize();
????}
???? /**
?????*?Method?from?imageFromWord?method?to?apply?font?to?String.?Implementations?must?take?into?account?the?minFontSize
?????*?and?the?MaxFontSize.
?????*
?????*? @return ?a?Font
????? */
????Font?getFont()?{
???????? return ?fontGenerator.getFont();
????}
???? /**
?????*?Generates?a?backround?image?on?wich?text?will?be?paste.?Implementations?must?take?into?account?the?imageHeigt?and
?????*?imageWidth.
?????*
?????*? @return ?the?background?image
????? */
????BufferedImage?getBackround()?{
???????? return ?background.getBackground();
????}
???? /**
?????*?Pastes?the?attributed?string?on?the?backround?image?and?return?the?final?image.?Implementation?must?take?into
?????*?account?the?fact?that?the?text?must?be?readable?by?human?and?non?by?programs
?????*
?????*? @return ?the?final?image
?????*
?????*? @throws ?CaptchaException?if?any?exception?accurs?during?paste?routine.
????? */
????BufferedImage?pasteText(BufferedImage?background,
????????????AttributedString?attributedWord)? throws ?CaptchaException?{
???????? return ?textPaster.pasteText(background,?attributedWord);
????}
???? /**
?????*? @param ?args
????? */
???? public ? static ? void ?main(String[]?args)?{
????????Integer?minAcceptedWordLength? = ? new ?Integer( 5 );
????????Integer?maxAcceptedWordLength? = ? new ?Integer( 30);
????????Integer?imageHeight?=?new?Integer(100);
????????Integer?imageWidth?=?new?Integer(400);
????????Integer?minFontSize?=?new?Integer(30);
????????Integer?maxFontSize?=?new?Integer(30);
????????BackgroundGenerator?background?=?new?GradientBackgroundGenerator(
????????????????imageWidth,?imageHeight,?Color.white,?Color.white);
????????FontGenerator?fontGenerator?=?new?RandomFontGenerator(minFontSize,
????????????????maxFontSize);
????????TextPaster?textPaster?=?new?SimpleTextPaster(minAcceptedWordLength,
????????????????maxAcceptedWordLength,?Color.blue);
????????CaptchaHelper?chelper?=?new?CaptchaHelper();
????????chelper.init(fontGenerator,?background,?textPaster);
????????BufferedImage?test?=?chelper.getImage("openssl.blogjava.net");
????????if?(test?!=?null)
????????????System.out.println("width="?+?test.getWidth());
????????File?testfile?=?new?File("c:\\a.png");
????????try?{
????????????//?well?the?captcha?picture?is?generate,?open?the?png?file!
????????????ImageIO.write(test,?"PNG",?testfile);
????????}?catch?(IOException?e)?{
????????????e.printStackTrace();
????????}
????}
}
posted on 2006-06-20 22:10 david.turing 閱讀(2629) 評論(1) 編輯 收藏 所屬分類: Security領域