java 图片获取宽高

package util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

public class  ImagesUtil {
	static String  imagesformat[]  ={"jpg","png"};
	/**
	 * 获取图片高
	 * @param imagePath
	 * @return
	 */
	public static Integer  GetPhotoHeight(String imagePath){
		int index=imagePath.lastIndexOf(".");
		BufferedImage bufferedImage=null;;
		try {
			bufferedImage= ImageIO.read(new File(imagePath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		
		String format=imagePath.substring(index+1, imagePath.length());
		for(int i=0;i<imagesformat.length;i++){
			if(imagesformat[i].equals(format)){
				return   bufferedImage.getWidth();  
			}
		}
		 return null;
	}
	/**
	 * 获取图片宽度
	 * @param imagePath
	 * @return
	 */
  public static Integer GetPhotoWidth(String imagePath){
		int index=imagePath.lastIndexOf(".");
		BufferedImage bufferedImage=null;;
		try {
			bufferedImage= ImageIO.read(new File(imagePath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
		
		String format=imagePath.substring(index+1, imagePath.length());
		for(int i=0;i<imagesformat.length;i++){
			if(imagesformat[i].equals(format)){
				return   bufferedImage.getHeight();  
			}
		}
		 return null;
	}
	
	/**
	 * 获取图片的分辨率
	 * @param url
	 * @return height :高
	 *         width 宽
	 */
public static Map<String,Integer> GetImageResolution(String imageUrl){
	  Map<String,Integer> map=null;
	  int index=imageUrl.lastIndexOf(".");
	  String format=imageUrl.substring(index+1, imageUrl.length());
	  for(int i=0;i<imagesformat.length;i++){
			if(imagesformat[i].equals(format)){
				 BufferedImage image=getBufferedImage(imageUrl);  
			      if (image!=null)  
			      {   map=new HashMap<String, Integer>();
			    	  map.put("height", image.getHeight());
			    	  map.put("width", image.getWidth()); 
			      }  
			      else  
			      {  //图片地址不正确
			         return null;
			      }   
			}
		}
	  return map;
  }
  
  /** 
   *  
   * @param imgUrl 图片地址 
   * @return  
   */  
  public static BufferedImage getBufferedImage(String imgUrl) {  
      URL url = null;  
      InputStream is = null;  
      BufferedImage img = null;  
      try {  
          url = new URL(imgUrl);  
          is = url.openStream();  
          img = ImageIO.read(is);  
      } catch (MalformedURLException e) {  
          e.printStackTrace();  
      } catch (IOException e) {  
          e.printStackTrace();  
      } finally {  
            
          try {  
              is.close();  
          } catch (IOException e) {  
              e.printStackTrace();  
          }  
      }  
      return img;  
  }  


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值