Java中对图片文件的类型的获取
2017-02-12 21:29
363 查看
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中对图片文件的类型的获取
- node.js获取图片文件的真实类型
- JS 客户端验证文件类型,获取图片信息
- Java根据文件头获取文件类型
- JAVA将任意图片文件压缩成想要的图片类型与大小
- Android中Java根据文件头获取文件类型
- Java获取文件类型Mime Type的各种方法
- Java获取URL链接的文件类型
- Android中Java根据文件头获取文件类型
- java 自动识别图片文件类型 图片后缀 图片类型
- java获取文件exif信息-添加图片文字水印
- JS 客户端验证文件类型,获取图片信息
- JS 客户端验证文件类型,获取图片信息
- java 图片缩放,获取图片类型,字节数据和流的转换