1.修改图片格式
public static boolean modifyImageFormat(String srcPath, String destPath, String formatName) {
boolean isSuccess = false;
InputStream fis = null;
try {
fis = new FileInputStream(srcPath);
BufferedImage bufferedImg = ImageIO.read(fis);
isSuccess = ImageIO.write(bufferedImg, formatName, new File(destPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return isSuccess;
}
2.读取文件夹中的图片名称并修改,导出图片
public static void downLoadImg(String path, String newPath, String imgName) {
File file1 = new File(path);
if (file1.exists()) {
File[] files = file1.listFiles();
for (int i = 0; i < files.length; i++) {
File file2 = files[i];
if (file2.getName().indexOf("bmp")!=-1){
String str = path +"\\"+ file2.getName();
System.out.println("原始图片路径: "+str);
int num = i+1;
String path2=newPath+"\\"+num+imgName;
System.out.println("新图片路径: "+path2);
modifyImageFormat(str,path2,"png");
}
}
}
}
3.调用方法测试
public static void main(String[] args) {
String path="D:\\1文件资料\\img\\bmp格式\\sider";
String newPath="D:\\1文件资料\\img\\png格式\\sider";
String imgName = "_sider.png";
downLoadImg(path,newPath,imgName);
}