SimpleCaptchaGenerator { public CaptchaResult generate(CaptchaSettings settings){ int width = settings.getWidth(); int height = settings.getHeight(); BufferedImage bufImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = bufImg.createGraphics(); g2d.fillRect(0, 0, width, height); drawBorder(g2d, settings); drawPuzzleLines(g2d, settings); String codes = RandomStringUtils.randomAlphanumeric(settings.getCodeLength()).toUpperCase(); drawCodes(g2d, settings, codes); CaptchaResult result = new CaptchaResult(codes); ByteArrayOutputStream bos = new ByteArrayOutputStream(128); FileUtils.writeToStream(bufImg, settings.getImageFormatName(), bos); result.setData(bos.toByteArray()); return result; }  CaptchaResult writeTo(CaptchaSettings settings, OutputStream output); CaptchaResult writeTo(CaptchaSettings settings, String path); CaptchaResult generate(CaptchaSettings settings); static final int MAX_RGB; }
@Test public void testGenerate(){ CaptchaSettings settings = new CaptchaSettings(); settings.setWidth(400); settings.setHeight(100); settings.setPuzzleLineCount(50); SimpleCaptchaGenerator generator = new SimpleCaptchaGenerator(); CaptchaResult res = generator.writeTo(settings, "/data/tmp/captcha.png"); CaptchaSignedResult signed = checker.encode(res.getCode()); boolean checked = checker.check(res.getCode(), signed.getSigned()); assertThat(checked).isTrue(); LangUtils.await(validInSeconds); checked = checker.check(res.getCode(), signed.getSigned()); assertThat(checked).isFalse(); }