package util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
/**
* 通过java获取图片的宽和高
* @author sunlightcs
* 2011-6-1
* http://hi.juziku.com/sunlightcs/
*/
public class ImageTools {
/**
* 获取图片宽度
* @param file 图片文件
* @return 宽度
*/
public static int getImgWidth(File file) {
InputStream is = null;
BufferedImage src = null;
int ret = -1;
try {
is = new FileInputStream(file);
src = javax.imageio.ImageIO.read(is);
ret = src.getWidth(null); // 得到源图宽
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* 获取图片高度
* @param file 图片文件
* @return 高度
*/
public static int getImgHeight(File file) {
InputStream is = null;
BufferedImage src = null;
int ret = -1;
try {
is = new FileInputStream(file);
src = javax.imageio.ImageIO.read(is);
ret = src.getHeight(null); // 得到源图高
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
package util;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import javax.imageio.ImageIO;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
String imagePath="F:\\images\\1.jpg";
//System.out.println("高"+ImagesUtil.GetPhotoHeight(imagePath));
//System.out.println("宽"+ImagesUtil.GetPhotoWidth(imagePath));
String imageUrl="http://avatar.csdn.net/9/F/2/1_5iasp.jpg";
String url="F:/images/1.jpg";
/* Map<String,Integer> map=ImagesUtil.GetImageResolution(imageUrl);
if(map!=null){
System.out.println(map.get("width"));
System.out.println(map.get("width"));
}else{
System.out.println("图片路径或者格式不正确");
} */
File file=new File(url);
Integer i= ImageTools.getImgHeight(file);
System.out.println(i);
}
}