日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 综合教程 >内容正文

综合教程

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

發(fā)布時(shí)間:2023/12/24 综合教程 29 生活家
生活随笔 收集整理的這篇文章主要介紹了 java实现压缩图片且不改变原图尺寸 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

需求:

大于2MB的圖片需要壓縮到2MB以下,且不改變?cè)瓐D的尺寸。

(推薦教程:java入門教程)

引入依賴:

        <dependency>
            <groupId>net.coobird</groupId>
            <artifactId>thumbnailator</artifactId>
            <version>0.4.8</version>
        </dependency>

附件實(shí)體類:

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class FileCO {
    /**
     * 附件字節(jié)流
     */
    private byte[] fileContent;

    /**
     * 附件OID
     */
    private UUID attachmentOid;
}

(視頻教程推薦:java視頻教程)

圖片實(shí)體類:

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class ImageInfo {

    /**
     * 圖片字節(jié)流
     */
    private byte[] imageBytes;

    /**
     * 圖片是否進(jìn)行壓縮
     */
    private Boolean compressFlag;

    /**
     * 圖片寬度
     */
    private Integer width;

    /**
     * 圖片高度
     */
    private Integer height;
}

圖片壓縮工具類:

@Slf4j
public class ImageUtils {

    /**
     * 合法圖片大小為2MB
     */
    private static final Long LEGAL_IMAGE_SIZE = 1024 * 2L;

    /**
     * 圖片壓縮 當(dāng)圖片大小大于2MB進(jìn)行等比例壓縮
     * 不修改圖片尺寸進(jìn)行壓縮
     *
     * @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;
        }
    }

    /**
     * 計(jì)算壓縮精度
     *
     * @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;
    }
}

總結(jié)

以上是生活随笔為你收集整理的java实现压缩图片且不改变原图尺寸的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。