批量转换bmp文件为jpg
2009-09-09 00:00:00 来源:WEB开发网Java代码
package image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
public class Bmp2Jpeg{
public static ArrayList<File> getFiles(File dir) {
ArrayList files = new ArrayList<File>();
File list[] = dir.listFiles();
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
System.out.println("ignore directory");
} else if (!(list[i].getName().endsWith(".bmp") || list[i].getName().endsWith(".BMP"))) {
System.out.println("ignore not bmp file :" + list[i].getName());
} else if (list[i].isFile()) {
files.add(list[i]);
}
}
return files;
}
public static void main(String[] args) {
File f1 = new File(args[0]);
ArrayList<File> files = getFiles(f1);
for (File file : files) {
try {
BufferedImage bi = ImageIO.read(file);
String s = File.separator + file.getName().substring(0, file.getName().length() - 4) + ".jpg";
File f2 = new File(args[1] + s);
ImageIO.write(bi, "jpeg", f2);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("/********\n" + file.getName() + " is dealed!");
}
}
}
更多精彩
赞助商链接