首页 > 其他分享 >图片格式转换webp

图片格式转换webp

时间:2022-12-19 11:01:04浏览次数:50  
标签:转换 String 图片格式 webp webpPath pngPath import imageio

今天突然保存梅西夺冠照片,发现google上下载的图片是webp格式,好新颖的格式。

百度了下,webp是google推出的必jpg格式更小的图片格式文件,但是softpage等浏览器还不支持,WIN11设置背景图片也不支持。

既然不支持,那就只有转了。网上下载的工具不付费有水印,就只能自己撸代码干了。

 

添加jar包,写个简单工具类

package org.example;

import com.luciad.imageio.webp.WebPReadParam;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.FileImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class WebpUtils {

    public static void main(String[] args) throws IOException {
        String basePath = "C:\\Users\\22952\\Pictures\\Saved Pictures\\";
        String webpPath=basePath+"goat.webp";
        String pngPath=basePath+"goat.png";
        //webpToJpg(webpPath,pngPath);
        System.out.println("done");

        String webpPath1=basePath+"goat2.webp";
        jpgToWebp(pngPath,webpPath1);
    }

    public static void webpToJpg(String webpPath,String pngPath) throws IOException {

        ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();

        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);

        reader.setInput(new FileImageInputStream(new File(webpPath)));
        BufferedImage image = reader.read(0,readParam);

        // png can be replaced by jpg
        ImageIO.write(image,"png",new File(pngPath));


    }

    public static void jpgToWebp(String pngPath,String webpPath) throws IOException {

        BufferedImage image = ImageIO.read(new File(pngPath));

        ImageIO.write(image,"webp",new File(webpPath));


    }
}

  

<!--webp文件转换依赖-->
        <!-- https://mvnrepository.com/artifact/org.sejda.imageio/webp-imageio -->
        <dependency>
            <groupId>org.sejda.imageio</groupId>
            <artifactId>webp-imageio</artifactId>
            <version>0.1.6</version>
        </dependency>

标签:转换,String,图片格式,webp,webpPath,pngPath,import,imageio
From: https://www.cnblogs.com/restar/p/16991653.html

相关文章