java圖片壓縮(Thumbnails)
阿新 • • 發佈:2018-12-14
article details 目標 最大 大小 mage util pac nts
package com.hzxc.groupactivity.server.util; import java.awt.image.BufferedImage; import java.io.*; import java.math.BigDecimal; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import org.apache.commons.lang3.StringUtils; /** * Created by hdwang on 2018/12/13. */ public classPicUtils { public static void main(String[] args) { PicUtils.commpressPicForScale("/Users/hdwang/Downloads/1.jpg","/Users/hdwang/Downloads/1_s.jpg", 1000, 0.8,750,1334); // 圖片小於1000kb } /** * 根據指定大小和指定精度壓縮圖片 * * @param srcPath * 源圖片地址 * @param desPath * 目標圖片地址 *@param desFileSize * 指定圖片大小,單位kb * @param accuracy * 精度,遞歸壓縮的比率,建議小於0.9 * @param desMaxWidth * 目標最大寬度 * @param desMaxHeight * 目標最大高度 * @return 目標文件路徑 */ public static String commpressPicForScale(String srcPath, String desPath,long desFileSize, double accuracy,int desMaxWidth,int desMaxHeight) { if (StringUtils.isEmpty(srcPath) || StringUtils.isEmpty(srcPath)) { return null; } if (!new File(srcPath).exists()) { return null; } try { File srcFile = new File(srcPath); long srcFileSize = srcFile.length(); System.out.println("源圖片:" + srcPath + ",大小:" + srcFileSize / 1024 + "kb"); //獲取圖片信息 BufferedImage bim = ImageIO.read(srcFile); int srcWidth = bim.getWidth(); int srcHeight = bim.getHeight(); //先轉換成jpg Thumbnails.Builder builder = Thumbnails.of(srcFile).outputFormat("jpg"); // 指定大小(寬或高超出會才會被縮放) if(srcWidth > desMaxWidth || srcHeight > desMaxHeight) { builder.size(desMaxWidth, desMaxHeight); }else{ //寬高均小,指定原大小 builder.size(srcWidth,srcHeight); } // 寫入到內存 ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字節輸出流(寫入到內存) builder.toOutputStream(baos); // 遞歸壓縮,直到目標文件大小小於desFileSize byte[] bytes = commpressPicCycle(baos.toByteArray(), desFileSize, accuracy); // 輸出到文件 File desFile = new File(desPath); FileOutputStream fos = new FileOutputStream(desFile); fos.write(bytes); System.out.println("目標圖片:" + desPath + ",大小" + desFile.length() / 1024 + "kb"); System.out.println("圖片壓縮完成!"); } catch (Exception e) { e.printStackTrace(); return null; } return desPath; } private static byte[] commpressPicCycle(byte[] bytes, long desFileSize, double accuracy) throws IOException { // File srcFileJPG = new File(desPath); long srcFileSizeJPG = bytes.length; // 2、判斷大小,如果小於500kb,不壓縮;如果大於等於500kb,壓縮 if (srcFileSizeJPG <= desFileSize * 1024) { return bytes; } // 計算寬高 BufferedImage bim = ImageIO.read(new ByteArrayInputStream(bytes)); int srcWdith = bim.getWidth(); int srcHeigth = bim.getHeight(); int desWidth = new BigDecimal(srcWdith).multiply( new BigDecimal(accuracy)).intValue(); int desHeight = new BigDecimal(srcHeigth).multiply( new BigDecimal(accuracy)).intValue(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字節輸出流(寫入到內存) Thumbnails.of(new ByteArrayInputStream(bytes)).size(desWidth, desHeight).outputQuality(accuracy).toOutputStream(baos); return commpressPicCycle(baos.toByteArray(), desFileSize, accuracy); } }
參考文章:
https://blog.csdn.net/qiaqia609/article/details/53171149
https://blog.csdn.net/qiaqia609/article/details/53171149
java圖片壓縮(Thumbnails)