批量修改图片后缀并重新命名

1.修改图片格式

    /**
     * 修改原图的文件格式
     * @param srcPath 原图路径
     * @param destPath 新图路径
     * @param formatName 图片格式,支持bmp|gif|jpg|jpeg|png
     */
    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.读取文件夹中的图片名称并修改,导出图片

  /**
     * 读取文件夹中的图片名,并导出新图片
     * @param path 原图路径
     * @param newPath 新图路径
     * @param imgName 图片名称  sider.png
     */
    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";//top bottom   endfacel   endfacer  sidel  sider
       String newPath="D:\\1文件资料\\img\\png格式\\sider"; //bottom-abnormal
       String imgName = "_sider.png";
       downLoadImg(path,newPath,imgName);
   } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值