webp學習以及在java中的使用

webp(發音:weppy

谷歌於2010年推出的新一代圖片格式,在壓縮方面比當前JPEG格式更優越。是一種同時提供了有損壓縮無損壓縮(可逆壓縮)的圖片文件格式,派生自影像編碼格式VP8,被認爲是WebM多媒體格式的姊妹項目,是由Google在購買On2 Technologies後發展出來,以BSD授權條款發佈。

JPEG相同,WebP是一種有損壓縮。但谷歌表示,這種格式的主要優勢在於高效率。在質量相同的情況下,WebP格式圖像的體積要比JPEG格式圖像小40%。美中不足的是,WebP格式圖像的編碼時間“比JPEG格式圖像長8倍”。

不支持webp的瀏覽器

目前筆者測試的有:ie,Microsoft Edge,macOS safari

用法

1.Luciad/webp-imageio 使用Java Image I/O 對 libwebp 進行的封裝

1.先將項目源碼下載到本地,下載地址https://bitbucket.org/luciad/webp-imageio/get/873c5677244b.zip

。。。

比較麻煩,筆者沒有耐心,跳過看第二種。

2. webp-imageio-core 下載地址:Download jar

在第一種方法中,需要將本地lib例如.so/.dll/.dylib添加到java.library.path中,這樣會增加開發步驟的繁瑣。爲了更便於使用,qwong/j-webp 在 webp project of Luciad 0.4.2的基礎上引入了 native-lib-loader,使得項目可以從resource文件下加載本地lib文件,從而代替了將lib添加到java庫中。

然而,開發者更傾向於使用jar包來代替java源碼,所以nintha/webp-imageio-coreqwong/j-webp項目進行再次修改,使之成爲一個可以直接使用的jar包,接下來我們來學習一下怎麼使用開源包webp-imageio-core來操作webp。

1.使用本地jar包

如果你使用的是gradle,可以直接將jar包添加到src/main/resource/libs下,並且編輯配置文件build.gradle來添加本地依賴。

dependencies {
    compile fileTree(dir:'src/main/resources/libs',include:['*.jar'])
}

如果你使用的是maven,可以把包放在本地路徑${project.basedir}/libs下,然後編輯pom.xml來加載本地依賴。

<dependency>  
    <groupId>com.github.nintha</groupId>  
    <artifactId>webp-imageio-core</artifactId>  
    <version>{versoin}</version>  
    <scope>system</scope>  
    <systemPath>${project.basedir}/libs/webp-imageio-core-{version}.jar</systemPath>  
</dependency>

2.maven私服。因爲webp-imageio-core-{version}.jar包並不在maven庫中,所以需要手動將包添加到私服中。  

mvn deploy:deploy-file -Dmaven.test.skip=true -Dfile=E:/webp-imageio-core-0.1.3.jar -DgroupId=com.github.nintha -DartifactId=webp-imageio-core -Dversion=0.1.3 -Durl=http://IP:port/repository/nexus-releases -DrepositoryId=serverId

pom文件配置:

        <dependency>
            <groupId>com.github.nintha</groupId>
            <artifactId>webp-imageio-core</artifactId>
            <version>0.1.3</version>
        </dependency>

3.使用:

3.1將其他文件編碼爲webp格式。

 public static void main(String args[]){
        String srcFile = "D:\\file\\imgs\\order/b121502f8ed9346f9ee1236e5a1f7ab4.png";
        String webpFile = "D:\\file\\imgs\\order/b121502f8ed9346f9ee1236e5a1f7ab4.png.webp";
        encodingToWebp(srcFile, webpFile);
    }
    
    public static void encodingToWebp(String srcFile, String webpFile) {
        encodingToWebp(new File(srcFile),new File(webpFile) );
    }

    /**
     * @param: srcFile
     * @param: webpFile
     * @description: 將文件編碼爲WEBP格式
     * @author: tangjianghua
     */
    public static void encodingToWebp(File srcFile, File webpFile) {

        try {

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

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

            // Configure encoding parameters
            WebPWriteParam writeParam = new WebPWriteParam(writer.getLocale());
            writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            writeParam.setCompressionType(writeParam.getCompressionTypes()[WebPWriteParam.LOSSLESS_COMPRESSION]);

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

            // Encode
            writer.write(null, new IIOImage(image, null, null), writeParam);


            //釋放reader
            writer.dispose();

            //關閉文件流
            fileImageOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.2 將webp文件解碼爲其他格式

public static void main(String args[]) {
        String srcFile = "D:\\file\\imgs\\order/b121502f8ed9346f9ee1236e5a1f7ab4.png";
        String webpFile = "D:\\file\\imgs\\order/b121502f8ed9346f9ee1236e5a1f7ab4.png.webp";
        String toFile1 = "D:\\file\\imgs\\order/abc.png";
        String toFile2 = "D:\\file\\imgs\\order/adbc.jpg";
        String toFile3 = "D:\\file\\imgs\\order/asd.jpeg";
//        encodingToWebp(srcFile, webpFile);
        decodingFromWebp(webpFile, toFile1);
        decodingFromWebp(webpFile, toFile2);
        decodingFromWebp(webpFile, toFile3);
    }

    /**
     * @param: webpFile
     * @param: toFile
     * @param: fileType 文件格式 png,jpg,jpeg
     * @description: 將WEBP格式解碼爲其他文件
     * @author: tangjianghua
     */
    public static void decodingFromWebp(String webpFile, String toFile) {
        decodingFromWebp(new File(webpFile), new File(toFile), toFile.substring(toFile.lastIndexOf('.') + 1, toFile.length()));
    }

    /**
     * @param: webpFile
     * @param: toFile
     * @param: fileType 文件格式 png,jpg,jpeg
     * @description: 將WEBP格式解碼爲其他文件
     * @author: tangjianghua
     */
    public static void decodingFromWebp(File webpFile, File toFile, String fileType) {

        try {
            // 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
            FileImageInputStream fileImageInputStream = new FileImageInputStream(webpFile);
            reader.setInput(fileImageInputStream);

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

            ImageIO.write(image, fileType, toFile);

            //釋放reader
            reader.dispose();

            //關閉文件流
            fileImageInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

附上git地址:https://github.com/tang-jianghua/webp

發佈了130 篇原創文章 · 獲贊 37 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章