• JAVA图片压缩到指定大小


    这是压缩到小于300KB的,循环压缩,一次不行再压一次,

    不废话,直接贴代码

    <!-- 图片缩略图 -->
            <dependency>
                <groupId>net.coobird</groupId>
                <artifactId>thumbnailator</artifactId>
                <version>0.4.8</version>
            </dependency>
    
    
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-lang3</artifactId>
            </dependency>
            <!--<dependency>
                <groupId>commons-codec</groupId>
                <artifactId>commons-codec</artifactId>
            </dependency>-->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-io</artifactId>
                <version>1.3.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-collections4</artifactId>
                <version>4.4</version>
            </dependency>
    package com.uniview.keepalive.util;
    
    import lombok.extern.slf4j.Slf4j;
    import net.coobird.thumbnailator.Thumbnails;
    import org.apache.commons.io.FileUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.IOException;
    
    @Slf4j
    public class PicUtils {
    
    
        private static Logger logger = LoggerFactory.getLogger(PicUtils.class);
    
        public static void main(String[] args) throws IOException {
            byte[] bytes = FileUtils.readFileToByteArray(new File("C:\\Users\\Administrator\\Desktop\\邓111111.jpg"));
            long l = System.currentTimeMillis();
            bytes = PicUtils.compressPicForScale(bytes, 300, "x");// 图片小于300kb
            System.out.println(System.currentTimeMillis() - l);
            FileUtils.writeByteArrayToFile(new File("C:\\Users\\Administrator\\Desktop\\邓11111压缩后.jpg"), bytes);
        }
    
        /**
         * 根据指定大小压缩图片
         *
         * @param imageBytes  源图片字节数组
         * @param desFileSize 指定图片大小,单位kb
         * @param imageId     影像编号
         * @return 压缩质量后的图片字节数组
         */
        public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize, String imageId) {
            if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
                return imageBytes;
            }
            long srcSize = imageBytes.length;
            double accuracy = getAccuracy(srcSize / 1024);
            try {
                while (imageBytes.length > desFileSize * 1024) {
                    ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
                    Thumbnails.of(inputStream)
                            .scale(accuracy)
                            .outputQuality(accuracy)
                            .toOutputStream(outputStream);
                    imageBytes = outputStream.toByteArray();
                }
                logger.info("【图片压缩】imageId={} | 图片原大小={}kb | 压缩后大小={}kb",
                        imageId, srcSize / 1024, imageBytes.length / 1024);
            } catch (Exception e) {
                logger.error("【图片压缩】msg=图片压缩失败!", e);
            }
            return imageBytes;
        }
    
        /**
         * 自动调节精度(经验数值)
         *
         * @param size 源图片大小
         * @return 图片压缩质量比
         */
        private static double getAccuracy(long size) {
            double accuracy;
            if (size < 900) {
                accuracy = 0.85;
            } else if (size < 2047) {
                accuracy = 0.6;
            } else if (size < 3275) {
                accuracy = 0.44;
            } else {
                accuracy = 0.4;
            }
            return accuracy;
        }
    
    }
  • 相关阅读:
    Centos7 安装 MySQL5.7
    搭建Harbor企业级docker仓库
    HAProxy安装文档
    mysqlbinlog查看 binlog日志报错mysqlbinlog: unknown variable 'default-character-set=utf8mb4'
    mysql删除数据库报错及解决方法
    服务器流量异常排查步骤(查看进程的流量)
    基于Docker Hub镜像的ProxySQL容器化部署与运行
    ProxySQL环境下,快速处理异常会话的方法(黑名单、KILL)
    DB2 SQL 错误(SQLCODE:-964,SQLSTATE:57011)处理方法
    pg_hba.conf、pool_hba.conf 以及 pool_passwd 三者间的关系
  • 原文地址:https://www.cnblogs.com/ymlyxp/p/15926683.html
Copyright © 2020-2023  润新知