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.component.image.color.ColorGenerator;
10  
11  import java.awt.*;
12  import java.awt.image.BufferedImage;
13  import java.text.AttributedString;
14  
15  /***
16   * <p/>
17   * Paste the text randomly on the background </p>
18   *
19   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue </a>
20   * @version 1.0
21    * @deprecated 
22   */
23  public class RandomTextPaster extends AbstractTextPaster {
24  
25      protected final int kerning = 20;
26  
27      protected Color[] textColors = null;
28  
29  
30      public RandomTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
31                              Color textColor) {
32          super(minAcceptedWordLength, maxAcceptedWordLength, textColor);
33      }
34  
35      public RandomTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
36                              Color[] textColors) {
37          super(minAcceptedWordLength, maxAcceptedWordLength);
38          this.textColors = textColors;
39      }
40  
41      public RandomTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
42                              ColorGenerator colorGenerator) {
43          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
44  
45      }
46  
47      public RandomTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
48                              ColorGenerator colorGenerator, Boolean manageColorPerGlyph) {
49          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
50  
51      }
52  
53      /***
54       * Paste the text randomly on the background.
55       * <p/>
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. <p/>
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(final BufferedImage background,
65                                     final AttributedString attributedString) {
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 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 newAttrString = new MutableAttributedString(g2,
78                  attributedString, kerning);
79  
80          // space out the glyphs with a little kerning
81          newAttrString.useMinimumSpacing(kerning);
82          // shift string to a random spot in the output imge
83          newAttrString.moveToRandomSpot(background);
84          // now draw each glyph at the appropriate spot on the image.
85          if (isManageColorPerGlyph())
86              newAttrString.drawString(g2, getColorGenerator());
87          else
88              newAttrString.drawString(g2);
89  
90          g2.dispose();
91          return out;
92      }
93  
94  }