登录  /  注册
首页 > Java > Java入门 > 正文

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

王林
发布: 2020-08-03 16:38:38
转载
2513人浏览过

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

需求:

大于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中文网其它相关文章!

相关标签:
来源:csdn网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 技术文章
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2023 //m.sbmmt.com/ All Rights Reserved | 苏州跃动光标网络科技有限公司 | 苏ICP备2020058653号-1

 | 本站CDN由 数掘科技 提供

登录PHP中文网,和优秀的人一起学习!
全站2000+教程免费学