|
@@ -5,7 +5,11 @@ import cn.hutool.core.io.FileUtil;
|
|
|
import com.gis.common.base.exception.BaseRuntimeException;
|
|
|
import com.gis.common.constant.CmdConstant;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.imaging.ImageInfo;
|
|
|
+import org.apache.commons.imaging.ImageReadException;
|
|
|
+import org.apache.commons.imaging.Imaging;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.junit.Test;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
@@ -97,19 +101,37 @@ public class ImgUtils {
|
|
|
// }
|
|
|
|
|
|
|
|
|
+// public static void checkImgRatio(MultipartFile file){
|
|
|
+// try {
|
|
|
+// Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
|
|
|
+// ImageReader reader = (ImageReader) readers.next();
|
|
|
+// ImageInputStream iis = ImageIO.createImageInputStream(file);
|
|
|
+// reader.setInput(iis, true);
|
|
|
+// int width = reader.getWidth(0);
|
|
|
+// int height = reader.getHeight(0);
|
|
|
+//
|
|
|
+// BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
|
|
|
+//
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
public static void checkImgRatio(MultipartFile file){
|
|
|
try {
|
|
|
- Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
|
|
|
- ImageReader reader = (ImageReader) readers.next();
|
|
|
- ImageInputStream iis = ImageIO.createImageInputStream(file);
|
|
|
- reader.setInput(iis, true);
|
|
|
- int width = reader.getWidth(0);
|
|
|
- int height = reader.getHeight(0);
|
|
|
+ ImageInfo imageInfo = Imaging.getImageInfo(file.getInputStream(), null);
|
|
|
+ log.info("正常读取=============================");
|
|
|
+ int width = imageInfo.getWidth();
|
|
|
+ int height = imageInfo.getHeight();
|
|
|
|
|
|
BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } catch (ImageReadException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -129,4 +151,20 @@ public class ImgUtils {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test1(){
|
|
|
+ String srcPath = "E:\\桌面\\pano_1x1_素材\\1.jpg";
|
|
|
+ File file = new File(srcPath);
|
|
|
+ try {
|
|
|
+ ImageInfo info = Imaging.getImageInfo(file);
|
|
|
+ System.out.println("width: " + info.getWidth());
|
|
|
+ System.out.println("height: " + info.getHeight());
|
|
|
+ } catch (ImageReadException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|