1. 程式人生 > 其它 >Java使用圖片壓縮工具壓縮圖片的兩種方法

Java使用圖片壓縮工具壓縮圖片的兩種方法

pom.xml

<!--thumbnailator圖片處理-->
    <dependency>
      <groupId>net.coobird</groupId>
      <artifactId>thumbnailator</artifactId>
      <version>0.4.8</version>
    </dependency>

工具類

指定大小,不失真,不丟失精度

package org.example.util;
 
 
import net.coobird.thumbnailator.Thumbnails;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; /** * @author Gavin.luo * @title: PicUtils * @projectName MyData * @description: * @date 2021/7/20 17:30 */ public class PicUtils { //以下是常量,按照阿里程式碼開發規範,不允許程式碼中出現魔法值 private static final Logger logger = LoggerFactory.getLogger(PicUtils.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; /** * 根據指定大小壓縮圖片 * * @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; } }

不能指定大小,不失真,不丟失精度,壓縮會有幾百K

package org.example.util;
 
 
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * @author Gavin.luo
 * @title: ImgCompression
 * @projectName MyData
 * @description:
 * @date 2021/7/21 14:29
 */
 
public class ImgCompression {
 
    public static byte[] getImageCom(MultipartFile file) throws IOException {
        //獲取檔案輸入流
        InputStream inputStream = file.getInputStream();
        try {
            // 把圖片讀入到記憶體中
            BufferedImage bufImg = ImageIO.read(inputStream);
            // 壓縮程式碼,儲存圖片檔案byte陣列
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            //防止圖片變紅,這一步非常重要
            BufferedImage bufferedImage = new BufferedImage(bufImg.getWidth(), bufImg.getHeight(), BufferedImage.TYPE_INT_RGB);
            bufferedImage.createGraphics().drawImage(bufImg,0,0, Color.WHITE,null);
            //先轉成jpg格式來壓縮,然後在通過OSS來修改成原始檔本來的字尾格式
            ImageIO.write(bufferedImage,"jpg",bos);
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            inputStream.close();
        }
        return null;
    }
}

使用測試

package org.example.controller;
 
import org.example.util.ImgCompression;
import org.example.util.PicUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.*;
import java.util.UUID;
 
/**
 * @author Gavin.luo
 * @title: ImageController
 * @projectName MyData
 * @description:
 * @date 2021/7/21 10:09
 */
@RestController
@RequestMapping("/image")
public class ImageController {
 
 
    @RequestMapping(value = "file/img",method = RequestMethod.POST)
    public String imgUploads(@RequestParam("file") MultipartFile file) throws IOException {
        //壓縮圖片到指定120K以內,不管你的圖片有多少兆,都不會超過120kb,精度還算可以,不會模糊
        byte[] bytes = PicUtils.compressPicForScale(file.getBytes(), 120);
        //生成儲存在伺服器的圖片名稱,統一修改原字尾名為:jpg
        String newFileName = UUID.randomUUID() + ".jpg";
        File fOut = new File("C:\\image\\copy\\" + newFileName);
        FileOutputStream fileOutputStream = new FileOutputStream(fOut);
        fileOutputStream.write(bytes);
        fileOutputStream.close();
        return newFileName;
    }
 
    @RequestMapping(value = "file/img2",method = RequestMethod.POST)
    public String imgUploads2(@RequestParam("file") MultipartFile file) throws IOException {
        byte[] bs = ImgCompression.getImageCom(file);
        String newFileName = UUID.randomUUID() + ".jpg";
        File fOut = new File("C:\\image\\copy\\" + newFileName);
        FileOutputStream fileOutputStream = new FileOutputStream(fOut);
        fileOutputStream.write(bs);
        fileOutputStream.close();
        return newFileName;
    }
 
}

測試結果:這裡我用的是3M的原圖進行測試