Java壓縮圖片util,可等比例寬高不失真壓縮,也可直接指定壓縮後的寬高
阿新 • • 發佈:2019-01-27
package com.yipai.util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class ImageCompressUtil { /** * 直接指定壓縮後的寬高: * (先儲存原檔案,再壓縮、上傳) * 壹拍專案中用於二維碼壓縮 * @param oldFile 要進行壓縮的檔案全路徑 * @param width 壓縮後的寬度 * @param height 壓縮後的高度 * @param quality 壓縮質量 * @param smallIcon 檔名的小小字尾(注意,非檔案字尾名稱),入壓縮檔名是yasuo.jpg,則壓縮後文件名是yasuo(+smallIcon).jpg * @return 返回壓縮後的檔案的全路徑 */ public static String zipImageFile(String oldFile, int width, int height, float quality, String smallIcon) { if (oldFile == null) { return null; } String newImage = null; try { /**對伺服器上的臨時檔案進行處理 */ Image srcFile = ImageIO.read(new File(oldFile)); /** 寬,高設定 */ BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null); String filePrex = oldFile.substring(0, oldFile.indexOf('.')); /** 壓縮後的檔名 */ newImage = filePrex + smallIcon + oldFile.substring(filePrex.length()); /** 壓縮之後臨時存放位置 */ FileOutputStream out = new FileOutputStream(newImage); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag); /** 壓縮質量 */ jep.setQuality(quality, true); encoder.encode(tag, jep); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return newImage; } /** * 儲存檔案到伺服器臨時路徑(用於檔案上傳) * @param fileName * @param is * @return 檔案全路徑 */ public static String writeFile(String fileName, InputStream is) { if (fileName == null || fileName.trim().length() == 0) { return null; } try { /** 首先儲存到臨時檔案 */ FileOutputStream fos = new FileOutputStream(fileName); byte[] readBytes = new byte[512];// 緩衝大小 int readed = 0; while ((readed = is.read(readBytes)) > 0) { fos.write(readBytes, 0, readed); } fos.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fileName; } /** * 等比例壓縮演算法: * 演算法思想:根據壓縮基數和壓縮比來壓縮原圖,生產一張圖片效果最接近原圖的縮圖 * @param srcURL 原圖地址 * @param deskURL 縮圖地址 * @param comBase 壓縮基數 * @param scale 壓縮限制(寬/高)比例 一般用1: * 當scale>=1,縮圖height=comBase,width按原圖寬高比例;若scale<1,縮圖width=comBase,height按原圖寬高比例 * @throws Exception * @author shenbin * @createTime 2014-12-16 * @lastModifyTime 2014-12-16 */ public static void saveMinPhoto(String srcURL, String deskURL, double comBase, double scale) throws Exception { File srcFile = new java.io.File(srcURL); Image src = ImageIO.read(srcFile); int srcHeight = src.getHeight(null); int srcWidth = src.getWidth(null); int deskHeight = 0;// 縮圖高 int deskWidth = 0;// 縮圖寬 double srcScale = (double) srcHeight / srcWidth; /**縮圖寬高演算法*/ if ((double) srcHeight > comBase || (double) srcWidth > comBase) { if (srcScale >= scale || 1 / srcScale > scale) { if (srcScale >= scale) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } else { if ((double) srcHeight > comBase) { deskHeight = (int) comBase; deskWidth = srcWidth * deskHeight / srcHeight; } else { deskWidth = (int) comBase; deskHeight = srcHeight * deskWidth / srcWidth; } } } else { deskHeight = srcHeight; deskWidth = srcWidth; } BufferedImage tag = new BufferedImage(deskWidth, deskHeight, BufferedImage.TYPE_3BYTE_BGR); tag.getGraphics().drawImage(src, 0, 0, deskWidth, deskHeight, null); //繪製縮小後的圖 FileOutputStream deskImage = new FileOutputStream(deskURL); //輸出到檔案流 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(deskImage); encoder.encode(tag); //近JPEG編碼 deskImage.close(); } public static void main(String args[]) throws Exception { ImageCompressUtil.zipImageFile("f:/食屍鬼 - 藿香.jpg", 1280, 1280, 1f, "x2"); ImageCompressUtil.saveMinPhoto("f:/食屍鬼 - 藿香.jpg", "f:/11.jpg", 139, 0.9d); } }
轉自:http://www.zuidaima.com/code/file/2156534163489792.htm?dir=/2156534163489792.java