wuweihao 3 år sedan
förälder
incheckning
b77f5b7741

+ 45 - 15
720yun_local_manage/gis_common/src/main/java/com/gis/common/util/ImgUtils.java

@@ -13,6 +13,7 @@ import javax.imageio.ImageReadParam;
 import javax.imageio.ImageReader;
 import javax.imageio.stream.ImageInputStream;
 import java.awt.image.BufferedImage;
+import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
 
@@ -73,29 +74,58 @@ public class ImgUtils {
      * 检查2:1图片
      * @param file
      */
+//    public static void checkImgRatio(MultipartFile file){
+//        BufferedImage read = null;
+//        try {
+//            ImageIO.setUseCache(false);
+//            read = ImageIO.read(file.getInputStream());
+//            int width = read.getWidth();
+//            int height = read.getHeight();
+//
+//            BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
+//
+//        } catch (IOException e) {
+//            e.printStackTrace();
+//        } finally {
+//            if (read != null){
+//                // 释放内存
+//                read.getGraphics().dispose();
+//                log.info("释放内存");
+//            }
+//        }
+//
+//    }
+
+
     public static void checkImgRatio(MultipartFile file){
-        BufferedImage read = null;
         try {
-            ImageIO.setUseCache(false);
-            read = ImageIO.read(file.getInputStream());
-            int width = read.getWidth();
-            int height = read.getHeight();
+            Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(file.getInputStream());
+
+            ImageReader read = imageReaders.next();
+            int width = read.getWidth(0);
+            int height = read.getHeight(0);
 
             BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
+
         } catch (IOException e) {
             e.printStackTrace();
-        } finally {
-            if (read != null){
-                // 释放内存
-                read.getGraphics().dispose();
-                log.info("释放内存");
-            }
         }
 
     }
 
+    public static void main(String[] args) {
+        String srcPath = "E:\\桌面\\pano_1x1_素材\\1.jpg";
+        File file = new File(srcPath);
+        try {
+            Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
+            ImageReader reader = (ImageReader) readers.next();
+            ImageInputStream iis = ImageIO.createImageInputStream(file);
+            reader.setInput(iis, true);
+            System.out.println("width: " + reader.getWidth(0));
+           System.out.println("height: " + reader.getHeight(0));
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
 
-
-
-
-}
+    }
+    }