View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   package com.octo.captcha.component.image.textpaster;
8   
9   import com.octo.captcha.CaptchaException;
10  import com.octo.captcha.component.image.color.ColorGenerator;
11  import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
12  
13  import java.awt.*;
14  import java.awt.image.BufferedImage;
15  import java.text.AttributedString;
16  
17  /***
18   * <p><ul><li></li></ul></p>
19   *
20   * @author <a href="mailto:marc.antoine.garrigue@gmail.com">Marc-Antoine Garrigue</a>
21   * @version 1.0
22    * @deprecated 
23   */
24  public class DecoratedRandomTextPaster extends AbstractTextPaster {
25  
26      protected final int kerning = 20;
27      private TextDecorator[] decorators;
28  
29      /***
30       * @param minAcceptedWordLength Max length of a word
31       * @param maxAcceptedWordLength Min length of a word
32       * @param colorGenerator        Color generatior for the text
33       * @param decorators            An array of decorators
34       */
35      public DecoratedRandomTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
36                                       ColorGenerator colorGenerator, TextDecorator[] decorators) {
37          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
38          this.decorators = decorators;
39  
40      }
41  
42      /***
43       * @param minAcceptedWordLength Max length of a word
44       * @param maxAcceptedWordLength Min length of a word
45       * @param colorGenerator        Color generatior for the text
46       * @param decorators            An array of decorators
47       */
48      public DecoratedRandomTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
49                                       ColorGenerator colorGenerator, Boolean manageColorPerGlyph, TextDecorator[] decorators) {
50          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
51          this.decorators = decorators;
52  
53      }
54  
55      /***
56       * Pastes the attributed string on the backround image and return the final image. Implementation must take into
57       * account the fact that the text must be readable by human and non by programs
58       *
59       * @return the final image
60       *
61       * @throws com.octo.captcha.CaptchaException
62       *          if any exception accurs during paste routine.
63       */
64      public BufferedImage pasteText(BufferedImage background, AttributedString attributedWord)
65              throws CaptchaException {
66          BufferedImage out = copyBackground(background);
67          Graphics2D g2 = pasteBackgroundAndSetTextColor(out, background);
68          g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
69                  RenderingHints.VALUE_FRACTIONALMETRICS_ON);
70          g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
71          g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
72                  RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
73          // this attribute doesn't do anything in JDK 1.4, but maybe it will in JDK 1.5
74          // attributedString.addAttribute(TextAttribute.WIDTH, TextAttribute.WIDTH_EXTENDED);
75  
76          // convert string into a series of glyphs we can work with
77          MutableAttributedString mas = new MutableAttributedString(g2,
78                  attributedWord, kerning);
79  
80          // space out the glyphs with a little kerning
81                 mas.useMinimumSpacing(kerning);
82                 // shift string to a random spot in the output imge
83                 mas.moveToRandomSpot(background);
84  
85  
86          // now draw each glyph at the appropriate spot on the image.
87          if (isManageColorPerGlyph()) {
88              mas.drawString(g2, getColorGenerator());
89          } else {
90              mas.drawString(g2);
91          }
92  
93          //and now decorate
94          if (decorators != null) {
95              for (int i = 0; i < decorators.length; i++) {
96                  decorators[i].decorateAttributedString(g2,  mas);
97  
98              }
99          }
100         g2.dispose();
101         return out;
102     }
103 
104 }