pom.xml <!-- thumbnail generation library for Java --> <dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</artifactId> <version>0.4.8</version></dependency>
package team.soi.utils; import net.coobird.thumbnailator.Thumbnails; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; /** * 圖片工具類:<br/> * 使用Thumbnailtor實現圖片的裁剪,縮放等。<br/> * 代碼基於hoojo的工具類修改,主要是由於其代碼使用了底層的API,具備平臺依賴性。 * * @author Soi 2015-10-15 * @email ycrxun@163.com * @blog http://my.oschina.net/u/1580674 */ public class ImageUtils { /** * 圖像文件的格式 */ private static final String DEFAULT_IMAGE_FORMAT = ".jpg"; /** * 默認文件存放路徑 */ private static final String DEFAULT_FILE_PATH = "./temp-"; private static Image image = null; private ImageUtils() { } /** * 圖片壓縮質量枚舉<br/> * MAX-1.0f<br/> * HIGH-0.75f<br/> * MEDIUM-0.5f<br/> * LOW-0.25f<br/> */ public enum ImageQuality { MAX(1.0f), HIGH(0.75f), MEDIUM(0.5f), LOW(0.25f); final Float QUALITY; ImageQuality(Float quality) { this.QUALITY = quality; } } /** * <b>function:</b> 經過目標對象的大小和標準(指定)大小計算出圖片縮小的比例 * * @param targetWidth 目標的寬度 * @param targetHeight 目標的高度 * @param standardWidth 標準(指定)寬度 * @param standardHeight 標準(指定)高度 * @return 最小的合適比例 * @author hoojo * @createDate 2012-2-6 下午04:41:48 */ public static double getScaling(double targetWidth, double targetHeight, double standardWidth, double standardHeight) { double widthScaling = 0d; double heightScaling = 0d; if (targetWidth > standardWidth) { widthScaling = standardWidth / (targetWidth * 1.00d); } else { widthScaling = 1d; } if (targetHeight > standardHeight) { heightScaling = standardHeight / (targetHeight * 1.00d); } else { heightScaling = 1d; } return Math.min(widthScaling, heightScaling); } /** * 將Image的寬度、高度縮放到指定width、height,並保存在savePath目錄<br/> * * @param width 縮放的寬度 * @param height 縮放的高度 * @param savePath 保存目錄 * @param targetImage 即將縮放的目標圖片 * @return 圖片保存路徑、名稱 * @throws IOException * @author Soi */ public static String resize(int width, int height, String savePath, Image targetImage) throws IOException { return resize(width, height, ImageQuality.MAX.QUALITY, savePath, targetImage); } /** * <b>function:</b> 能夠設置圖片縮放質量,而且能夠根據指定的寬高縮放圖片 * * @param width 縮放的寬度 * @param height 縮放的高度 * @param quality 圖片壓縮質量,最大值是1; 使用枚舉值:{@link ImageQuality} * Some guidelines: 0.75 high quality、0.5 medium quality、0.25 low quality * @param savePath 保存目錄 * @param targetImage 即將縮放的目標圖片 * @return 圖片保存路徑、名稱 * @throws IOException * @author hoojo * @createDate 2012-2-7 上午11:01:27 */ public static String resize(int width, int height, Float quality, String savePath, Image targetImage) throws IOException { width = Math.max(width, 1); height = Math.max(height, 1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); image.getGraphics().drawImage(targetImage, 0, 0, width, height, null); if (savePath == null || "".equals(savePath)) { savePath = DEFAULT_FILE_PATH + System.currentTimeMillis() + DEFAULT_IMAGE_FORMAT; } if (quality == null || quality <= 0) { quality = ImageQuality.MAX.QUALITY; } Thumbnails.of(image).size(width, height).outputQuality(quality).toFile(new File(savePath)); return savePath; } public static String resize(final int width, final int height, final String savePath, final String targetFile) throws IOException { return resize(width, height, savePath, new File(targetFile)); } public static String resize(final int width, final int height, final String savePath, final File targetFile) throws IOException { return resize(width, height, savePath, new FileInputStream(targetFile)); } public static String resize(final int width, final int height, final String savePath, InputStream inputStream) throws IOException { image = ImageIO.read(inputStream); int[] size = getSize(width, height, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 將一個網絡圖片文件按照指定的比例進行縮放 * * @param scale 縮放比例 * @param savePath 保存文件路徑、名稱 * @param targetURL 本地圖片文件 * @return 新的文件名稱 * @throws IOException * @author hoojo * @createDate 2012-2-7 上午10:30:56 */ public static String resize(final float scale, final String savePath, final URL targetURL) throws IOException { image = ImageIO.read(targetURL); int[] size = getSize(scale, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定寬度進行等比縮放本地圖片 * * @param width 固定寬度 * @param savePath 保存路徑、名稱 * @param targetFile 本地目標文件 * @return 返回保存路徑 * @throws IOException * @author hoojo * @createDate 2012-2-7 上午10:49:56 */ public static String resize(final int width, final String savePath, final File targetFile) throws IOException { image = ImageIO.read(targetFile); int[] size = getSize(width, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定寬度進行等比縮放網絡圖片 * * @param width 固定寬度 * @param savePath 保存路徑、名稱 * @param targetURL 本地目標文件 * @return 返回保存路徑 * @throws IOException * @author hoojo * @createDate 2012-2-7 上午10:50:52 */ public static String resize(final int width, final String savePath, final URL targetURL) throws IOException { image = ImageIO.read(targetURL); int[] size = getSize(width, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定高度進行等比縮放本地圖片 * * @param height 固定高度 * @param savePath 保存路徑、名稱 * @param targetFile 本地目標文件 * @return 返回保存路徑 * @throws IOException * @author hoojo * @createDate 2012-2-7 上午10:51:17 */ public static String resizeByHeight(final int height, final String savePath, final File targetFile) throws IOException { image = ImageIO.read(targetFile); int[] size = getSizeByHeight(height, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 按照固定高度進行等比縮放網絡圖片 * * @param height 固定高度 * @param savePath 保存路徑、名稱 * @param targetURL 本地目標文件 * @return 返回保存路徑 * @throws IOException * @author hoojo * @createDate 2012-2-7 上午10:52:23 */ public static String resizeByHeight(final int height, final String savePath, final URL targetURL) throws IOException { image = ImageIO.read(targetURL); int[] size = getSizeByHeight(height, image); return resize(size[0], size[1], savePath, image); } /** * <b>function:</b> 經過指定大小和圖片的大小,計算出圖片縮小的合適大小 * * @param width 指定的寬度 * @param height 指定的高度 * @param image 圖片文件 * @return 返回寬度、高度的int數組 * @author hoojo * @createDate 2012-2-6 下午05:53:10 */ public static int[] getSize(final int width, final int height, final Image image) { int[] target = getSize(image); double scaling = getScaling(target[0], target[1], width, height); long[] standard = getStandard(target, scaling); return new int[]{Integer.parseInt(Long.toString(standard[0])), Integer.parseInt(String.valueOf(standard[1]))}; } /** * <b>function:</b> 經過指定的比例和圖片對象,返回一個放大或縮小的寬度、高度 * * @param scale 縮放比例 * @param image 圖片對象 * @return 返回寬度、高度 * @author hoojo * @createDate 2012-2-7 上午10:27:59 */ public static int[] getSize(final float scale, final Image image) { int[] target = getSize(image); long[] standard = getStandard(target, scale); return new int[]{Integer.parseInt(Long.toString(standard[0])), Integer.parseInt(String.valueOf(standard[1]))}; } public static int[] getSize(final int width, final Image image) { int[] target = getSize(image); long height = Math.round((target[1] * width) / (target[0] * 1.00f)); return new int[]{width, Integer.parseInt(String.valueOf(height))}; } public static int[] getSizeByHeight(final int height, final Image image) { int[] target = getSize(image); long width = Math.round((target[0] * height) / (target[1] * 1.00f)); return new int[]{Integer.parseInt(String.valueOf(width)), height}; } public static int[] getSize(final Image image) { int targetWidth = image.getWidth(null); int targetHeight = image.getHeight(null); return new int[]{targetWidth, targetHeight}; } public static long[] getStandard(final int[] target, final double scale) { long standardWidth = Math.round(target[0] * scale); long standardHeight = Math.round(target[1] * scale); return new long[]{standardWidth, standardHeight}; } public static void main(String[] args) throws IOException{ ImageUtils.resize(500,500,"/home/soi/xn.png","/home/soi/012133dsa4toxud8snsbo1.jpg"); } }