java图片压缩工具类

Posted 向天再借500年V

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java图片压缩工具类相关的知识,希望对你有一定的参考价值。

java图片压缩工具类

PicCompressUtil.java

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.coobird.thumbnailator.Thumbnails;
 
/**
 * @author Gavin.luo
 * @title: PicUtils
 * @projectName MyData
 * @description:
 * @date 2021/7/20 17:30
 */
public class PicCompressUtil 
    //以下是常量,按照阿里代码开发规范,不允许代码中出现魔法值
    private static final Logger logger = LoggerFactory.getLogger(PicCompressUtil.class);
    private static final Integer ZERO = 0;
    private static final Integer ONE_ZERO_TWO_FOUR = 1024;
    private static final Integer NINE_ZERO_ZERO = 900;
    private static final Integer THREE_TWO_SEVEN_FIVE = 3275;
    private static final Integer TWO_ZERO_FOUR_SEVEN = 2047;
    private static final Double ZERO_EIGHT_FIVE = 0.85;
    private static final Double ZERO_SIX = 0.6;
    private static final Double ZERO_FOUR_FOUR = 0.44;
    private static final Double ZERO_FOUR = 0.4;
 
    
    public static void main(String[] args) throws IOException 
		
    	byte[] bytes = Files.readAllBytes(Paths.get("C:\\\\Users\\\\admin\\\\Pictures\\\\qinghua\\\\hao\\\\aaa.jpg"));
    	
    	byte[] compressPicForScale = compressPicForScale(bytes, 1024*2);
    	Files.write(Paths.get("C:\\\\Users\\\\admin\\\\Pictures\\\\qinghua\\\\hao\\\\222.JPG"), compressPicForScale);
    	System.out.println("hao");
	
    
    /**
     * 根据指定大小压缩图片
     *
     * @param imageBytes  源图片字节数组
     * @param desFileSize 指定图片大小,单位kb
     * @return 压缩质量后的图片字节数组
     */
    public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) 
        if (imageBytes == null || imageBytes.length <= ZERO || imageBytes.length < desFileSize * ONE_ZERO_TWO_FOUR) 
            return imageBytes;
        
        long srcSize = imageBytes.length;
        double accuracy = getAccuracy(srcSize / ONE_ZERO_TWO_FOUR);
        try 
            while (imageBytes.length > desFileSize * ONE_ZERO_TWO_FOUR) 
                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("图片原大小=kb | 压缩后大小=kb",
                    srcSize / ONE_ZERO_TWO_FOUR, imageBytes.length / ONE_ZERO_TWO_FOUR);
         catch (Exception e) 
            logger.error("【图片压缩】msg=图片压缩失败!", e);
        
        return imageBytes;
    
 
    /**
     * 自动调节精度(经验数值)
     *
     * @param size 源图片大小
     * @return 图片压缩质量比
     */
    private static double getAccuracy(long size) 
        double accuracy;
        if (size < NINE_ZERO_ZERO) 
            accuracy = ZERO_EIGHT_FIVE;
         else if (size < TWO_ZERO_FOUR_SEVEN) 
            accuracy = ZERO_SIX;
         else if (size < THREE_TWO_SEVEN_FIVE) 
            accuracy = ZERO_FOUR_FOUR;
         else 
            accuracy = ZERO_FOUR;
        
        return accuracy;
    

以上是关于java图片压缩工具类的主要内容,如果未能解决你的问题,请参考以下文章

图片压缩java工具类

压缩图片工具类,压缩100KB以内拿走直接用

java图片压缩

android -------- 压缩图片文件工具类

图片压缩,裁切,水印工具类

您好!请问用java怎么将截取png的图片中间一部分,以及如何压缩一个png图片?