Google提供的Thumbnails對圖片的各種操作(縮放、加水印、翻轉、轉換圖片格式)效能比jdk提供的好N倍,壓縮出來的圖片更清晰
阿新 • • 發佈:2019-02-10
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">jar下載:</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">http://code.google.com/p/thumbnailator/</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">版本:thumbnailator-0.4.8.jar</span>
package com.lvmama.comm.utils.pic;
import java.awt.Image; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import net.coobird.thumbnailator.Thumbnails; import net.coobird.thumbnailator.geometry.Positions; @SuppressWarnings("restriction") public class ImageUtil2 { /** * 縮放影象(按高度和寬度縮放) * @param srcImageFile 源影象檔案地址 * @param result 縮放後的影象地址 * @param height 縮放後的高度 * @param width 縮放後的寬度 * @throws IOException * @isPHP_PC php的pc端呼叫 * @isPHP_MOBILE php的移動無線端呼叫 */ public final static void scale(String srcImageFile, String newImageFile, int width,int height, boolean isPHP_PC,boolean isPHP_MOBILE) throws IOException { Image img = ImageIO.read(new File(srcImageFile)); int newWidth = img.getWidth(null),newHeight = img.getHeight(null); // 計算比例 if(isPHP_MOBILE && !isPHP_PC && img.getWidth(null) <= width){ //無線呼叫時,寬度沒有壓縮寬大,則返回原圖 }else if ((img.getHeight(null) > height) || (img.getWidth(null) > width)) { if(isPHP_PC && !isPHP_MOBILE){//PHP的微遊記PC端縮圖以定寬或定高(以較長的一邊)為基準 if(img.getWidth(null) > img.getHeight(null)){ newWidth = width; newHeight = (int)(new Integer(width).doubleValue()/img.getWidth(null)*img.getHeight(null)); }else{ newHeight = height; newWidth = (int)(new Integer(height).doubleValue()/img.getHeight(null)*img.getWidth(null)); } }else if(isPHP_MOBILE && !isPHP_PC){//PHP的微遊記(移動)端縮圖以定寬為基準 newWidth = width; newHeight = (int)(new Integer(width).doubleValue()/img.getWidth(null)*img.getHeight(null)); }else {//vst端:原圖長/壓縮長 > 原圖寬/壓縮圖寬 ? 原圖長/壓縮長:原圖寬/壓縮圖寬,以比例較大的為基準壓縮 // 壓縮比判斷方法二 double ratio_height = (new Integer(height)).doubleValue()/ img.getHeight(null); double ratio_width = (new Integer(width)).doubleValue() / img.getWidth(null); if(ratio_height>ratio_width){ newHeight = height; newWidth = (int)((new Integer(height)).doubleValue()/ img.getHeight(null) * img.getWidth(null)); }else{ newWidth = width; newHeight = (int)((new Integer(width)).doubleValue() / img.getWidth(null) * img.getHeight(null)); } } } Thumbnails.of(srcImageFile) .size(newWidth, newHeight) .toFile(newImageFile); } /** * 新增圖片水印 * * @param targetImg * 目標圖片路徑,如:C:\\myPictrue\\1.jpg * @param waterImg * 水印圖片路徑,如:C:\\myPictrue\\logo.png * @param positions 水印圖片所在位置 * * @param alpha * 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) * @param quality 壓縮清晰度 * <b>建議為1.0</b> * @throws IOException */ public final static void pressImage(String targetImg, File waterImg, Positions positions, float alpha) throws IOException { Thumbnails.of(targetImg) .watermark(positions, ImageIO.read(waterImg), alpha) .scale(1)//縮放比例 .toFile(targetImg); } /** * 把圖片印刷到圖片上 * * @param pressImg * -- 水印檔案 * @param targetImg * -- 目標檔案 * @param alpha * 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) * @param quality 壓縮清晰度 * <b>建議為1.0</b> * * @throws IOException */ public final static void pressImage(String targetImg, String pressImg,float alpha) throws IOException { Thumbnails.of(targetImg) .watermark(Positions.TOP_LEFT, ImageIO.read(new File(pressImg)), alpha) .outputQuality(1)//生成質量100% .scale(1)//縮放比例 .toFile(targetImg); } /** * 把圖片印刷到圖片上 * * @param pressImg * -- 水印檔案 * @param targetImg * -- 目標檔案 * @param position * @param alpha * 透明度(0.0 -- 1.0, 0.0為完全透明,1.0為完全不透明) * @param quality 壓縮清晰度 * <b>建議為1.0</b> * * @throws IOException */ public final static void pressImage(String targetImg, String pressImg, Positions position,float alpha) throws IOException { Thumbnails.of(targetImg) .watermark(position, ImageIO.read(new File(pressImg)), alpha) .outputQuality(1)//生成質量100% .scale(1)//縮放比例 .toFile(targetImg); } /** * 裁剪圖片 * @throws IOException */ public static void region(String sourceImg,String newImgPath,int x,int y,int width,int height) throws IOException{ //指定座標 Thumbnails.of(sourceImg) .sourceRegion(x, y, width, height)//x軸、y軸,裁剪寬、裁剪高 .size(width, height)//裁剪後的圖片生成的尺寸 //設定是否按比例 false,圖片可能會走形 預設true,必須在設定尺寸後設置 .keepAspectRatio(false) .toFile(newImgPath); } /** * 旋轉圖片 * @param sourceImage 原圖片 * @param newImage 生成的新圖片 * @param degrees 旋轉度數 * @throws IOException */ public static void rorate(String sourceImage,String newImage,double degrees) throws IOException{ Thumbnails.of(sourceImage) .rotate(degrees)//旋轉度數 .scale(1)//縮放比例 .toFile(newImage); } /** * 轉換圖片格式 * @param sourceImg 原圖 * @param newImg 轉換後的新圖 * @param format 格式 * @throws IOException */ public static void transferImageFormat(String sourceImg,String newImg,String format) throws IOException{ Thumbnails.of(sourceImg) .outputFormat(format) .scale(1) .toFile(newImg); } public static void main(String[] args) { try { //ImageUtil2.scale("C:\\Users\\chengjiangbo\\Desktop\\images\\c743d228-3ecf-4711-9187-725f252d14b1.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\480_300.jpg", 480, 300, true, false); //ImageUtil2.pressImage("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png",Positions.BOTTOM_CENTER, 1); //ImageUtil2.region("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage123.jpg",0,0,100,100); //ImageUtil2.rorate("C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage.jpg", "C:\\Users\\chengjiangbo\\Desktop\\images\\IMG_waterImage111.jpg", 90); ImageUtil2.transferImageFormat("C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.png", "C:\\Users\\chengjiangbo\\Desktop\\images\\QRCode.jpg", "JPEG"); } catch (IOException e) { e.printStackTrace(); } } }