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.wordtoimage;
8   
9   import com.octo.captcha.CaptchaException;
10  import com.octo.captcha.component.image.backgroundgenerator.BackgroundGenerator;
11  import com.octo.captcha.component.image.deformation.ImageDeformation;
12  import com.octo.captcha.component.image.fontgenerator.FontGenerator;
13  import com.octo.captcha.component.image.textpaster.TextPaster;
14  
15  import java.awt.*;
16  import java.awt.image.BufferedImage;
17  import java.text.AttributedString;
18  import java.util.*;
19  import java.util.List;
20  
21  /***
22   * <p>This implementation uses deformation components to distord the image. </br>It takes three array of deformations :
23   * for the background image, for the text only, and for the final image it proceeds as folows : <ul> <li>Checks the word
24   * length</li> <li>Creates an java.text.AttributedString from the word</li> <li>Create an image for the background a
25   * BackgroundGenerator component</li> <li>Apply background deformations</li> <li>Apply font to the AttributedString
26   * using the abstract method getFont</li> <li>Create a transparent backround </li> <li>Put the text on the transparent
27   * backround using the abstact method pasteText</li> <li>Apply the text deformations </li> <li>Paste the transparent
28   * image using an alpha composite</li> <li>Apply the final deformations </li> <li>Return the newly created image</li>
29   * </ul>
30   *
31   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
32   * @version 1.0
33   */
34  public class DeformedComposedWordToImage extends ComposedWordToImage {
35  
36      private List<ImageDeformation> backgroundDeformations = new ArrayList<ImageDeformation>();
37      private List<ImageDeformation> textDeformations = new ArrayList<ImageDeformation>();
38      private List<ImageDeformation> finalDeformations = new ArrayList<ImageDeformation>();
39  
40  
41  
42      /***
43       * Composed word to image that applys filters
44       *
45       * @param fontGenerator         a AbstractFontGenerator to implement the getFont() method
46       * @param background            a AbstractBackgroundGenerator to implement the getBackround() method
47       * @param textPaster            a AbstractTextParser to implement the pasteText() method
48       * @param backgroundDeformation to be apply on the background image
49       * @param textDeformation       to be apply on the text image
50       * @param finalDeformation      to be apply on the final image
51       */
52      public DeformedComposedWordToImage(FontGenerator fontGenerator,
53                                         BackgroundGenerator background,
54                                         TextPaster textPaster,
55                                         ImageDeformation backgroundDeformation,
56                                         ImageDeformation textDeformation,
57                                         ImageDeformation finalDeformation) {
58          super(fontGenerator, background, textPaster);
59          if(backgroundDeformation !=null)this.backgroundDeformations.add(backgroundDeformation);
60          if(textDeformation !=null) this.textDeformations.add(textDeformation);
61          if(finalDeformation !=null)this.finalDeformations.add(finalDeformation);
62      }
63  
64      /***
65       * Composed word to image that applys filters
66       *
67       * @param fontGenerator         a AbstractFontGenerator to implement the getFont() method
68       * @param background            a AbstractBackgroundGenerator to implement the getBackround() method
69       * @param textPaster            a AbstractTextParser to implement the pasteText() method
70       * @param backgroundDeformations to be apply on the background image
71       * @param textDeformations      to be apply on the text image
72       * @param finalDeformations      to be apply on the final image
73       */
74      public DeformedComposedWordToImage(FontGenerator fontGenerator,
75                                         BackgroundGenerator background,
76                                         TextPaster textPaster,
77                                         List<ImageDeformation> backgroundDeformations,
78                                          List<ImageDeformation> textDeformations,
79                                         List<ImageDeformation> finalDeformations) {
80          super(fontGenerator, background, textPaster);
81          this.backgroundDeformations = backgroundDeformations;
82          this.textDeformations = textDeformations;
83          this.finalDeformations = finalDeformations;
84      }
85  
86      public DeformedComposedWordToImage(boolean manageFontByCharacter, FontGenerator fontGenerator, BackgroundGenerator background, TextPaster textPaster, List<ImageDeformation> backgroundDeformations, List<ImageDeformation> textDeformations, List<ImageDeformation> finalDeformations) {
87          super(manageFontByCharacter, fontGenerator, background, textPaster);
88          this.backgroundDeformations = backgroundDeformations;
89          this.textDeformations = textDeformations;
90          this.finalDeformations = finalDeformations;
91      }
92  
93      /***
94       * Creates an image of the provided String This method is a skeleton for creation algorithm. it proceeds as folows :
95       * <ul> <li>Checks the word length</li> <li>Creates an java.text.AttributedString from the word</li> <li>Create an
96       * image for the background using the abstact method getBackround</li> <li>Apply background filters</li> <li>Apply
97       * font to the AttributedString using the abstract method getFont</li> <li>Create a transparent backround </li>
98       * <li>Put the text on the transparent backround using the abstact method pasteText</li> <li>Apply the text filters
99       * </li> <li>Paste the transparent image using an alpha composite</li> <li>Apply the final filters </li> <li>Return
100      * the newly created image</li> </ul>
101      *
102      * @return an image representation of the word
103      *
104      * @throws com.octo.captcha.CaptchaException
105      *          if word is invalid or if image generation fails.
106      */
107     public BufferedImage getImage(String word) throws CaptchaException {
108         BufferedImage background = getBackground();
109         AttributedString aword = getAttributedString(word, checkWordLength(word));
110         //copy background
111         BufferedImage out = new BufferedImage(background.getWidth(), background.getHeight(),
112                 background.getType());
113         Graphics2D g2 = (Graphics2D) out.getGraphics();
114         //paste background
115         g2.drawImage(background, 0, 0, out.getWidth(), out.getHeight(), null);
116         g2.dispose();
117         //apply filters to backround
118         for (ImageDeformation deformation:backgroundDeformations) {
119             out = deformation.deformImage(out);
120         }
121 
122 
123         //paste text on a transparent background
124         BufferedImage transparent = new BufferedImage(out.getWidth(), out.getHeight(),
125                 BufferedImage.TYPE_INT_ARGB);
126 
127         //use textpaster to paste the text
128         transparent = pasteText(transparent, aword);
129 
130         //and apply deformation
131       for (ImageDeformation deformation:textDeformations) {
132             transparent = deformation.deformImage(transparent);
133         }
134 
135 
136         Graphics2D g3 = (Graphics2D) out.getGraphics();
137 
138         g3.drawImage(transparent, 0, 0, null);
139         g3.dispose();
140         //apply final deformation
141         for (ImageDeformation deformation:finalDeformations) {
142             out = deformation.deformImage(out);
143         }
144        
145         return out;
146 
147     }
148 }