判断图片是横版还是竖版图片,就要获得图片的宽和高,有两种获得方式
1、图片在同一个机器(本地)可以预览
/**
* @function:根据图片完整路径,判断是横版还是竖版图片
* @param imagePath
* @return true是竖版,false是横版
*/
private static boolean getVerticalImage(String imagePath) {
boolean is_vertical = false;
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new File(imagePath));
int height = bufferedImage.getHeight();
int width = bufferedImage.getWidth();
if (height > width) {
// 竖版
is_vertical = true;
} else {
// 横版
is_vertical = false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is_vertical;
}
2、图片从不同服务器(网络)上获得
import com.itextpdf.text.Image;
// 图片属于网络上的地址
try {
String imagePath = "http://g.hiphotos.baidu.com/baike/c0%3Dbaike180%2C5%2C5%2C180%2C60/sign=db5a49b475f08202399f996d2a929088/8ad4b31c8701a18bf1c7bfd8982f07082838fe7d.jpg";
Image img = Image.getInstance(new URL(imagePath));
System.out.println("width=" + img.getWidth() + "-----------height="
+ img.getHeight());
} catch (Exception e) {
e.printStackTrace();
}
结果是:
width=1000.0-----------height=1486.0