|
@@ -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();
|
|
|
}
|
|
|
}
|