private Producer createCaptchaProducer( int width, int height, int length, String possibleSymbols) { Properties props = new Properties(); props.setProperty("kaptcha.textproducer.char.string", possibleSymbols); props.setProperty("kaptcha.textproducer.char.length", String.valueOf(length)); props.setProperty("kaptcha.image.width", String.valueOf(width)); props.setProperty("kaptcha.image.height", String.valueOf(height)); Config conf = new Config(props); DefaultKaptcha captcha = new DefaultKaptcha(); captcha.setConfig(conf); return captcha; }
private String getCaptchaText() { if (preDefinedTexts != null && !preDefinedTexts.isEmpty()) { String text = preDefinedTexts.get(textCount); textCount = (textCount + 1) % preDefinedTexts.size(); return text; } else { return producer.createText(); } }
@Override public byte[] generateCaptchaImage(String captchaKey) throws AccountCaptchaException { String text = captchaMap.get(captchaKey); if (text == null) { throw new AccountCaptchaException("Captch key" + captchaKey + " not found!"); } BufferedImage image = producer.createImage(text); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { ImageIO.write(image, "jpg", out); } catch (IOException e) { throw new AccountCaptchaException("Failed to write captcha stream!", e); } return out.toByteArray(); }
@Override public void afterPropertiesSet() throws Exception { producer = new DefaultKaptcha(); producer.setConfig(new Config(new Properties())); }