Java里判断Image文件信息格式
2012-04-10 14:59
585 查看
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()));
用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()));
相关文章推荐
- Java里判断Image文件信息格式(GIF/PNG/JPG)/Size/Height/Width?
- js和java获取上传文件的文件名以及上传文件格式判断
- Java 判断上传文件格式是否是有效的
- java 判断文件编码格式(支持zip)
- SpringBoot文件上传控制及Java 获取和判断文件头信息
- 使用java判断TXT文件的编码格式
- Java判断文件编码格式
- java判断文件编码格式
- java自动判断文件编码格式
- java 判断txt文件的编码格式
- java代码判断图片文件格式, 不是根据文件后缀来判断。
- 判断一个java文件和邮箱格式是否合法
- Java class文件格式之访问标志信息_动力节点Java学院整理
- java判断文件编码格式
- java判断文件编码格式以防止乱码
- Java判断文件编码格式
- JAVA 程序中如何实现对RAR压缩包文件中文件格式的判断?
- java判断文件编码格式
- Java判断文件编码格式
- GetImageSize()取得图片的长宽、文件格式信息