001/* 002 * $Source: /cvsroot2/open/projects/WebARTS/ca/bc/webarts/widgets/ReduceImage.java,v $ 003 * ReduceImage.java - A widget to grab URLs through a proxy. 004 * 005 * $Revision: 1006 $ 006 * $Date: 2015-10-11 19:06:06 -0700 (Sun, 11 Oct 2015) $ 007 * $Locker: $ 008 * 009 * 010 * Written by Tom Gutwin - WebARTS Design. 011 * Copyright (C) 2003 WebARTS Design, North Vancouver Canada 012 * http://www.webarts.bc.ca 013 * 014 * This program is free software; you can redistribute it and/or modify 015 * it under the terms of the GNU General Public License as published by 016 * the Free Software Foundation; either version 2 of the License, or 017 * (at your option) any later version. 018 * 019 * This program is distributed in the hope that it will be useful, 020 * but WITHOUT ANY WARRANTY; without even the implied warranty of 021 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 022 * GNU General Public License for more details. 023 * 024 * You should have received a copy of the GNU General Public License 025 * along with this program; if not, write to the Free Software 026 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 027 */ 028package ca.bc.webarts.tools; 029 030import java.io.File; 031 032import ca.bc.webarts.widgets.Util; 033import ca.bc.webarts.tools.NativeAppLauncher; 034 035/** 036 * A tool to downsize/grade graphic files in a dir and save the results in a 037 * subdir. It does the processing by calling the native ImageMagik 'convert' 038 * command. The actual impl of this class will provide the work method that 039 * performs the actual image reduction 040 * 041 * <pre> 042 * Usage: 043 * java ca.bc.webarts.widgets.ReduceImage [directory] 044 * 045 * The directory is not required... if NOT spec'd the current dir will be processed. 046 *</pre> 047 * 048 * @author Tom Gutwin P.Eng 049 **/ 050public abstract class ReduceImage 051{ 052 /** A holder for the NA Clients System File Separator. **/ 053 protected static final String SYSTEM_FILE_SEPERATOR = File.separator; 054 055 /** 056 * A holder for the directory location to save downloads. DEFAULT is the 057 * current working dir. 058 **/ 059 protected static final String DEFAULT_SAVE_LOCATION = 060 System.getProperty("user.dir") + SYSTEM_FILE_SEPERATOR+"reduced" 061 + SYSTEM_FILE_SEPERATOR; 062 063 /** The default reduduction size percentage. **/ 064 protected static final int DEFAULT_REDUCTION_PERCENTAGE = 40; 065 066 /** The default reduced size X. **/ 067 protected static final int DEFAULT_REDUCED_X = 800; 068 069 /** The default reduced size Y. **/ 070 protected static final int DEFAULT_REDUCED_Y = 600; 071 072 /** The default reduduction quality. **/ 073 protected static final int DEFAULT_REDUCTION_QUALITY = 75; 074 075 /** The default image fileextension to look for to convert. **/ 076 protected static final String DEFAULT_FILE_EXTENSION = ".jpg"; 077 protected static final String SECONDARY_FILE_EXTENSION = ".jpeg"; 078 079 /** The reduduction size percentage. **/ 080 protected int reduceByPercentage_ = DEFAULT_REDUCTION_PERCENTAGE; 081 082 /** The reduced size X. **/ 083 protected int reduceSizeX_ = DEFAULT_REDUCED_X; 084 085 /** The reduced size X. **/ 086 protected int reduceSizeY_ = DEFAULT_REDUCED_Y; 087 088 /** The file extension to search for. **/ 089 //protected String imageFileExtension_ = DEFAULT_FILE_EXTENSION; 090 091 /** The dir name where the reduced files will go. **/ 092 protected String saveLocation_ = DEFAULT_SAVE_LOCATION; 093 094 /** The dir name where the reduced files will go. **/ 095 protected boolean dateStampImage_ = false; 096 097 098 /** 099 * Does the recursive grunt work to convert a whole dir of files by calling 100 * the impls reduceFile method. 101 * 102 * @param fileName is the name of the file to convert. 103 * @param reductionSizePercent the size to reduce the image by in percent. 104 * @param reducedQuality is the name quality param for the reduction 105 * (jpg or png might be spec'd as 75). 106 * @param recurseDir is a flag to recursing on sub-dirs 107 * @return the conversion apps result code. 108 **/ 109 public int reduceDir(String fileName, 110 int reductionSizePercent, 111 int reducedQuality, 112 boolean recurseDir) 113 { 114 int retVal = 0; 115 File dirToConvert = new File(fileName); 116 if (dirToConvert!=null && dirToConvert.exists()) 117 { 118 String[] files = dirToConvert.list(); 119 int counter = 0; 120 File tmpFile = null; 121 int tmpRet = 0; 122 System.out.println("Reducing Dir ("+reductionSizePercent+","+reducedQuality+"): "+fileName+ " ("+files.length+" files)"); 123 saveLocation_ = fileName + SYSTEM_FILE_SEPERATOR+"reduced"+ SYSTEM_FILE_SEPERATOR; 124 //loop through all the files in the dir 125 for (int i = 0; i < files.length; i++) 126 { 127 tmpFile = new File(fileName+ SYSTEM_FILE_SEPERATOR+files[i]); 128 tmpRet = 0; 129 if (tmpFile != null && tmpFile.isDirectory() && tmpFile.canRead() 130 && recurseDir) 131 { 132 if ((tmpRet = reduceDir(files[i], reductionSizePercent, 133 reducedQuality, recurseDir)) != 0) 134 retVal = tmpRet; 135 } 136 else 137 { 138 if (tmpFile != null && !tmpFile.isDirectory() && tmpFile.canRead() && 139 ( files[i].trim().toLowerCase().endsWith(this.getFileExtension()) || 140 files[i].trim().toLowerCase().endsWith(this.getSecondaryFileExtension()) 141 ) 142 ) 143 { 144 //System.out.println("Reducing File ("+reductionSizePercent+","+reducedQuality+"): "+fileName+ SYSTEM_FILE_SEPERATOR+files[i]); 145 if ((tmpRet = reduceFile(fileName+ SYSTEM_FILE_SEPERATOR+files[i], 146 reductionSizePercent, 147 reducedQuality)) != 0) 148 retVal = tmpRet; 149 } 150 else 151 { 152 System.out.println(" WHAT THE Putz."); 153 } 154 } 155 } 156 } 157 return retVal; 158 } 159 160 161 /** 162 * Does the recursive grunt work to convert a whole dir of files by calling 163 * the impls reduceFile method. 164 * 165 * @param fileName is the name of the file to convert. 166 * @param reducedSizeX the X size to reduce the image by . 167 * @param reducedSizeY the X size to reduce the image by . 168 * @param reducedQuality is the name quality param for the reduction 169 * (jpg or png might be spec'd as 75). 170 * @param recurseDir is a flag to recursing on sub-dirs 171 * @return the conversion apps result code. 172 **/ 173 public int reduceDir(String fileName, 174 int reducedSizeX, 175 int reducedSizeY, 176 int reducedQuality, 177 boolean recurseDir) 178 { 179 int retVal = 0; 180 File dirToConvert = new File(fileName); 181 if (dirToConvert!=null && dirToConvert.exists()) 182 { 183 String[] files = dirToConvert.list(); 184 int counter = 0; 185 File tmpFile = null; 186 int tmpRet = 0; 187 System.out.println("Reducing Dir ("+reducedSizeX+"x"+reducedSizeY+","+reducedQuality+"): "+fileName+ " ("+files.length+" files)"); 188 saveLocation_ = fileName + SYSTEM_FILE_SEPERATOR+"reduced"+ SYSTEM_FILE_SEPERATOR; 189 //loop through all the files in the dir 190 for (int i = 0; i < files.length; i++) 191 { 192 tmpFile = new File(fileName+ SYSTEM_FILE_SEPERATOR+files[i]); 193 tmpRet = 0; 194 if (tmpFile != null && tmpFile.isDirectory() && tmpFile.canRead() 195 && recurseDir) 196 { 197 if ((tmpRet = reduceDir(files[i], reducedSizeX,reducedSizeY, 198 reducedQuality, recurseDir)) != 0) 199 retVal = tmpRet; 200 } 201 else 202 { 203 if (tmpFile != null && !tmpFile.isDirectory() && tmpFile.canRead() && 204 ( files[i].trim().toLowerCase().endsWith(this.getFileExtension()) || 205 files[i].trim().toLowerCase().endsWith(this.getSecondaryFileExtension()) 206 ) 207 ) 208 { 209 //System.out.println("Reducing File ("+reductionSizePercent+","+reducedQuality+"): "+fileName+ SYSTEM_FILE_SEPERATOR+files[i]); 210 if ((tmpRet = reduceFile(fileName+ SYSTEM_FILE_SEPERATOR+files[i], 211 reducedSizeX,reducedSizeY, 212 reducedQuality)) != 0) 213 retVal = tmpRet; 214 } 215 else 216 { 217 System.out.println(" WHAT THE Putz."); 218 } 219 } 220 } 221 } 222 return retVal; 223 } 224 225 226 /** 227 * Does the grunt work to reduce/convert a single file, 228 * THIS IS THE METHOD THAT MUST BE IMPLEMENTED. After this method is finished, 229 * a new image file should reside in the reduced dir. 230 * 231 * @param fileName is the name of the file to convert. 232 * @param reductionSizePercent the size to reduce the image by in percent. 233 * @param reducedQuality is the name quality param for the reduction 234 * (jpg or png might be spec'd as 75). 235 * @return the conversion apps result code. 236 **/ 237 public abstract int reduceFile(String fileName, 238 int reductionSizePercent, 239 int reducedQuality); 240 241 /** 242 * Does the grunt work to reduce/convert a single file, 243 * THIS IS THE METHOD THAT MUST BE IMPLEMENTED. After this method is finished, 244 * a new image file should reside in the reduced dir. 245 * 246 * @param fileName is the name of the file to convert. 247 * @param reducedSizeX the X size to reduce the image by . 248 * @param reducedSizeY the X size to reduce the image by . 249 * @param reducedQuality is the name quality param for the reduction 250 * (jpg or png might be spec'd as 75). 251 * @return the conversion apps result code. 252 **/ 253 public abstract int reduceFile(String fileName, 254 int reducedSizeX, 255 int reducedSizeXY, 256 int reducedQuality); 257 258 public abstract String getFileExtension(); 259 260 public abstract String getSecondaryFileExtension(); 261} 262