使用java對圖片進行縮放,java
pom依賴spa
<!-- 照片處理 --> <dependency> <groupId>com.mortennobel</groupId> <artifactId>java-image-scaling</artifactId> <version>0.8.6</version> </dependency>
java範例代碼.net
package com.mypackage; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URI; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import com.mortennobel.imagescaling.DimensionConstrain; import com.mortennobel.imagescaling.ResampleOp; public class ImageUtil { /** * 默認縮放比例 */ public final static int SCALE = 10; /** * 默認縮放後圖片寬度 */ //public final static int WIDTH = ApplicationConfig.getInstance().getThumbnailWidth(); public final static int WIDTH = 100; /** * 默認縮放後圖片高度 */ //public final static int HEIGHT = ApplicationConfig.getInstance().getThumbnailHeight(); public final static int HEIGHT = 100; /** * gif */ public final static String GIF = "gif"; /** * png */ public final static String PNG = "png"; public static byte[] getImageByUrl(URI uri) { File file = new File(uri); if (file.exists() && file.isFile()) { return file2byte(file); } return null; } public static byte[] getImageByPath(String path) { File file = new File(path); if (file.exists() && file.isFile()) { return file2byte(file); } return null; } // public static saveImage public static byte[] file2byte(File file) { byte[] buffer = null; try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * 按默認比例縮放圖片。 * * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @return 縮略圖文件流 */ public static InputStream getThumbnailByDefaultScale(InputStream src, String fileSuffix) throws Exception { return getThumbnailByScale(src, fileSuffix, SCALE); } /** * 按比例縮放圖片。 * * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @param scale 圖片縮放比例 * @return 縮略圖文件流 */ public static InputStream getThumbnailByScale(InputStream src, String fileSuffix, int scale) throws Exception { if(GIF.equals(fileSuffix.toLowerCase())) { return src; } InputStream is = null; try { BufferedImage image = ImageIO.read(src); int width = image.getWidth(null); int height = image.getHeight(null); int w = width / scale; int h = height / scale; ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(w, h, true)); BufferedImage to = rsop.filter(image, null); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(to, fileSuffix, imOut); is = new ByteArrayInputStream(bs.toByteArray()); } catch (Exception ex) { ex.printStackTrace(); throw ex; } return is; } /** * 按默認寬度高度縮放。 * * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @return 縮略圖文件流 */ public static InputStream getThumbnail(InputStream src, String fileSuffix) throws Exception { return compressImage(src, fileSuffix, WIDTH, HEIGHT); } /** * 指定寬度高度縮放。<br> * 圖片的實際寬高不足時返回null * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @param _width 縮略圖寬度 * @param _height 縮略圖高度 * @return 縮略圖文件流 */ public static InputStream getThumbnail(InputStream src, String fileSuffix, int _width, int _height) throws Exception { if(GIF.equals(fileSuffix.toLowerCase())) { return src; } InputStream is = null; try { BufferedImage bi2 = ImageIO.read(src); //原圖寬高 int width = bi2.getWidth(null); int height = bi2.getHeight(null); if(width < _width || height < _height){ return null; } //縮放後寬高 int newWidth = 0; int newHeight = 0; if(width <= _width && height <= _height) { _width = width; _height = height; } //計算按原圖的橫向縱向最大比例方向縮放 //橫向圖片的場合 if (width / _width > height / _height) { newWidth = _width; newHeight = _width * height / width; } else { newHeight = _height; newWidth = _height * width / height; } ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(newWidth, newHeight, true)); BufferedImage to = rsop.filter(bi2, null); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(to, fileSuffix, imOut); is = new ByteArrayInputStream(bs.toByteArray()); } catch (Exception ex) { ex.printStackTrace(); throw ex; } finally{ //關於原來的流 if(src != null){ src.close(); } } return is; } /** * 根據原圖壓縮爲系統容許的最大的尺寸的圖片。 * * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @return 縮略圖文件流 */ public static InputStream compressMaxImage(InputStream src, String fileSuffix, int maxWidth) throws Exception { return compressImage(src, fileSuffix, maxWidth); } /** * 固定圖片寬高縮放(按原圖的橫向縱向最大比例方向縮放)。 * * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @param _width 縮略圖寬度 * @return 縮略圖文件流 */ public static InputStream compressImage(InputStream src, String fileSuffix, int _width, int _height) throws Exception { if(GIF.equals(fileSuffix.toLowerCase())) { return src; } InputStream is = null; try { BufferedImage bi2 = ImageIO.read(src); //原圖寬高 int width = bi2.getWidth(null); int height = bi2.getHeight(null); //縮放後寬高 int newWidth = 0; int newHeight = 0; if(width <= _width && height <= _height) { _width = width; _height = height; } //計算按原圖的橫向縱向最大比例方向縮放 //橫向圖片的場合 if (width / _width > height / _height) { newWidth = _width; newHeight = _width * height / width; } else { newHeight = _height; newWidth = _height * width / height; } ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(newWidth, newHeight, true)); BufferedImage to = rsop.filter(bi2, null); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(to, fileSuffix, imOut); is = new ByteArrayInputStream(bs.toByteArray()); //關於原來的流 if(src != null){ src.close(); } } catch (Exception ex) { ex.printStackTrace(); throw ex; } return is; } /** * 固定圖片寬度縮放。 * * @param src 源圖片文件流 * @param fileSuffix 後綴名 * @param _width 縮略圖寬度 * @return 縮略圖文件流 */ public static InputStream compressImage(InputStream src, String fileSuffix, int _width) throws Exception { if(GIF.equals(fileSuffix.toLowerCase())) { return src; } InputStream is = null; try { BufferedImage bi2 = ImageIO.read(src); //原圖寬高 int width = bi2.getWidth(null); int height = bi2.getHeight(null); //縮放後寬高 int newWidth = 0; int newHeight = 0; if(width < _width) { _width = width; } newWidth = _width; //計算按原圖的橫向縱向最大比例方向縮放 //橫向圖片的場合 newHeight = height * _width / width; ResampleOp rsop = new ResampleOp(DimensionConstrain.createMaxDimension(newWidth, newHeight, true)); BufferedImage to = rsop.filter(bi2, null); ByteArrayOutputStream bs = new ByteArrayOutputStream(); ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); ImageIO.write(to, fileSuffix, imOut); is = new ByteArrayInputStream(bs.toByteArray()); //關於原來的流 if(src != null){ src.close(); } } catch (Exception ex) { ex.printStackTrace(); throw ex; } return is; } }