通过查看十六进制文件,发现WebP格式图片的长宽信息位于文件头第26、27(高),28、29(宽)这四个字节中。
Java代码:
import java.io.File; import java.io.FileInputStream; /** * @author 42 */ public class WebpSize { public static void main(String[] args) { File 文件=new File("示例.webp"); try { FileInputStream 输入=new FileInputStream(文件); byte[] 字节数组=new byte[30]; 输入.read(字节数组); if("WEBP".equals(new String(字节数组,8,4,"utf-8"))){//先判断图片格式为webp int 宽 = (((int) 字节数组[27] & 0xff) << 8) | (int) 字节数组[26] & 0xff; int 高 = (((int) 字节数组[29] & 0xff) << 8) | (int) 字节数组[28] & 0xff; System.out.print("宽:"+宽+",高:"+高); }else{ System.out.print("文件不是webp格式"); } 输入.close(); } catch(Exception e) { System.out.println(e.getMessage()); } } }[/code]本文更新於 2014/05/19。