1
2
3
4
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
74
75
76
77 MutableAttributedString newAttrString = new MutableAttributedString(g2,
78 attributedString, kerning);
79
80
81 newAttrString.useMinimumSpacing(kerning);
82
83 newAttrString.moveToRandomSpot(background);
84
85 if (isManageColorPerGlyph())
86 newAttrString.drawString(g2, getColorGenerator());
87 else
88 newAttrString.drawString(g2);
89
90 g2.dispose();
91 return out;
92 }
93
94 }