用java将jpg或者Png的图片转换为WEBP格式,以及WEBP转换成JPG格式

第一步导入WEBP的jar包

webp-imageio-0.4.2

第二步导入WEBP的工具类

public class ImageConverterWebp {

    public static final String WEBP = "webp";
    public static final String WEBP_SUFFIX = ".webp";

    private ImageConverterWebp() {
    }

    /**
     * 1. 传入图片文件路径,返回file对象
     * @param imgFilePath 图片文件路径(比如转换图片为F:/1.png 那么转换后的webp文件为:F:/1.png.webp)
     * @return
     */
    public static File toWebpFile(String imgFilePath) {
        File imgFile = new File(imgFilePath);
        File webpFile = new File(imgFilePath + WEBP_SUFFIX);
        try {
            BufferedImage bufferedImage = ImageIO.read(imgFile);
            ImageIO.write(bufferedImage, WEBP, webpFile);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return webpFile;
    }

    /**
     * 2. 传入图片url,返回file对象
     * @param imgUrlPath 图片路径url
     * @param webpFilePath 生成的webp文件路径
     * @return
     */
    public static File toWebpFile(String imgUrlPath, String webpFilePath) {
        File webpFile = new File(webpFilePath);
        try {
            BufferedImage bufferedImage = ImageIO.read(new URL(imgUrlPath));
            ImageIO.write(bufferedImage, WEBP, webpFile);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return webpFile;
    }

    /**
     * 3. 传入图片文件路径,返回InputStream
     * @param imgFilePath 图片文件路径(比如转换图片为F:/1.png 那么转换后的webp文件为:F:/1.png.webp)
     * @return
     */
    public static InputStream toWebpStream(String imgFilePath) {
        File imgFile = new File(imgFilePath);
        File webpFile = new File(imgFilePath + WEBP_SUFFIX);
        FileInputStream fis = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imgFile);
            ImageIO.write(bufferedImage, WEBP, webpFile);
            fis = new FileInputStream(webpFile);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if(fis != null){
                try {
                    fis.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fis;
    }

    /**
     * 4. 传入图片url,返回InputStream
     * @param imgUrlPath 图片路径url
     * @param webpFilePath 生成的webp文件路径
     * @return
     */
    public static InputStream toWebpStream(String imgUrlPath, String webpFilePath) {
        File webpFile = new File(webpFilePath);
        FileInputStream fis = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(new URL(imgUrlPath));
            ImageIO.write(bufferedImage, WEBP, webpFile);
            fis = new FileInputStream(webpFile);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            if(fis != null){
                try {
                    fis.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fis;
    }

    public static void main(String[] args) {
        String imgUrlPath = "D:\\huangqiming\\jpg\\111.jpg";
        File result = toWebpFile(imgUrlPath);
    }
}

第三步,引入dll的配置文件到JDK上:


最后一步测试:


测试成功

================================================================

WEBP转换一下JPG格式

   File file1= new File("/home/geeklei/Desktop/640_tp.webp");
    File file2= new File("/home/geeklei/Desktop/640a.png");  
          
System.out.println(System.getProperty("java.library.path"));  
          
try {

        BufferedImage im = ImageIO.read(file1);
        ImageIO.write(im, "png", file2);


    } catch (IOException e) {
        e.printStackTrace();
    }
转换完成~~~

猜你喜欢

转载自blog.csdn.net/weixin_41904221/article/details/80845428