Java中对图片文件的类型的获取
2008-01-09 17:53
555 查看
public static void main(String[] args) {
File f = new File("c://test.jpg");
if (f.exists()) {
System.out.println(getFormatInFile(f));
}
}
// Returns the format of the image in the file 'f'.
// Returns null if the format is not known.
public static String getFormatInFile(File f) {
return getFormatName(f);
}
// Returns the format name of the image in the object 'o'.
// Returns null if the format is not known.
private static String getFormatName(Object o) {
try {
// Create an image input stream on the image
ImageInputStream iis = ImageIO.createImageInputStream(o);
// Find all image readers that recognize the image format
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}
// Use the first reader
ImageReader reader = iter.next();
// Close stream
iis.close();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
//
}
// The image could not be read
return null;
}
File f = new File("c://test.jpg");
if (f.exists()) {
System.out.println(getFormatInFile(f));
}
}
// Returns the format of the image in the file 'f'.
// Returns null if the format is not known.
public static String getFormatInFile(File f) {
return getFormatName(f);
}
// Returns the format name of the image in the object 'o'.
// Returns null if the format is not known.
private static String getFormatName(Object o) {
try {
// Create an image input stream on the image
ImageInputStream iis = ImageIO.createImageInputStream(o);
// Find all image readers that recognize the image format
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
// No readers found
return null;
}
// Use the first reader
ImageReader reader = iter.next();
// Close stream
iis.close();
// Return the format name
return reader.getFormatName();
} catch (IOException e) {
//
}
// The image could not be read
return null;
}
相关文章推荐
- Java 文件 获取图片文件的类型
- Java中对图片文件的类型的获取
- Java中对图片文件的类型的获取
- Java中对图片文件的类型的获取
- Java中对图片文件的类型的获取
- Java中对图片文件的类型的获取
- Java中对图片文件的类型的获取
- Java获取文件类型Mime Type的各种方法
- Java GUI中根据文件类型获取文件的图标
- java获得U盘根目录、读取图片视频头文件信息、获取视频时长
- Java获取文件类型Mime Type的各种方法
- java获取文件的类型MagicMatch.getMimeType()报错 java.lang.NoClassDefFoundError: org/apache/oro/text/perl/Perl5
- 使用Java获取文件类型
- 判断文件类型是否为图片的方法——Java实现
- JS 客户端验证文件类型,获取图片信息
- java获取文件MIME类型
- java 实现BufferedImage和ImageReader两种方式获取图片宽高、判断图片类型、获取图片大小工具类代码以及测试响应结果
- Java获取文件类型Mime Type的各种方法
- Java获取文件的类型
- JAVA获取真实的文件类型