一个 boolean 的方法,超过规定像素大小返回false。 可用于接口上传图片,像素大小的判断。
public boolean judgeImgPixel(String path) throws Exception{
File file = new File( path);//读取文件路径
BufferedImage bi = null;
try{
bi = ImageIO.read(file);
}catch (IOException e){
logger.error("获取图片像素异常!");
}
int width = bi.getWidth();
int height = bi.getHeight();
if (width < 150 || height < 150 || width > 1500 || height > 1500) {
return false;
}
return true;
}
使用的话 参考如下
if (!uploadService.judgeImgPixel(registerImgPath)) {
throw new ServiceException(EnumClass.ResultCodeType.IMG_ERROR.getKey(), "注册照图片像素不达标!");
}