返回顶部

收藏

java使用ImageReader判断图片的类型

更多
package com.javacodegeeks.snippets.desktop;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

public class DetermineFormatOfAnImage {

    public static void main(String[] args) throws IOException {

        // get image format in a file
        File file = new File("newimage.jpg");

        // create an image input stream from the specified file
        ImageInputStream iis = ImageIO.createImageInputStream(file);

        // get all currently registered readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

        if (!iter.hasNext()) {
            throw new RuntimeException("No readers found!");
        }

        // get the first reader
        ImageReader reader = iter.next();

        System.out.println("Format: " + reader.getFormatName());

        // close stream
        iis.close();

    }

}

输出:

Format: JPEG

标签:java,图片

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2012-11-05 21:59:42java获得随机数代码 by 怪兽狂殴奥特曼
  2. 2012-11-29 13:19:40java将svg图片转换为png格式图片 by 甄码农
  3. 2013-04-13 13:20:25java设置图片像素 by init0
  4. 2014-06-05 13:45:39Java图像旋转代码 by 虚空之眼
  5. 2014-12-16 09:22:19java获得真实IP代码 by 金背二郎
  6. 2013-06-11 11:14:40java 图片裁剪原理 by zoufei
  7. 2012-11-13 13:55:56java程序中显示gif图片 by demon
  8. 2015-02-04 21:20:02Java图片转换为base64格式 by 甄码农
  9. 2012-11-06 22:22:13Android 获得图片代码 by 文丐
  10. 2013-04-21 11:09:01java图片反色 by Ev4n
  11. 2018-03-30 21:56:50java自动识别用户上传的文本文件编码 by Hugh

发表评论