java获取图片的宽度和高度

package com.suobei.test;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 获取图片的宽度和高度
 * Created by wangmin on 2018/01/28.
 */
public class ImageSize {

    public static void main(String[] args) {
        //方法一
        ImageIcon imageIcon = new ImageIcon("E:/1.jpg");
        int iconWidth = imageIcon.getIconWidth();
        int iconHeight = imageIcon.getIconHeight();
        System.out.println(iconWidth + "," + iconHeight);

        //方法二
        Image image = imageIcon.getImage();
        int imageWidth = image.getWidth(imageIcon.getImageObserver());
        int imageHeight = image.getHeight(imageIcon.getImageObserver());
        System.out.println(imageWidth + "," + imageHeight);

        //方法三
        try {
            BufferedImage bufferedImage = ImageIO.read(new File("E:/1.jpg"));
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            System.out.println(width + "," + height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(文件操作)