View Javadoc

1   package com.octo.captcha.component.image.textpaster;
2   
3   
4   import com.octo.captcha.CaptchaException;
5   import com.octo.captcha.component.image.color.ColorGenerator;
6   import com.octo.captcha.component.image.textpaster.textdecorator.TextDecorator;
7   import com.octo.captcha.component.image.textpaster.textvisitor.TextVisitor;
8   
9   import java.awt.*;
10  import java.awt.image.BufferedImage;
11  import java.text.AttributedString;
12  
13  /***
14   * @author mag
15   * @Date 5 mars 2008
16   * @deprecated 
17   */
18  public class VisitedAndDecoratedTextPaster extends AbstractTextPaster {
19  
20  
21      protected final int kerning = 20;
22      private TextVisitor[] textVisitors;
23      private TextDecorator[] textDecorators;
24  
25      public VisitedAndDecoratedTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, TextVisitor[] textVisitors, TextDecorator[] textDecorators) {
26          super(minAcceptedWordLength, maxAcceptedWordLength);
27          this.textVisitors = textVisitors;
28          this.textDecorators = textDecorators;
29      }
30  
31      public VisitedAndDecoratedTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, Color textColor, TextVisitor[] textVisitors, TextDecorator[] textDecorators) {
32          super(minAcceptedWordLength, maxAcceptedWordLength, textColor);
33          this.textVisitors = textVisitors;
34          this.textDecorators = textDecorators;
35      }
36  
37      public VisitedAndDecoratedTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, TextVisitor[] textVisitors, TextDecorator[] textDecorators) {
38          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
39          this.textVisitors = textVisitors;
40          this.textDecorators = textDecorators;
41      }
42  
43      public VisitedAndDecoratedTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength, ColorGenerator colorGenerator, Boolean manageColorPerGlyph, TextVisitor[] textVisitors, TextDecorator[] textDecorators) {
44          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
45          this.textVisitors = textVisitors;
46          this.textDecorators = textDecorators;
47      }
48  
49      /***
50       * Pastes the attributed string on the backround image and return the final image. Implementation must take into
51       * account the fact that the text must be readable by human and non by programs
52       *
53       * @return the final image
54       *
55       * @throws com.octo.captcha.CaptchaException
56       *          if any exception accurs during paste routine.
57       */
58      public BufferedImage pasteText(BufferedImage background, AttributedString attributedWord)
59              throws CaptchaException {
60          BufferedImage out = copyBackground(background);
61          Graphics2D g2 = pasteBackgroundAndSetTextColor(out, background);
62          g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
63                  RenderingHints.VALUE_FRACTIONALMETRICS_ON);
64          g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
65          g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
66                  RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
67  
68  
69          // convert string into a series of glyphs we can work with
70          MutableAttributedString mas = new MutableAttributedString(g2,
71                        attributedWord, kerning);
72  
73  
74          //First decorate
75          if (textVisitors != null) {
76              for (int i = 0; i < textVisitors.length; i++) {
77                  textVisitors[i].visit(mas);
78  
79              }
80          }
81  
82           // shift string to a random spot in the output imge
83          mas.moveToRandomSpot(background);
84          // now draw each glyph at the appropriate spot on the image.
85          if (isManageColorPerGlyph()) {
86              mas.drawString(g2, getColorGenerator());
87          } else {
88              mas.drawString(g2);
89          }
90  
91          //and now decorate
92          if (textDecorators != null) {
93              for (int i = 0; i < textDecorators.length; i++) {
94                  textDecorators[i].decorateAttributedString(g2, mas);
95  
96              }
97          }
98          g2.dispose();
99          return out;
100     }
101 
102 }