KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > store > images > ImageMagickResizer


1 /*
2  * Created on Sep 20, 2005
3  */

4 package com.openedit.store.images;
5
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import com.openedit.OpenEditException;
14 import com.openedit.modules.image.ImageResizer;
15 import com.openedit.util.Exec;
16
17 public class ImageMagickResizer extends ImageResizer
18 {
19     protected String JavaDoc fieldCommandName;
20     protected String JavaDoc fieldGhostScriptCommand;
21     
22     private static final Log log = LogFactory.getLog(ImageMagickResizer.class);
23     public void resizeImage(File JavaDoc inFile, File JavaDoc inOutFile) throws Exception JavaDoc
24     {
25         if ( inFile.getName().toLowerCase().endsWith(".avi"))
26         {
27             //skip movies since we tend to crash on them
28
//TODO: Plug ina smart lookup table for getting the right image resizer for each type of image
29
return;
30         }
31         List JavaDoc com = new ArrayList JavaDoc();
32         com.add(getCommandName());
33         com.add(inFile.getAbsolutePath());
34         com.add("-resize");
35         com.add( (int)getMaxScaledSize().getWidth() + "x" + (int)getMaxScaledSize().getHeight() + ">" );
36         com.add("-colorspace");
37         com.add("rgb");
38         com.add(inOutFile.getAbsolutePath());
39         inOutFile.getParentFile().mkdirs();
40         long start = System.currentTimeMillis();
41         if ( runExec(com) )
42         {
43             log.info("Resize complete in:" + (System.currentTimeMillis() - start) + " " + inOutFile.getName());
44         }
45     }
46
47     public void resizeThumbnailImage(File JavaDoc inFile, File JavaDoc inOutFile) throws Exception JavaDoc
48     {
49         
50         if ( inFile.getName().toLowerCase().endsWith(".avi"))
51         {
52             //skip movies since we tend to crash on them
53
//TODO: Plug ina smart lookup table for getting the right image resizer for each type of image
54
return;
55         }
56
57         
58         List JavaDoc com = new ArrayList JavaDoc();
59         com.add(getCommandName());
60         com.add(inFile.getAbsolutePath());
61         com.add("-thumbnail");
62         com.add( (int)getMaxScaledSize().getWidth() + "x" + (int)getMaxScaledSize().getHeight() + ">" );
63         com.add(inOutFile.getAbsolutePath());
64         inOutFile.getParentFile().mkdirs();
65         long start = System.currentTimeMillis();
66         if ( runExec(com) )
67         {
68             log.info("Resize complete in:" + (System.currentTimeMillis() - start) + " " + inOutFile.getName());
69         }
70     }
71     protected boolean runExec(List JavaDoc inCom) throws OpenEditException
72     {
73         Exec exec = new Exec();
74         exec.setTrackOutput(false);
75         boolean works = exec.runExec(inCom);
76         if ( !works )
77         {
78             log.info("Resize failed running again with output tracking");
79             exec.setTrackOutput(true);
80             works = exec.runExec(inCom);
81             log.info(exec.getStandardOutput() + " error output:" + exec.getErrorOutput() );
82         }
83         return works;
84     }
85
86     public String JavaDoc getCommandName()
87     {
88         if (fieldCommandName == null)
89         {
90             String JavaDoc env = System.getProperty("IMAGE_MAGICK_COMMAND");
91             if ( env == null)
92             {
93                 env = System.getenv("IMAGE_MAGICK_COMMAND");
94             }
95             if ( env == null)
96             {
97                 env = "convert";
98             }
99             fieldCommandName = env;
100         }
101
102         return fieldCommandName;
103     }
104     public void setGhostScriptCommand( String JavaDoc inName)
105     {
106         fieldGhostScriptCommand = inName;
107     }
108     public String JavaDoc getGhostScriptCommand()
109     {
110         if (fieldGhostScriptCommand == null)
111         {
112             String JavaDoc env = System.getProperty("GS_COMMAND");
113             if ( env == null)
114             {
115                 env = System.getenv("GS_COMMAND");
116             }
117             if ( env == null)
118             {
119                 if ( System.getProperty("os.name").indexOf("Windows") > -1)
120                 {
121                     env = "gswin32c.exe";
122                 }
123                 else
124                 {
125                     env = "gs";
126                 }
127             }
128             fieldGhostScriptCommand = env;
129         }
130
131         return fieldGhostScriptCommand;
132         
133     }
134     public void setCommandName(String JavaDoc inCommandName)
135     {
136         fieldCommandName = inCommandName;
137     }
138
139     public boolean convert(File JavaDoc inIn, File JavaDoc inOut) throws OpenEditException
140     {
141         //if this is an eps output from an ai input then use ghostscript
142
boolean iseps = inOut.getPath().toLowerCase().endsWith("eps")
143             && inIn.getPath().toLowerCase().endsWith("ai");
144         
145         List JavaDoc com = new ArrayList JavaDoc();
146         if ( iseps )
147         {
148             com.add(getGhostScriptCommand());
149             
150             com.add("-q" );
151             com.add("-dBATCH" );
152             com.add("-dSAFER" );
153             com.add("-dNOPAUSE" );
154             com.add("-sDEVICE=epswrite" );
155             com.add("-sOutputFile=\"" + inOut.getAbsolutePath() + "\"");
156             com.add("\"" + inIn.getAbsolutePath() + "\"");
157             
158             //-q -dBATCH -dSAFER -dMaxBitmap=500000000 -dNOPAUSE -dAlignToPixels=0
159

160         }
161         else
162         {
163             com.add(getCommandName());
164             com.add(inIn.getAbsolutePath());
165             com.add(inOut.getAbsolutePath());
166         }
167         inOut.getParentFile().mkdirs();
168         long start = System.currentTimeMillis();
169         if ( runExec(com) )
170         {
171             log.info("Resize complete in:" + (System.currentTimeMillis() - start) + " output " + inOut.getAbsolutePath() );
172             return true;
173         }
174         else
175         {
176             return false;
177         }
178         
179     }
180 }
181
Popular Tags