KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > utils > PicRenamer


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 /** convert/rewrite all images to PNG and jpeg in a directory and subdirectories
11
12     PNG => PNG
13     the rest => JPEG
14
15     Goal: progressive jpegs cause JVM crash when embedded in jar files !!
16
17 */

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 JavaDoc[] 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 JavaDoc e) {e.printStackTrace();}
41     }
42
43   }
44
45
46   /** convert GIF in PNG and rewrite jpegs
47   */

48   public void convertFiles(File in) throws Exception JavaDoc
49   {
50     BufferedImage im = ImageIO.read(in);
51     String JavaDoc outName = in.getAbsolutePath();
52     int posPt = outName.lastIndexOf(".");
53     String JavaDoc originalExtension = outName.substring(posPt+1).toUpperCase();
54
55     // delete the original
56
in.delete();
57
58     if(originalExtension.equals("PNG") || originalExtension.equals("GIF"))
59     {
60       outName = outName.substring(0,posPt) + ".PNG";
61       //System.out.println("writing "+outName);
62
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 JavaDoc 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  /**
94   * Static main method
95   */

96   public static void main( String JavaDoc[] arguments )
97   {
98      new PicRenamer(new File("e:/temp/st"));
99   } // main
100

101
102
103 } // PicRenamer
Popular Tags