需求:
大於2MB的圖片需要壓縮到2MB以下,且不改變原圖的尺寸。
(推薦教學:java入門教學)
引入依賴:
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version> </dependency>
附件實體類別:
@Builder @NoArgsConstructor @AllArgsConstructor @Data public class FileCO { /** * 附件字节流 */ private byte[] fileContent; /** * 附件OID */ private UUID attachmentOid; }
(影片教學建議:java影片教學)
圖片實體類別:
@Builder @NoArgsConstructor @AllArgsConstructor @Data public class ImageInfo { /** * 图片字节流 */ private byte[] imageBytes; /** * 图片是否进行压缩 */ private Boolean compressFlag; /** * 图片宽度 */ private Integer width; /** * 图片高度 */ private Integer height; }
圖片壓縮工具類別:
@Slf4j public class ImageUtils { /** * 合法图片大小为2MB */ private static final Long LEGAL_IMAGE_SIZE = 1024 * 2L; /** * 图片压缩 当图片大小大于2MB进行等比例压缩 * 不修改图片尺寸进行压缩 * * @param fileCO * @return */ public static ImageInfo compressImageForScale(FileCO fileCO) throws IOException { byte[] imageBytes = fileCO.getFileContent(); UUID attachmentOid = fileCO.getAttachmentOid(); try { BufferedImage sourceImage = ImageIO.read(new ByteArrayInputStream(imageBytes)); //高度 int height = sourceImage.getHeight(); //宽度 int width = sourceImage.getWidth(); if (imageBytes.length <= 0 || imageBytes.length < LEGAL_IMAGE_SIZE * 1024) { return ImageInfo.builder() .imageBytes(imageBytes) .width(width) .height(height) .compressFlag(false) .build(); } long srcSize = imageBytes.length; double accuracy = getAccuracy(srcSize / 1024); while (imageBytes.length > LEGAL_IMAGE_SIZE * 1024) { ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length); Thumbnails.of(inputStream) .scale(1f) .outputQuality(accuracy) .toOutputStream(outputStream); imageBytes = outputStream.toByteArray(); } log.info("【图片压缩】附件OID={} | 图片原大小={}kb | 压缩后大小={}kb", attachmentOid, srcSize / 1024, imageBytes.length / 1024); return ImageInfo.builder() .imageBytes(imageBytes) .width(width) .height(height) .compressFlag(true) .build(); } catch (Exception e) { log.error("【图片压缩】msg=图片压缩失败!", e); throw e; } } /** * 计算压缩精度 * * @param size * @return */ private static double getAccuracy(long size) { double accuracy; //图片大小小于4M,压缩精度为0.44;否则精度为0.4 if (size <= 2048 * 2) { accuracy = 0.44; } else { accuracy = 0.4; } return accuracy; } }
以上是java實作壓縮圖片且不改變原圖尺寸的詳細內容。更多資訊請關注PHP中文網其他相關文章!