首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何设置jCaptcha生成的Captcha中文本的字体样式

如何设置jCaptcha生成的Captcha中文本的字体样式
EN

Stack Overflow用户
提问于 2015-08-04 13:15:51
回答 2查看 3.4K关注 0票数 0

我正在使用jcaptcha所有1.0-Rc6生成captcha映像。

相同的代码片段如下

代码语言:javascript
复制
          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);

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

甚至更改字体样式也是有用的。在这方面的任何帮助也将是有用的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-04 13:34:37

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

  • 字发生器
  • 彩色发生器
  • 背景发生器
  • 字体生成器 公共类MyImageCaptchaEngine扩展了ListImageCaptchaEngine {@重写受保护的buildInitialFactories() { WordGenerator wgen =新的RandomRangeColorGenerator(new int[] { 0,255 },new int[] { 20,100 },new int[] { 20,100 });textPaster TextPaster = new RandomTextPaster(新整数(4),新整数(5),cgen,Boolean.TRUE);backgroundGenerator BackgroundGenerator = new UniColorBackgroundGenerator(新Integer(240),new Integer(50),new Color(252,252,253));Font[] fontsList =新Font[] {新字体(“Helvetica”,Font.TYPE1_FONT,10),新字体(“Arial”,0,14),新字体(“Vardana”,0,17),};FontGenerator fontGenerator =新RandomFontGenerator(新Integer(18),新Integer(30),fontsList);WordToImage wordToImage =新ComposedWordToImage(fontGenerator,backgroundGenerator,textPaster);this.addFactory(wgen,wordToImage);}}

并利用它

代码语言:javascript
复制
ImageCaptchaService instance = new DefaultManageableImageCaptchaService(
            new FastHashMapCaptchaStore(),
            new MyImageCaptchaEngine(),
            180,
            100000,
            75000);
票数 3
EN

Stack Overflow用户

发布于 2015-08-04 13:21:04

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

AFAIK字体风格实际上是同一个家族的不同字体(比如"Arial粗体“和"Arial正则”是"Arial“家族的两种风格)。因此,您只能选择所需的字体(确保它们在系统上可用)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31810395

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档