1 package SnowMailClient.utils; 2 3 import java.io.*; 4 import java.nio.*; 5 import java.util.*; 6 import javax.imageio.*; 7 import java.awt.image.*; 8 9 10 18 public class PicRenamer 19 { 20 21 public PicRenamer(File base) 22 { 23 Vector<File> im = new Vector<File>(); 24 getImagesRecurse(im, base); 25 26 System.out.println(""+im.size()+" images found in "+base.getAbsolutePath()); 27 28 String [] wfn = ImageIO.getWriterFormatNames(); 29 System.out.println("Supported Write Format"); 30 for(int i=0; i<wfn.length; i++) 31 { 32 System.out.println(" "+wfn[i]); 33 } 34 System.out.println(".\n"); 35 36 for(int i=0; i<im.size(); i++) 37 { 38 try{ 39 convertFiles( im.elementAt(i)); 40 } catch(Exception e) {e.printStackTrace();} 41 } 42 43 } 44 45 46 48 public void convertFiles(File in) throws Exception 49 { 50 BufferedImage im = ImageIO.read(in); 51 String outName = in.getAbsolutePath(); 52 int posPt = outName.lastIndexOf("."); 53 String originalExtension = outName.substring(posPt+1).toUpperCase(); 54 55 in.delete(); 57 58 if(originalExtension.equals("PNG") || originalExtension.equals("GIF")) 59 { 60 outName = outName.substring(0,posPt) + ".PNG"; 61 ImageIO.write(im, "PNG", new File(outName)); 63 } 64 else 65 { 66 outName = outName.substring(0,posPt) + ".JPEG"; 67 System.out.println("writing "+outName); 68 ImageIO.write(im, "JPEG", new File(outName)); 69 } 70 } 71 72 73 74 public void getImagesRecurse(Vector<File> im, File base) 75 { 76 for(File fi : base.listFiles()) 77 { 78 if(fi.isDirectory()) 79 { 80 getImagesRecurse(im, fi); 81 } 82 else 83 { 84 String name = fi.getName().toUpperCase(); 85 if(name.endsWith(".JPG")) im.add(fi); 86 if(name.endsWith(".BMP")) im.add(fi); 87 if(name.endsWith(".PNG")) im.add(fi); 88 if(name.endsWith(".GIF")) im.add(fi); 89 } 90 } 91 } 92 93 96 public static void main( String [] arguments ) 97 { 98 new PicRenamer(new File("e:/temp/st")); 99 } 101 102 103 } | Popular Tags |