01: package snow.utils;
02:
03: import java.io.*;
04: import java.nio.*;
05: import java.util.*;
06: import javax.imageio.*;
07: import java.awt.image.*;
08:
09: /** convert/rewrite all images to PNG and jpeg in a directory and subdirectories
10:
11: PNG => PNG
12: the rest => JPEG
13: Goal: progressive jpegs cause JVM crash when embedded in jar files !!
14: */
15: public class PicRenamer {
16: public PicRenamer(File base) {
17: Vector<File> im = new Vector<File>();
18: getImagesRecurse(im, base);
19:
20: System.out.println("" + im.size() + " images found in "
21: + base.getAbsolutePath());
22:
23: String[] wfn = ImageIO.getWriterFormatNames();
24: System.out.println("Supported Write Format");
25: for (int i = 0; i < wfn.length; i++) {
26: System.out.println(" " + wfn[i]);
27: }
28: System.out.println(".\n");
29:
30: for (int i = 0; i < im.size(); i++) {
31: try {
32: convertFiles(im.elementAt(i));
33: } catch (Exception e) {
34: e.printStackTrace();
35: }
36: }
37: } // Constructor
38:
39: /** convert GIF in PNG and rewrite jpegs
40: */
41: public void convertFiles(File in) throws Exception {
42: BufferedImage im = ImageIO.read(in);
43: String outName = in.getAbsolutePath();
44: int posPt = outName.lastIndexOf('.');
45: String originalExtension = outName.substring(posPt + 1)
46: .toUpperCase();
47:
48: // delete the original
49: in.delete();
50:
51: if (originalExtension.equals("PNG")
52: || originalExtension.equals("GIF")) {
53: outName = outName.substring(0, posPt) + ".PNG";
54: //System.out.println("writing "+outName);
55: ImageIO.write(im, "PNG", new File(outName));
56: } else {
57: outName = outName.substring(0, posPt) + ".JPEG";
58: System.out.println("writing " + outName);
59: ImageIO.write(im, "JPEG", new File(outName));
60: }
61: }
62:
63: public void getImagesRecurse(Vector<File> im, File base) {
64: for (File fi : base.listFiles()) {
65: if (fi.isDirectory()) {
66: getImagesRecurse(im, fi);
67: } else {
68: String name = fi.getName().toUpperCase();
69: if (name.endsWith(".JPG"))
70: im.add(fi);
71: if (name.endsWith(".BMP"))
72: im.add(fi);
73: if (name.endsWith(".PNG"))
74: im.add(fi);
75: if (name.endsWith(".GIF"))
76: im.add(fi);
77: }
78: }
79: }
80:
81: public static void main(String[] arguments) {
82: new PicRenamer(new File("c:/sources/other/mail/client"));
83: } // main
84:
85: } // PicRenamer
|