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;
}
}