浏览代码

解决:BufferedImage 内存占用

wuweihao 3 年之前
父节点
当前提交
35aa88361f

+ 6 - 85
720yun_local_manage/gis_common/src/main/java/com/gis/common/util/ImgUtils.java

@@ -1,6 +1,5 @@
 package com.gis.common.util;
 
-import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import com.gis.common.base.exception.BaseRuntimeException;
 import com.gis.common.constant.CmdConstant;
@@ -12,14 +11,8 @@ import org.apache.commons.lang3.StringUtils;
 import org.junit.Test;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.imageio.ImageIO;
-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;
 
 /**
  * Created by owen on 2022/3/23 0023 17:48
@@ -61,96 +54,26 @@ public class ImgUtils {
     }
 
 
-//    public static void checkImgRatio(MultipartFile file){
-//
-//        try {
-//            BufferedImage read = ImgUtil.read(file.getInputStream());
-//            int width = read.getWidth();
-//            int height = read.getHeight();
-//            BaseRuntimeException.isHas(!(width/height == 2), null, "非2:1图片");
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
-//
-//    }
-
     /**
+     * 2022-6-2 by owen
      * 检查2:1图片
      * @param file
+     * 使用 commons.imaging 包
+     * BufferedImage 会导致内存占用过大, 不释放内存问题
+     *
      */
-//    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){
-//        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 {
             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) {
+        } catch (IOException | ImageReadException e) {
             e.printStackTrace();
         }
 
     }
 
-    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();
-        }
-
-    }
 
 
     @Test
@@ -161,9 +84,7 @@ public class ImgUtils {
             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) {
+        } catch (ImageReadException | IOException e) {
             e.printStackTrace();
         }
     }

+ 1 - 5
720yun_local_manage/pom.xml

@@ -203,13 +203,9 @@
         <version>${lang3.version}</version>
       </dependency>
 
-      <!--<dependency>-->
-        <!--<groupId>commons-io</groupId>-->
-        <!--<artifactId>commons-io</artifactId>-->
-        <!--<version>${commons-io.version}</version>-->
-      <!--</dependency>-->
 
 
+      <!-- 解决读取图片BufferedImage 内存不是否问题 -->
       <!--
           mvn install:install-file -Dfile=commons-imaging-1.0-alpha3.jar -DgroupId=org.apache.commons -DartifactId=commons-imaging -Dversion=1.0-alpha3 -Dpackaging=jar
       -->