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;
    }

    /**
     * 将BufferedImage转换为base64字符串
     * @param bufferedImage
     * @return
     */
    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;
    }
}

你可能感兴趣的:(java8,算法,java,蓝桥杯,linq)