栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java图片压缩工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java图片压缩工具类

引入依赖:


    net.coobird
    thumbnailator
    0.4.14
import cn.hutool.core.codec.base64;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.UUID;

public class ImageCompressUtils {

    public static void compress( File inputFile,String outputFilePath ) throws IOException {
        FileInputStream fis_inputFile = new FileInputStream(inputFile);
        BufferedImage sourceImg = ImageIO.read( fis_inputFile );
        int width = sourceImg.getWidth();
        double scale = BigDecimal.valueOf( 1000L )
                .divide( BigDecimal.valueOf( width ),5, RoundingMode.HALF_UP )
                .doublevalue();

        System.out.println( "scale is " + scale );
        FileOutputStream fos_outputFile = new FileOutputStream( outputFilePath );
        Thumbnails.of( inputFile )
                    .scale( scale ) //图片大小(长宽)压缩比例 从0-1,1表示原图
                    .outputQuality( 0.5d ) //图片质量压缩比例 从0-1,越接近1质量越好
                    .toOutputStream( fos_outputFile );
        fis_inputFile.close();
        fos_outputFile.close();
    }

    public static void compress( String inputFilePath,String outputFilePath ) throws IOException {
        File inputFile = new File( inputFilePath );
        ImageCompressUtils.compress( inputFile,outputFilePath );
    }

    public static String compress( File inputFile ) throws IOException {
        String inputFilePath = inputFile.getAbsolutePath();
        System.out.println( inputFilePath );

        
        int i = inputFilePath.lastIndexOf("\");
        String tempFilePath = inputFilePath.substring(0, i);
        tempFilePath = tempFilePath+"\" + System.currentTimeMillis() + UUID.randomUUID().toString().replaceAll("-","") + ".jpg";
        System.out.println( "tempFilePath=" + tempFilePath );
        compress( inputFile,tempFilePath );
        File tempFile = new File(tempFilePath);

        // tempFile 2 base64
        FileInputStream fis_tempFile = new FileInputStream(tempFile);
        BufferedImage bufferedImage = ImageIO.read( fis_tempFile );
        String base64 = "data:image/png;base64," + ImageCompressUtils.imageTobase64( bufferedImage );
        fis_tempFile.close();

        if( tempFile.exists() ){
            tempFile.delete();
        }
        return base64;
    }

    
    private static String imageTobase64(BufferedImage bufferedImage) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, "png", baos);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new String(base64.encode((baos.toByteArray())));
    }

    public static String compress( String inputFilePath ) throws IOException {
        File inputFile = new File(inputFilePath);
        String base64 = compress(inputFile);
        return base64;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749357.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号