public static Integer[] computeWHWithoutLocPic(String picUrl){
if(StringUtils.isBlank(picUrl)){
return null;
}
Integer[] picWHPair = new Integer[2];
BufferedImage bufferedImage = getBufferedImage(picUrl);
if(bufferedImage == null){
return null;
}
picWHPair[0] = bufferedImage.getWidth();
picWHPair[1] = bufferedImage.getHeight();
System.out.println("w="+bufferedImage.getWidth()+",h="+bufferedImage.getHeight());
return picWHPair;
}
/**
*
* @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) {
System.out.println("get pic width and height error! MalformedURLException imgUrl={}"+imgUrl);
} catch (IOException e) {
System.out.println("get pic width and height error! IOException imgUrl={}"+imgUrl);
} finally {
try {
is.close();
} catch (IOException e) {
System.out.println("get pic width and height error! is.close() IOException imgUrl={}"+imgUrl);
}
}
return img;
}
获取网络图片宽高
最新推荐文章于 2022-08-18 10:19:58 发布