【问题标题】:How to set font style for text in Captcha generated by jCaptcha如何为 jCaptcha 生成的验证码中的文本设置字体样式
【发布时间】:2015-08-04 21:40:38
【问题描述】:

我正在使用 jcaptcha-all-1.0-RC6 生成验证码图像。

下面是sn-p的代码

          captchaService = new DefaultManageableImageCaptchaService();
          logger.info(" After creating instance  getCaptcha ");
          long id = System.currentTimeMillis();
          String captchaId = String.valueOf(id);

          logger.info(" getCaptcha Id " + captchaId);

          logger.info("***********Coming into captcha service***************************************************");
          BufferedImage challenge = captchaService
                       .getImageChallengeForID(captchaId);
          WritableRaster raster = challenge.getRaster();
        ColorModel model = challenge.getColorModel();
          challenge.setRGB(0,25,51);

          logger.debug("challenge:" + challenge);
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

          ImageIO.write(challenge, "jpeg", outputStream);

          outputStream.close();
          outputStream.flush();
          byte[] res = outputStream.toByteArray();

          String encodedImage = Base64.encodeBase64String(res);

但是,我得到的图像质量不是很好。我们可以做些什么来获得更好的图像质量和更好的可读性。

即使更改字体样式也很有用。对此的任何帮助也会很有用

【问题讨论】:

    标签: java captcha


    【解决方案1】:

    要通过配置更改图像,您必须扩展 ListImageCaptchaEngine 并使用生成器:

    • 词生成器
    • 颜色生成器
    • 后台生成器
    • 字体生成器

      public class MyImageCaptchaEngine extends ListImageCaptchaEngine {
      
      @Override
      protected void buildInitialFactories() {
      
          WordGenerator wgen = new RandomWordGenerator("ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789");
          RandomRangeColorGenerator cgen = new RandomRangeColorGenerator(new int[] { 0, 255 }, new int[] { 20, 100 }, new int[] { 20, 100 });
      
          TextPaster textPaster = new RandomTextPaster(new Integer(4), new Integer(5), cgen, Boolean.TRUE);
      
          BackgroundGenerator backgroundGenerator = new UniColorBackgroundGenerator(new Integer(240), new Integer(50), new Color(252,252,253));
      
      
          Font[] fontsList = new Font[] { new Font("Helvetica", Font.TYPE1_FONT, 10), new Font("Arial", 0, 14), new Font("Vardana", 0, 17), };
      
          FontGenerator fontGenerator = new RandomFontGenerator(new Integer(18), new Integer(30), fontsList);
          WordToImage wordToImage = new ComposedWordToImage(fontGenerator, backgroundGenerator, textPaster);
              this.addFactory(new GimpyFactory(wgen, wordToImage));
          }
      }
      

    并使用它

    ImageCaptchaService instance = new DefaultManageableImageCaptchaService(
                new FastHashMapCaptchaStore(),
                new MyImageCaptchaEngine(),
                180,
                100000,
                75000);
    

    【讨论】:

    • 你能帮助我如何声明你的代码中指定的 backgroundGenerator、textPaster、wgen。
    • 我使用了你的代码。但是现在我遇到了错误,比如单词太高:尝试使用更少的字母、更小的字体或更大的背景。请对此提供帮助
    • @ParthaBisoi 只是玩弄设置,即图像大小、字体大小等,看看哪个适合你。您可以使字母变小,但它们可能不可读,您可以使图像变大,这可能不适合您的 ui 设计,您可能使用较短的单词,这可能与您的安全要求相矛盾等 - 由您来找到确切的适合您的设置。
    【解决方案2】:

    查看DefaultGimpyEngine,了解如何扩展ListImageCaptchaEngine 并提供背景颜色、图像大小、字体等设置(您可以复制这些设置并提供自己的值)。

    AFAIK 字体样式实际上是同一家族的不同字体(比如“Arial bold”和“Arial regular”是“Arial”家族的两种样式)。因此,您只能选择您想要的字体(确保它们在系统上可用)。

    【讨论】:

    • 如果你能用示例代码解释一下会有所帮助。
    • @ParthaBisoi 看看我链接的源代码。您可能想进一步深入研究代码和文档以了解那里发生了什么。解释所有超出 SO 范围的内容。
    猜你喜欢
    • 2019-06-25
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 2014-02-13
    • 1970-01-01
    相关资源
    最近更新 更多