Java 获得图片的 高度 宽度

File imgFile = new File(GrabImage.ROOTPATH + localPhotoUrl);
if(imgFile.exists()){
 photoWidth = GrabImage.getImgFileWidth(imgFile);
 photoHeight = GrabImage.getImgFileHeight(imgFile);    
}else{
 photoWidth = 0;
 photoHeight = 0;    
}

//得到图片的宽度
public static int getImgFileWidth(File file)
{
  Image img = null;
  try
  {
  img = ImageIO.read(file);
  return img.getWidth(null);
  }
  catch (IOException e)
  {
 e.printStackTrace();
  }
  return 0;
}

//得到图片的高度
public static int getImgFileHeight(File file)
{
  Image img = null;
  try
  {
  img = ImageIO.read(file);
  return img.getHeight(null);
  }
  catch (IOException e)
  {
 e.printStackTrace();
  }
  return 0;
}

    //图片的高度和宽度
    public static int getImageFileHeightWidth(File file, String type){
        try{
            Image img = null;
            img = ImageIO.read(file);
            if("height".equals(type)){
                return img.getHeight(null);
            }
            if("width".equals(type)){
                return img.getWidth(null);
            }           
        }catch(Exception ex){
           
        }
        return 0;
    }

你可能感兴趣的:(java,图片高度,图片宽度)