天天看點

Java操作Google Webp圖檔格式轉換實踐

目錄

webp壓縮的好處

 Java使用webp代碼編譯

Webp開源項目

nintha/webp-imageio-core

qwong/j-webp

可用的webp依賴和包

Webp 編解碼

編碼

解碼

Webp遇到的坑

檔案釋放

阿裡雲Linux核心版本差異

webp壓縮的好處

可以縮小網絡傳輸和磁盤占用空間。

Java操作Google Webp圖檔格式轉換實踐

 Java使用webp代碼編譯

  1. webp-imageio

    開源庫 下載下傳壓縮包到本地進行編譯jar,工程用gradle來維護。

  2. libwebp

    google-developer下載下傳壓縮包到本地編譯動态連結庫-不同平台的動态連結庫編譯出來是不同的。mac下是.dylib, windowns下是.dll, unix/linux下是.so. 我在mac和linux下編譯過,記錄下linux下編譯過程。

  3. compiling
libwebp-0.6.1.tar.gz luciad-webp-imageio-8f9b44b41902.zip

tar -xzvf libwebp-0.6.1.tar.gz
unzip luciad-webp-imageio-8f9b44b41902.zip
mv libwebp-0.6.1 ./luciad-webp-imageio-8f9b44b41902/libwebp

           
cd luciad-webp-imageio-8f9b44b41902
cmake .
cmake --build .
           

cmake如果裝的話,請自行下載下傳安裝官網 安裝用法

./gradlew build -x test
           
  1. java project

    将libwebp-imageio.so放入工程的目前目錄,因為預設的java.library.path含工程目前路徑。同時引入打好的luciad-webp-imageio-8f9b44b41902-0.6.0-SNAPSHOT.jar引入工程中。如果是maven工程的話,可以把jar先推到遠端的倉庫裡。

  • java transfer
image = ImageIO.read(new URL(imageUrl));
// Encode it as webp using default settings
boolean result = ImageIO.write(image, "webp", bos);
           
  • gradle push to repositry

    update build.gradle

apply plugin: "maven-publish" //插件required
group = "com.luciad.imageio.webp"

publishing {
    publications {
        mavenJava(MavenPublication) {
           from components.java
        }
    }
    repositories {
        maven {
             url "http://***"
             credentials {
                      username = ****
                      password = ****
            }
      }
   }
}
           

publish

gradle publishMavenPublicationToMavenRepository
           

有時間的話可以自己編譯,此處來自:https://www.jianshu.com/p/5827f9063fe9

Webp開源項目

nintha/webp-imageio-core

qwong/j-webp

可用的webp依賴和包

下載下傳此開源項目沒有跑起來,dll包有問題。直接使用0.4.2可以正常使用,下載下傳位址如下:

  • http://download.csdn.net/download/geeklei/8163329
  • https://download.csdn.net/download/geeklei/8161731
  • 連結:https://pan.baidu.com/s/1JZB_cNNr3Pf8sv2hdvu_-A

    提取碼:zvql

    複制這段内容後打開百度網盤手機App,操作更友善哦

Webp 編解碼

編碼

package example;

import com.luciad.imageio.webp.WebPWriteParam;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.FileImageOutputStream;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class EncodeTest {
    public static void main(String args[]) throws IOException {
        String inputPngPath = "test_pic/test.png";
        String inputJpgPath = "test_pic/test.jpg";
        String outputWebpPath = "test_pic/test_.webp";

        // Obtain an image to encode from somewhere
        BufferedImage image = ImageIO.read(new File(inputJpgPath));

        // Obtain a WebP ImageWriter instance
        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/webp").next();

        // Configure encoding parameters
        WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
        writeParam.setCompressionMode(WebPWriteParam.MODE_DEFAULT);

        // Configure the output on the ImageWriter
        writer.setOutput(new FileImageOutputStream(new File(outputWebpPath)));

        // Encode
        long st = System.currentTimeMillis();
        writer.write(null, new IIOImage(image, null, null), writeParam);
        System.out.println("cost: " + (System.currentTimeMillis() - st));
    }
}
           

解碼

package 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 DecodeTest {
    public static void main(String args[]) throws IOException {
    	System.load("C:\\Users\\admin\\Desktop\\webp\\webp-code\\webp-imageio-core\\src\\main\\resources\\META-INF\\lib\\windows_64\\webp-imageio.dll");
        String inputWebpPath = "test_pic/test.webp";
        String outputJpgPath = "test_pic/test_.jpg";
        String outputJpegPath = "test_pic/test_.jpeg";
        String outputPngPath = "test_pic/test_.png";

        // Obtain a WebP ImageReader instance
        ImageReader reader = ImageIO.getImageReadersByMIMEType("image/webp").next();

        // Configure decoding parameters
        WebPReadParam readParam = new WebPReadParam();
        readParam.setBypassFiltering(true);

        // Configure the input on the ImageReader
        reader.setInput(new FileImageInputStream(new File(inputWebpPath)));

        // Decode the image
        BufferedImage image = reader.read(0, readParam);

        ImageIO.write(image, "png", new File(outputPngPath));
        ImageIO.write(image, "jpg", new File(outputJpgPath));
        ImageIO.write(image, "jpeg", new File(outputJpegPath));

    }
}
           

Webp遇到的坑

檔案釋放

需要注意在用完檔案流之後記得關閉,否則删除檔案會失敗

Java操作Google Webp圖檔格式轉換實踐

阿裡雲Linux核心版本差異

在三台Centos的阿裡雲 伺服器上三個核心版本,其中有台伺服器webp不報錯但就是程式阻塞到webp調用然後傳回http404:

正常核心版本:

Java操作Google Webp圖檔格式轉換實踐
Java操作Google Webp圖檔格式轉換實踐

異常核心版本:

Java操作Google Webp圖檔格式轉換實踐

更多閱讀:

https://segmentfault.com/a/1190000016324137

https://blog.csdn.net/xu_san_duo/article/details/79085718

webp-imageio 如何編譯及使用