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.先将项目源码下载到本地,下载地址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-core将qwong/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 |