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   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  
13  /*
14   * jcaptcha, the open source java framework for captcha definition and integration
15   * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
16   * See the LICENSE.txt file distributed with this package.
17   */
18  
19  package com.octo.captcha.engine.image.utils;
20  
21  import com.octo.captcha.engine.image.ImageCaptchaEngine;
22  import com.octo.captcha.engine.image.fisheye.SimpleFishEyeEngine;
23  import com.octo.captcha.engine.image.gimpy.*;
24  import com.octo.captcha.image.ImageCaptcha;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.text.DecimalFormat;
29  
30  /***
31   * This utility class lets you create JPEG files with a particular
32   *
33   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
34   * @author <a href="mailto:travis.winfrey@gmail.com">Travis Winfrey</a>
35   * @version 1.0
36   */
37  public class ImageCaptchaToJPEG {
38  
39      private static boolean SHOULD_DELETE_OLD_JPEGS_FIRST = true;
40  
41      public static void main(String[] args) throws Exception {
42          if (args.length < 2) {
43              System.out.println("Usage : engineClassName outputDir iterations");
44              System.out.println("If engineClassName is 'all', then several Gimpy Engines are used");
45              System.exit(1);
46          }
47  
48  
49          String className = args[0];
50          File outputDir = new File(args[1]);
51          String iterationsString = args[2];
52          int iterations = Integer.parseInt(iterationsString);
53  
54          System.out.println("args : " +
55                  "image captcha engine class='" + className + "'" +
56                  ", output dir='" + outputDir + "'" +
57                  ",iterations='" + iterationsString + "'");
58  
59          clearOutputDirectory(outputDir);
60  
61          ImageCaptchaEngine pixCapchaEngine = null;
62          if (className.equals("all")) {
63              ImageCaptchaEngine[] engines = {
64                      new BaffleListGimpyEngine(),
65                      new DefaultGimpyEngine(),
66                      new DeformedBaffleListGimpyEngine(),
67                      new SimpleListImageCaptchaEngine(),
68                      new SimpleFishEyeEngine()
69              };
70              for (int i = 0; i < engines.length; i++) {
71                  pixCapchaEngine = engines[i];
72                  System.out.println("Beginning generation with " + pixCapchaEngine.getClass().getName());
73                  try {
74                      generate(iterations, pixCapchaEngine, outputDir);
75                  }
76                  catch (Exception e) {
77                      System.out.println("Errors with class " + pixCapchaEngine.getClass().getName());
78                  }
79              }
80          } else {
81  
82              try {
83                  pixCapchaEngine = (ImageCaptchaEngine) Class.forName(className).newInstance();
84              }
85              catch (Exception e) {
86                  System.out.println("Couldn't initialize '" + className + "', trying a likely package prefix");
87                  String defaultClassPrefix = "com.octo.captcha.engine.image.gimpy.";
88                  try {
89                      pixCapchaEngine = (ImageCaptchaEngine) Class.forName(defaultClassPrefix + className).newInstance();
90                  }
91                  catch (Exception e2) {
92                      System.out.println("Couldn't initialize '" + className + " -- specify a fully attributed name");
93                      System.exit(1);
94                  }
95              }
96  
97              generate(iterations, pixCapchaEngine, outputDir);
98          }
99  
100         System.exit(0);
101     }
102 
103     private static void clearOutputDirectory(File outputDir) {
104         if (SHOULD_DELETE_OLD_JPEGS_FIRST) {
105             File[] files = outputDir.listFiles();
106             if (files == null) {
107                 return;
108             }
109             if (files.length > 2) {
110                 // skip ., .. entries
111                 System.out.println("Deleting about " + (files.length - 2) + " jpeg files");
112             }
113             for (int i = 0; i < files.length; i++) {
114                 File f = files[i];
115                 if (f.isFile() && f.getName().endsWith("jpg")) {
116                     f.delete();
117                 }
118             }
119         }
120     }
121 
122     private static void generate(int iterations, ImageCaptchaEngine pixCaptchaEngine, File outputDir) throws IOException {
123 
124         outputDir.mkdirs();
125         String className = pixCaptchaEngine.getClass().getName().substring(pixCaptchaEngine.getClass().getPackage().getName().length() + 1);
126 
127         System.out.println("Starting on " + className);
128 
129         long sumImageCreation = 0;
130         long sumFileCreation = 0;
131         int i = 0;
132         try {
133             for (i = 0; i < iterations; i++) {
134                 long t = System.currentTimeMillis();
135                 ImageCaptcha captcha = pixCaptchaEngine.getNextImageCaptcha();
136                 sumImageCreation += System.currentTimeMillis() - t;
137                 t = System.currentTimeMillis();
138                 File outputFile = new File(outputDir, File.separator + className + "Captcha_" + i + ".jpg");
139                 ImageToFile.serialize(captcha.getImageChallenge(), outputFile);
140                 sumFileCreation += System.currentTimeMillis() - t;
141                 System.out.print(".");
142                 if (i % 100 == 99) {
143                     System.out.println("");
144                 }
145             }
146         }
147         finally {
148             if (i < iterations) {
149                 System.out.println("exited early! i=" + i);
150             } else {
151                 System.out.println("done");
152             }
153             DecimalFormat df = new DecimalFormat();
154             System.out.println("Summary for " + className + ":" +
155                     " avg image creation = " + df.format(sumImageCreation / iterations) + " milliseconds/image," +
156                     " avg file creation = " + df.format(sumFileCreation / iterations) + " milliseconds/file");
157         }
158     }
159 
160 }