public static BufferedImage imgCompressByScale(Image srcImg, double base, int wh, Float quality,
                                                   boolean isForceWh) {
        int width = 0;
        int height = 0;
        BufferedImage bufferedImage = null;
        try {
            if (wh == ImgCompressUtils.HEIGHT) {
                if (base > srcImg.getHeight(null) && !isForceWh) {
                    width = srcImg.getWidth(null);
                    height = srcImg.getHeight(null);
                } else {
                    //根据高度等比例设置宽高
                    height = (int) Math.floor(base);
                    width = (int) Math.floor((srcImg.getWidth(null) * base / srcImg.getHeight(null)));
                }
            } else if (wh == ImgCompressUtils.WIDTH) {
                //根据宽度等比例设置宽高
                if (base > srcImg.getWidth(null) && !isForceWh) {
                    height = srcImg.getHeight(null);
                    width = srcImg.getWidth(null);
                } else {
                    width = (int) Math.floor(base);
                    height = (int) Math.floor((srcImg.getHeight(null) * base / srcImg.getWidth(null)));
                }
            } else if (wh == ImgCompressUtils.NO_CHANGE) {
                //不改变原始宽高
                height = srcImg.getHeight(null);
                width = srcImg.getWidth(null);
            } else {
                throw new Exception("wh参数指定值不正确");
            }
            //指定目标图像
            BufferedImage desImg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
            //绘制目标图像
            desImg.getGraphics().drawImage(srcImg, 0, 0, width, height, null);

            bufferedImage = encodeImg(desImg, quality);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return bufferedImage;
    }
public static BufferedImage imgCompressByScale ( Image srcImg , double base , int wh , Float quality , boolean isForceWh ) { int width = 0 ; int height = 0 ; BufferedImage bufferedImage = null ; try { if ( wh == ImgCompressUtils . HEIGHT ) { if ( base > srcImg . getHeight ( null ) && ! isForceWh ) { width = srcImg . getWidth ( null ) ; height = srcImg . getHeight ( null ) ; } else { height = ( int ) Math . floor ( base ) ; width = ( int ) Math . floor ( ( srcImg . getWidth ( null ) * base / srcImg . getHeight ( null ) ) ) ; } } else if ( wh == ImgCompressUtils . WIDTH ) { if ( base > srcImg . getWidth ( null ) && ! isForceWh ) { height = srcImg . getHeight ( null ) ; width = srcImg . getWidth ( null ) ; } else { width = ( int ) Math . floor ( base ) ; height = ( int ) Math . floor ( ( srcImg . getHeight ( null ) * base / srcImg . getWidth ( null ) ) ) ; } } else if ( wh == ImgCompressUtils . NO_CHANGE ) { height = srcImg . getHeight ( null ) ; width = srcImg . getWidth ( null ) ; } else { throw new Exception ( "wh参数指定值不正确" ) ; } BufferedImage desImg = new BufferedImage ( width , height , BufferedImage . TYPE_3BYTE_BGR ) ; desImg . getGraphics ( ) . drawImage ( srcImg , 0 , 0 , width , height , null ) ; bufferedImage = encodeImg ( desImg , quality ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } return bufferedImage ; }
根据宽或者高和指定压缩质量进行等比例压缩,注意如果指定的宽或者高大于源图片的高或者宽,
那么压缩图片将直接使用源图片的宽高。
注:若isForceWh为true,不论如何均按照指定宽高进行等比例压缩

@param srcImg    源图片地址
@param base      指定压缩后图片的宽或者高
@param wh        此参数用于指定base参数是宽还是高，该参数应由{@link ImgCompressUtils}里的
静态常量指定
@param quality   指定压缩质量，范围[0.0,1.0],如果指定为null则按照默认值
@param isForceWh 指定是否强制使用指定宽高进行等比例压缩,true代表强制,false反之
@return 返回压缩后的图像对象