1
2
3
4
5
6
7 package com.octo.captcha.component.image.wordtoimage;
8
9 import com.octo.captcha.CaptchaException;
10
11 import java.awt.*;
12 import java.awt.image.BufferedImage;
13 import java.text.AttributedString;
14
15 /***
16 * <p>Simple image to word implementation. For eductation only, do not use it in production</p>
17 *
18 * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
19 * @version 1.0
20 */
21 public class SimpleWordToImage extends AbstractWordToImage {
22
23 public SimpleWordToImage() {
24 super();
25 }
26
27 /***
28 * @return the max word length accepted by this word2image service
29 */
30 public int getMaxAcceptedWordLength() {
31 return 10;
32 }
33
34 /***
35 * @return the min word length accepted by this word2image service
36 */
37 public int getMinAcceptedWordLength() {
38 return 1;
39 }
40
41 /***
42 * @return the max word lenght accepted by this word2image service
43 * @deprecated misspelled, use {@link #getMaxAcceptedWordLength()} instead
44 */
45 public int getMaxAcceptedWordLenght() {
46 return 10;
47 }
48
49 /***
50 * @return the min word lenght accepted by this word2image service
51 * @deprecated misspelled, use {@link #getMinAcceptedWordLength()} instead
52 */
53 public int getMinAcceptedWordLenght() {
54 return 1;
55 }
56
57
58 /***
59 * @return the generated image height
60 */
61 public int getImageHeight() {
62 return 50;
63 }
64
65 /***
66 * @return the generated image width
67 */
68 public int getImageWidth() {
69 return 100;
70 }
71
72 /***
73 * @return the min font size for the generated image
74 */
75 public int getMinFontSize() {
76 return 10;
77 }
78
79 /***
80 * Method from imageFromWord method to apply font to String. Implementations must take into account the minFontSize
81 * and the MaxFontSize.
82 *
83 * @return a Font
84 */
85 public Font getFont() {
86 return GraphicsEnvironment.getLocalGraphicsEnvironment()
87 .getAllFonts()[0];
88 }
89
90 /***
91 * Generates a backround image on wich text will be paste. Implementations must take into account the imageHeigt and
92 * imageWidth.
93 *
94 * @return the background image
95 */
96 public BufferedImage getBackground() {
97 BufferedImage background = new BufferedImage(getImageWidth(),
98 getImageHeight(), BufferedImage.TYPE_INT_RGB);
99 return background;
100 }
101
102 /***
103 * Pastes the attributed string on the backround image and return the final image. Implementation must take into
104 * account the fact that the text must be readable by human and non by programs
105 *
106 * @return the final image
107 *
108 * @throws CaptchaException if any exception accurs during paste routine.
109 */
110 BufferedImage pasteText(BufferedImage background,
111 AttributedString attributedWord)
112 throws CaptchaException {
113
114 Graphics graph = background.getGraphics();
115
116
117 int x = (getImageWidth() - getMaxAcceptedWordLength()) / 2;
118 int y = (getImageHeight() - getMinFontSize()) / 2;
119 graph.drawString(attributedWord.getIterator(), x, y);
120 graph.dispose();
121 return background;
122 }
123
124 }