Commit 3d9b80fede6a79c7103a2a3841562d13861de874
1 parent
afc19d1b
导出图片压缩
Showing
1 changed file
with
71 additions
and
1 deletions
urbanops-module-garden/src/main/java/com/zteits/urbanops/module/garden/util/ImgDownUtils.java
| @@ -4,6 +4,13 @@ import com.zteits.urbanops.module.infra.framework.file.core.client.FileClient; | @@ -4,6 +4,13 @@ import com.zteits.urbanops.module.infra.framework.file.core.client.FileClient; | ||
| 4 | import lombok.extern.slf4j.Slf4j; | 4 | import lombok.extern.slf4j.Slf4j; |
| 5 | import org.springframework.util.StringUtils; | 5 | import org.springframework.util.StringUtils; |
| 6 | 6 | ||
| 7 | +import javax.imageio.ImageIO; | ||
| 8 | +import java.awt.Graphics2D; | ||
| 9 | +import java.awt.RenderingHints; | ||
| 10 | +import java.awt.image.BufferedImage; | ||
| 11 | +import java.io.ByteArrayInputStream; | ||
| 12 | +import java.io.ByteArrayOutputStream; | ||
| 13 | +import java.io.IOException; | ||
| 7 | import java.util.ArrayList; | 14 | import java.util.ArrayList; |
| 8 | import java.util.List; | 15 | import java.util.List; |
| 9 | 16 | ||
| @@ -65,7 +72,10 @@ public class ImgDownUtils { | @@ -65,7 +72,10 @@ public class ImgDownUtils { | ||
| 65 | try { | 72 | try { |
| 66 | String realPath = ImgDownUtils.getRealPath(url); | 73 | String realPath = ImgDownUtils.getRealPath(url); |
| 67 | byte[] bytes = client.getContent(realPath); | 74 | byte[] bytes = client.getContent(realPath); |
| 68 | - result.add(bytes); | 75 | + //result.add(bytes); |
| 76 | + // 核心:图片压缩,可自行调整参数 | ||
| 77 | + byte[] compressBytes = compressImage(bytes, 1600, 0, 0.65f); | ||
| 78 | + result.add(compressBytes); | ||
| 69 | } catch (Exception e) { | 79 | } catch (Exception e) { |
| 70 | log.error("[图片下载] 异常:{},路径:{}", e.getMessage(), url); | 80 | log.error("[图片下载] 异常:{},路径:{}", e.getMessage(), url); |
| 71 | result.add(null); | 81 | result.add(null); |
| @@ -73,4 +83,64 @@ public class ImgDownUtils { | @@ -73,4 +83,64 @@ public class ImgDownUtils { | ||
| 73 | } | 83 | } |
| 74 | return result; | 84 | return result; |
| 75 | } | 85 | } |
| 86 | + | ||
| 87 | + /** | ||
| 88 | + * 图片压缩方法 | ||
| 89 | + * @param imageBytes 原始图片字节 | ||
| 90 | + * @param maxWidth 最大宽度,0不限制 | ||
| 91 | + * @param maxHeight 最大高度,0不限制 | ||
| 92 | + * @param quality jpg质量 0~1 | ||
| 93 | + * @return 压缩后byte[] | ||
| 94 | + * @throws IOException | ||
| 95 | + */ | ||
| 96 | + public static byte[] compressImage(byte[] imageBytes, int maxWidth, int maxHeight, float quality) throws IOException { | ||
| 97 | + try (ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes); | ||
| 98 | + ByteArrayOutputStream bos = new ByteArrayOutputStream()) { | ||
| 99 | + | ||
| 100 | + BufferedImage srcImg = ImageIO.read(bis); | ||
| 101 | + if (srcImg == null) { | ||
| 102 | + throw new IOException("无法解析图片流"); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + int srcW = srcImg.getWidth(); | ||
| 106 | + int srcH = srcImg.getHeight(); | ||
| 107 | + int targetW = srcW; | ||
| 108 | + int targetH = srcH; | ||
| 109 | + | ||
| 110 | + // 等比例缩放 | ||
| 111 | + if (maxWidth > 0 && srcW > maxWidth) { | ||
| 112 | + targetW = maxWidth; | ||
| 113 | + targetH = (int) (srcH * ((double) targetW / srcW)); | ||
| 114 | + } | ||
| 115 | + if (maxHeight > 0 && targetH > maxHeight) { | ||
| 116 | + targetH = maxHeight; | ||
| 117 | + targetW = (int) (srcW * ((double) targetH / srcH)); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + // 创建缩放画布 | ||
| 121 | + BufferedImage targetImg = new BufferedImage(targetW, targetH, BufferedImage.TYPE_3BYTE_BGR); | ||
| 122 | + Graphics2D g2d = targetImg.createGraphics(); | ||
| 123 | + try { | ||
| 124 | + g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | ||
| 125 | + g2d.drawImage(srcImg, 0, 0, targetW, targetH, null); | ||
| 126 | + } finally { | ||
| 127 | + g2d.dispose(); | ||
| 128 | + } | ||
| 129 | + | ||
| 130 | + // JPG输出并设置质量 | ||
| 131 | + javax.imageio.IIOImage iioImage = new javax.imageio.IIOImage(targetImg, null, null); | ||
| 132 | + javax.imageio.ImageWriteParam writeParam = javax.imageio.ImageIO.getImageWritersByFormatName("jpg").next().getDefaultWriteParam(); | ||
| 133 | + writeParam.setCompressionMode(javax.imageio.ImageWriteParam.MODE_EXPLICIT); | ||
| 134 | + writeParam.setCompressionQuality(quality); | ||
| 135 | + | ||
| 136 | + javax.imageio.ImageWriter writer = javax.imageio.ImageIO.getImageWritersByFormatName("jpg").next(); | ||
| 137 | + writer.setOutput(ImageIO.createImageOutputStream(bos)); | ||
| 138 | + try { | ||
| 139 | + writer.write(null, iioImage, writeParam); | ||
| 140 | + } finally { | ||
| 141 | + writer.dispose(); | ||
| 142 | + } | ||
| 143 | + return bos.toByteArray(); | ||
| 144 | + } | ||
| 145 | + } | ||
| 76 | } | 146 | } |