1 43 44 package net.jforum.util; 45 46 import java.awt.Color ; 47 import java.awt.image.BufferedImage ; 48 import java.io.IOException ; 49 import java.io.OutputStream ; 50 import java.util.ArrayList ; 51 import java.util.Iterator ; 52 import java.util.List ; 53 54 import javax.imageio.ImageIO ; 55 56 import net.jforum.JForumExecutionContext; 57 import net.jforum.SessionFacade; 58 import net.jforum.util.preferences.ConfigKeys; 59 import net.jforum.util.preferences.SystemGlobals; 60 61 import org.apache.log4j.Logger; 62 63 import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator; 64 import com.octo.captcha.component.image.backgroundgenerator.FunkyBackgroundGenerator; 65 import com.octo.captcha.component.image.backgroundgenerator.GradientBackgroundGenerator; 66 import com.octo.captcha.component.image.fontgenerator.FontGenerator; 67 import com.octo.captcha.component.image.fontgenerator.TwistedAndShearedRandomFontGenerator; 68 import com.octo.captcha.component.image.textpaster.RandomTextPaster; 69 import com.octo.captcha.component.image.textpaster.TextPaster; 70 import com.octo.captcha.component.image.wordtoimage.ComposedWordToImage; 71 import com.octo.captcha.component.image.wordtoimage.WordToImage; 72 import com.octo.captcha.component.wordgenerator.RandomWordGenerator; 73 import com.octo.captcha.component.wordgenerator.WordGenerator; 74 import com.octo.captcha.engine.image.ListImageCaptchaEngine; 75 import com.octo.captcha.image.ImageCaptchaFactory; 76 import com.octo.captcha.image.gimpy.GimpyFactory; 77 78 82 public class Captcha extends ListImageCaptchaEngine 83 { 84 private static final Logger logger = Logger.getLogger(Captcha.class); 85 86 private static Captcha classInstance = new Captcha(); 87 private List backgroundGeneratorList; 88 private List textPasterList; 89 private List fontGeneratorList; 90 91 private static final String charsInUse = "123456789ABCDEFGHJLKMNPRSTWXYZabcdefghijlmnopkrstuvxzyk@#%^"; 92 93 98 public static Captcha getInstance() 99 { 100 return classInstance; 101 } 102 103 protected void buildInitialFactories() 104 { 105 this.backgroundGeneratorList = new ArrayList (); 106 this.textPasterList = new ArrayList (); 107 this.fontGeneratorList = new ArrayList (); 108 109 int width = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_WIDTH); 110 int height = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_HEIGHT); 111 int minWords = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MIN_WORDS); 112 int maxWords = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MAX_WORDS); 113 int minFontSize = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MIN_FONT_SIZE); 114 int maxFontSize = SystemGlobals.getIntValue(ConfigKeys.CAPTCHA_MAX_FONT_SIZE); 115 116 this.backgroundGeneratorList.add(new GradientBackgroundGenerator(new Integer (width), 117 new Integer (height), Color.BLACK, Color.GRAY)); 118 this.backgroundGeneratorList.add(new FunkyBackgroundGenerator(new Integer (250), new Integer (50))); 119 120 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.RED)); 121 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.ORANGE)); 122 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.BLUE)); 123 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.WHITE)); 124 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.GREEN)); 125 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.GRAY)); 126 this.textPasterList.add(new RandomTextPaster(new Integer (minWords), new Integer (maxWords), Color.YELLOW)); 127 128 this.fontGeneratorList.add(new TwistedAndShearedRandomFontGenerator(new Integer (minFontSize), new Integer (maxFontSize))); 129 130 WordGenerator words = new RandomWordGenerator(charsInUse); 132 133 for (Iterator fontIter = this.fontGeneratorList.iterator(); fontIter.hasNext();) { 134 FontGenerator fontGeny = (FontGenerator) fontIter.next(); 135 136 for (Iterator backIter = this.backgroundGeneratorList.iterator(); backIter.hasNext();) { 137 BackgroundGenerator bkgdGeny = (BackgroundGenerator) backIter.next(); 138 139 for (Iterator textIter = this.textPasterList.iterator(); textIter.hasNext();) { 140 TextPaster textPaster = (TextPaster) textIter.next(); 141 142 WordToImage word2image = new ComposedWordToImage(fontGeny, bkgdGeny, textPaster); 143 144 ImageCaptchaFactory factory = new GimpyFactory(words, word2image); 146 147 addFactory(factory); 149 } 150 } 151 } 152 } 153 154 public void writeCaptchaImage() 155 { 156 BufferedImage image = SessionFacade.getUserSession().getCaptchaImage(); 157 158 if (image == null) { 159 return; 160 } 161 162 OutputStream outputStream = null; 163 164 try { 165 outputStream = JForumExecutionContext.getResponse().getOutputStream(); 166 ImageIO.write(image, "jpg", outputStream); 167 } 168 catch (IOException ex) { 169 logger.error(ex); 170 } 171 finally { 172 if (outputStream != null) { 173 try { 174 outputStream.close(); 175 } 176 catch (IOException ex) {} 177 } 178 } 179 } 180 } 181 | Popular Tags |