设为首页 加入收藏

TOP

Java里判断Image文件信息格式
2014-11-24 13:12:23 来源: 作者: 【 】 浏览:0
Tags:Java 判断 Image 文件 信息 格式

1,判断Image格式
用UE打开GIF/PNG/JPG格式的图片文件
我们会发现在文件头部某几个位置的字节的值连起来是'GIF'/'PNG'/'JFIF'
它们的位置分别如下:
GIF: 012
JFIF(JPG): 6789
PNG: 123


这样我们可以通过判断这几个字节值来得到Image文件格式:


String type = "";
byte b0 = image.getFileData()[0];
byte b1 = image.getFileData()[1];
byte b2 = image.getFileData()[2];
byte b3 = image.getFileData()[3];
byte b6 = image.getFileData()[6];
byte b7 = image.getFileData()[7];
byte b8 = image.getFileData()[8];
byte b9 = image.getFileData()[9];
// GIF
if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F')
type = "GIF";
// PNG
else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G')
type = "PNG";
// JPG
else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I' && b9 == (byte) 'F')
type = "JPG";
else
type = "Unknown";
image.setType(type);


2,判断Image大小



FileImageInputStream fiis = new FileImageInputStream(new File(image.getPath()));



image.setSize((float) fii.length() / 1000 + "KB");


3,判断Image宽度和高度



ImageIcon ii = new ImageIcon(image.getPath());



image.setHeight(String.valueOf(ii.getIconHeight()));



image.setWidth(String.valueOf(ii.getIconWidth()));


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python修饰器 下一篇Android界面设计之:使用水平视图..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: