Advertisement
RedTeaHacker

Convert BMPs to PNGs

Oct 5th, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.54 KB | None | 0 0
  1. import java.io.File;
  2. import javax.imageio.ImageIO;
  3. import java.awt.image.BufferedImage;
  4. import java.io.IOException;
  5.  
  6. /**
  7.  * Convert a directory of BMPs to PNGs and delete BMPs when done.
  8.  *
  9.  * @author RedTeaHacker
  10.  **/
  11. public class Immorphage {
  12.   public static void main(String[] args) {
  13.     if(args.length < 1) {
  14.       System.out.println("[Usage]:java Immorphage <dir> (-test)");
  15.       return;
  16.     }
  17.  
  18.     System.out.println("[Path]:" + args[0]);
  19.     File file = new File(args[0]);
  20.     if(!file.exists()) {
  21.       System.out.println("[Error]:Path does not exist.");
  22.       return;
  23.     }
  24.     if(!file.isDirectory()) {
  25.       System.out.println("[Error]:Path is not a directory.");
  26.       return;
  27.     }
  28.  
  29.     boolean test = args.length > 1;
  30.  
  31.     System.out.println("[Count]:" + recurseFiles(file.listFiles(),"bmp","png",test));
  32.   }
  33.  
  34.   public static int recurseFiles(File[] files,String extension,String morphExtension,boolean test) {
  35.     if(files == null || files.length == 0) {
  36.       return 0;
  37.     }
  38.     int count = 0;
  39.     for(File file: files) {
  40.       if(file.isDirectory()) {
  41.         count += recurseFiles(file.listFiles(),extension,morphExtension,test);
  42.       }
  43.       else {
  44.         String ext = getExtension(file).replaceAll("\\s+","");
  45.         if(ext.equalsIgnoreCase(extension)) {
  46.           morphFile(file,morphExtension,test);
  47.           ++count;
  48.         }
  49.       }
  50.     }
  51.     return count;
  52.   }
  53.  
  54.   public static String getExtension(File file) {
  55.     String name = file.getName();
  56.     if(name == null) {
  57.       return "";
  58.     }
  59.     int i = name.lastIndexOf(".");
  60.     if(i < 1 || (++i) >= name.length()) {
  61.       return "";
  62.     }
  63.     return name.substring(i);
  64.   }
  65.  
  66.   public static void morphFile(File file,String morphExtension,boolean test) {
  67.     File morphFile = new File(getPathWithoutExtension(file) + "." + morphExtension);
  68.     System.out.println("[Morph]:'" + file.getPath() + "'=>'" + morphFile.getPath() + "'");
  69.     if(test) {
  70.       return;
  71.     }
  72.  
  73.     try {
  74.       BufferedImage image = ImageIO.read(file);
  75.       ImageIO.write(image,morphExtension,morphFile);
  76.       file.deleteOnExit();
  77.     }
  78.     catch(IOException ex) {
  79.       ex.printStackTrace();
  80.     }
  81.   }
  82.  
  83.   public static String getPathWithoutExtension(File file) {
  84.     String name = file.getName();
  85.     String path = file.getPath();
  86.     if(name == null || name.length() < 1) {
  87.       return path;
  88.     }
  89.     int i = name.lastIndexOf(".");
  90.     if(i < 1) {
  91.       return path;
  92.     }
  93.     return path.substring(0,path.length() - (name.length() - i));
  94.   }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement