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  
12  import java.awt.*;
13  import java.awt.image.BufferedImage;
14  import java.text.AttributedString;
15  
16  /***
17   * <p/>
18   * Pastes the text at width/20 and height/2 </p>
19   *
20   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue </a>
21   * @version 1.0
22    * @deprecated 
23   */
24  public class SimpleTextPaster extends AbstractTextPaster {
25  
26      public SimpleTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
27                              Color textColor) {
28          super(minAcceptedWordLength, maxAcceptedWordLength, textColor);
29      }
30  
31      public SimpleTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
32                              ColorGenerator colorGenerator) {
33          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator);
34      }
35  
36      public SimpleTextPaster(Integer minAcceptedWordLength, Integer maxAcceptedWordLength,
37                              ColorGenerator colorGenerator, Boolean manageColorPerGlyph) {
38          super(minAcceptedWordLength, maxAcceptedWordLength, colorGenerator, manageColorPerGlyph);
39      }
40  
41      /***
42       * Pastes the attributed string on the backround image and return the final image. Implementation must take into
43       * account the fact that the text must be readable by human and non by programs. Pastes the text at width/20 and
44       * height/2
45       *
46       * @return the final image
47       *
48       * @throws com.octo.captcha.CaptchaException
49       *          if any exception accurs during paste routine.
50       */
51      public BufferedImage pasteText(final BufferedImage background,
52                                     final AttributedString attributedWord) throws CaptchaException {
53          int x = (background.getWidth()) / 20;
54          int y = (background.getHeight()) / 2;
55          BufferedImage out = copyBackground(background);
56          Graphics2D g2 = pasteBackgroundAndSetTextColor(out, background);
57          //pie.drawString(attributedWord.getIterator(), x, y);
58          //pie.dispose();
59  
60          // convert string into a series of glyphs we can work with
61          MutableAttributedString newAttrString = new MutableAttributedString(g2,
62                  attributedWord, 2);
63  
64          // space out the glyphs with a little kerning
65          newAttrString.useMinimumSpacing(1);
66          //newAttrString.useMonospacing(0);
67          // shift string to a random spot in the output imge
68          newAttrString.moveTo(x, y);
69          // now draw each glyph at the appropriate spot on the image.
70          if (isManageColorPerGlyph())
71              newAttrString.drawString(g2, getColorGenerator());
72          else
73              newAttrString.drawString(g2);
74  
75          g2.dispose();
76          return out;
77      }
78  }