温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java如何实现压缩图片且不改变原图尺寸

发布时间:2020-08-04 09:15:18 来源:亿速云 阅读:564 作者:Leah 栏目:编程语言

java如何实现压缩图片且不改变原图尺寸?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

需求:

大于2MB的图片需要压缩到2MB以下,且不改变原图的尺寸。

引入依赖:

        <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;
}

图片实体类:

@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如何实现压缩图片且不改变原图尺寸问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI