Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import javax.imageio.ImageIO;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- /**
- * Convert a directory of BMPs to PNGs and delete BMPs when done.
- *
- * @author RedTeaHacker
- **/
- public class Immorphage {
- public static void main(String[] args) {
- if(args.length < 1) {
- System.out.println("[Usage]:java Immorphage <dir> (-test)");
- return;
- }
- System.out.println("[Path]:" + args[0]);
- File file = new File(args[0]);
- if(!file.exists()) {
- System.out.println("[Error]:Path does not exist.");
- return;
- }
- if(!file.isDirectory()) {
- System.out.println("[Error]:Path is not a directory.");
- return;
- }
- boolean test = args.length > 1;
- System.out.println("[Count]:" + recurseFiles(file.listFiles(),"bmp","png",test));
- }
- public static int recurseFiles(File[] files,String extension,String morphExtension,boolean test) {
- if(files == null || files.length == 0) {
- return 0;
- }
- int count = 0;
- for(File file: files) {
- if(file.isDirectory()) {
- count += recurseFiles(file.listFiles(),extension,morphExtension,test);
- }
- else {
- String ext = getExtension(file).replaceAll("\\s+","");
- if(ext.equalsIgnoreCase(extension)) {
- morphFile(file,morphExtension,test);
- ++count;
- }
- }
- }
- return count;
- }
- public static String getExtension(File file) {
- String name = file.getName();
- if(name == null) {
- return "";
- }
- int i = name.lastIndexOf(".");
- if(i < 1 || (++i) >= name.length()) {
- return "";
- }
- return name.substring(i);
- }
- public static void morphFile(File file,String morphExtension,boolean test) {
- File morphFile = new File(getPathWithoutExtension(file) + "." + morphExtension);
- System.out.println("[Morph]:'" + file.getPath() + "'=>'" + morphFile.getPath() + "'");
- if(test) {
- return;
- }
- try {
- BufferedImage image = ImageIO.read(file);
- ImageIO.write(image,morphExtension,morphFile);
- file.deleteOnExit();
- }
- catch(IOException ex) {
- ex.printStackTrace();
- }
- }
- public static String getPathWithoutExtension(File file) {
- String name = file.getName();
- String path = file.getPath();
- if(name == null || name.length() < 1) {
- return path;
- }
- int i = name.lastIndexOf(".");
- if(i < 1) {
- return path;
- }
- return path.substring(0,path.length() - (name.length() - i));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement