需求:原始圖片縮放爲目標尺寸後,要求縮放後的圖片保持原圖片的樣式不改變,可是縮放後圖片的四邊能夠裁減。 原理:將待裁剪圖片寬高相除值和目標尺寸寬高相除值進行比較,若是前者較大,說明待裁剪圖片相對於目標尺寸來講要寬出一塊,如今已高爲基準進行裁剪,即將原始圖片的高度縮放到目標尺寸的高度。計算出原始圖片高度和目標尺寸高度的比例,和原始圖片寬度相乘,獲得縮放後的寬度。此時的縮縮放後的高度就是目標尺寸的高度,可是寬度*確定*會比目標尺寸寬度寬。將縮放後的多餘的寬度分爲兩份右邊和左邊各裁去一份。 以寬爲基準原理相似。
代碼:java
<!-- lang: java --> public class ImgUtil { /** * * @param srcImgPath 待切割圖片路徑 * @param destImgPath 切割後圖片路徑 * @param destImgW 所需寬度 * @param destImgH 所需高度 */ public static void createThumb (String srcImgPath , String destImgPath, int destImgW , int destImgH){ //原圖片等比例縮小或放大以後的圖片 int narrowImgW ; int narrowImgH ; //原圖片大小 int srcImgW ; int srcImgH ; try { BufferedImage bi = ImageIO. read(new File(srcImgPath)); srcImgW = bi.getWidth(); srcImgH = bi.getHeight(); // 轉換圖片尺寸與目標尺寸比較 , 若是轉換圖片較小,說明轉換圖片相對於目標圖片來講高較小,須要以高爲基準進行縮放。 if((float )srcImgW /srcImgH > (float)destImgW / destImgH){ narrowImgW = ( int)(((float )destImgH / (float)srcImgH)*srcImgW); narrowImgH = destImgH; //按照原圖以高爲基準等比例縮放、或放大。這一步高爲所需圖片的高度,寬度確定會比目標寬度寬。 int cutNarrowImgSize = (narrowImgW - destImgW)/2; BufferedImage narrowImg = new BufferedImage(narrowImgW, narrowImgH,BufferedImage.TYPE_INT_RGB); narrowImg.getGraphics().drawImage(bi.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_SMOOTH ), 0, 0, null); //等比例縮放完成後寬度與目標尺寸寬度相比較 , 將多餘寬的部分分爲兩份 ,左邊刪除一部分 Image image = narrowImg.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_DEFAULT ); CropImageFilter cropFilter = new CropImageFilter(cutNarrowImgSize, 0, narrowImgW-cutNarrowImgSize, narrowImgH); Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage cutLiftNarrowImg = new BufferedImage( narrowImgW-cutNarrowImgSize, narrowImgH,BufferedImage.TYPE_INT_RGB ); cutLiftNarrowImg.getGraphics().drawImage(img, 0, 0, null); //右邊刪除一部分 image = cutLiftNarrowImg.getScaledInstance(narrowImgW-cutNarrowImgSize, narrowImgH, Image.SCALE_DEFAULT ); cropFilter = new CropImageFilter(0, 0, narrowImgW-cutNarrowImgSize*2, narrowImgH); img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage cutRightNarrowImg = new BufferedImage( narrowImgW-cutNarrowImgSize*2, narrowImgH,BufferedImage.TYPE_INT_RGB ); Graphics g = cutRightNarrowImg.getGraphics(); g.drawImage(img, 0, 0, null); // 繪製截取後的圖 g.dispose(); //輸出爲文件 最終爲所須要的格式 ImageIO. write(cutRightNarrowImg, "JPEG", new File(destImgPath)); } else{ //以寬度爲基準 narrowImgW = destImgW; narrowImgH = ( int) (((float )destImgW / (float)srcImgW)*srcImgH); int cutNarrowImgSize = (narrowImgH - destImgH)/2; BufferedImage narrowImg = new BufferedImage(narrowImgW, narrowImgH,BufferedImage.TYPE_INT_RGB); narrowImg.getGraphics().drawImage(bi.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_SMOOTH ), 0, 0, null); Image image = narrowImg.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_DEFAULT ); CropImageFilter cropFilter = new CropImageFilter(0, cutNarrowImgSize, narrowImgW, narrowImgH-cutNarrowImgSize); Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage cutTopNarrowImg = new BufferedImage( narrowImgW, narrowImgH-cutNarrowImgSize,BufferedImage. TYPE_INT_RGB); cutTopNarrowImg.getGraphics().drawImage(img, 0, 0, null); image = cutTopNarrowImg.getScaledInstance(narrowImgW, narrowImgH-cutNarrowImgSize, Image. SCALE_DEFAULT); cropFilter = new CropImageFilter(0, 0, narrowImgW, narrowImgH-cutNarrowImgSize*2); img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage cutBottomNarrowImg = new BufferedImage( narrowImgW, narrowImgH-cutNarrowImgSize*2,BufferedImage. TYPE_INT_RGB); Graphics g = cutBottomNarrowImg.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); ImageIO. write(cutBottomNarrowImg, "JPEG", new File(destImgPath)); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String []args){ createThumb("G://3.png", "G://223.jpg", 320, 212); }
}code
原圖片1:1,而且長寬都比目標尺寸要小。圖片
縮放後:get
以高爲基準縮放:原圖it
縮略圖io
已款以基準:原圖class
縮略圖:原理