网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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

需求:

创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的五寨网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!

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

(推荐教程:java入门教程)

引入依赖:

 
            net.coobird
            thumbnailator
            0.4.8
        

附件实体类:

@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实现压缩图片且不改变原图尺寸
转载源于:http://bjjierui.cn/article/cgjjij.html

其他资讯