Java批量将BMP转换为JPG

java

public static void main(String[] args)  throws Exception{
     //前提是文件夹内都是bmp文件,无论层级

transfImg(new File("C:\\Users\\Desktop\\111"));

}

private static void transfImg(File file) throws Exception{

if(file.isDirectory()){

for (File listFile : file.listFiles()) {

transfImg(listFile);

}

}else {

Image img = ImageIO.read(file);

BufferedImage tag = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(img.getScaledInstance(img.getWidth(null), img.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null);

String path = file.getAbsolutePath();

FileOutputStream out = new FileOutputStream(path.substring(0,path.length()-4)+".jpg");

// JPEGImageEncoder可适用于其他图片类型的转换

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

encoder.encode(tag);

out.close();
       //不保留源文件(可选)

file.delete();

}

}

参考https://www.cnblogs.com/snowberg/archive/2011/08/30/2468610.html

以上是 Java批量将BMP转换为JPG 的全部内容, 来源链接: utcz.com/z/390921.html

回到顶部