java高质量缩放图片
可按照比例缩放,也可以指定宽高
1 import com.sun.image.codec.jpeg.JPEGImageEncoder; 2 import com.sun.image.codec.jpeg.JPEGCodec; 3 import com.sun.image.codec.jpeg.JPEGEncodeParam; 4 import javax.swing.*; 5 import java.io.File; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 import java.awt.*; 9 import java.awt.image.BufferedImage; 10 import java.awt.image.Kernel; 11 import java.awt.image.ConvolveOp; 12 13 public class ImageUtil { 14 /** 15 * 16 * @param originalFile 原文件 17 * @param resizedFile 压缩目标文件 18 * @param newWidth 压缩后的图片宽度 19 * @param quality 压缩质量(0到1之间,越高质量越好) 20 * @throws IOException 21 */ 22 23 public static void resize(File originalFile, File resizedFile, 24 int newWidth, float quality) throws IOException { 25 26 if (quality > 1) { 27 throw new IllegalArgumentException( 28 "Quality has to be between 0 and 1"); 29 } 30 31 ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath()); 32 Image i = ii.getImage(); 33 Image resizedImage = null; 34 35 int iWidth = i.getWidth(null); 36 int iHeight = i.getHeight(null); 37 //比例缩放 38 if (iWidth > iHeight) { 39 resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) 40 / iWidth, Image.SCALE_SMOOTH); 41 } else { 42 resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, 43 newWidth, Image.SCALE_SMOOTH); 44 } 45 //指定宽高 46 //resizedImage = i.getScaledInstance(560,315, Image.SCALE_SMOOTH); 47 // This code ensures that all the pixels in the image are loaded. 48 Image temp = new ImageIcon(resizedImage).getImage(); 49 50 // Create the buffered image. 51 BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), 52 temp.getHeight(null), BufferedImage.TYPE_INT_RGB); 53 54 // Copy image to buffered image. 55 Graphics g = bufferedImage.createGraphics(); 56 57 // Clear background and paint the image. 58 g.setColor(Color.white); 59 g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null)); 60 g.drawImage(temp, 0, 0, null); 61 g.dispose(); 62 63 // Soften. 64 float softenFactor = 0.05f; 65 float[] softenArray = { 0, softenFactor, 0, softenFactor, 66 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 }; 67 Kernel kernel = new Kernel(3, 3, softenArray); 68 ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null); 69 bufferedImage = cOp.filter(bufferedImage, null); 70 71 // Write the jpeg to a file. 72 FileOutputStream out = new FileOutputStream(resizedFile); 73 74 // Encodes image as a JPEG data stream 75 JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 76 77 JPEGEncodeParam param = encoder 78 .getDefaultJPEGEncodeParam(bufferedImage); 79 80 param.setQuality(quality, true); 81 82 encoder.setJPEGEncodeParam(param); 83 encoder.encode(bufferedImage); 84 } // Example usage 85 86 public static void main(String[] args) throws IOException { 87 File originalImage = new File("C:\1207.gif"); 88 resize(originalImage, new File("c:\1207-0.jpg"),150, 0.7f); 89 resize(originalImage, new File("c:\1207-1.jpg"),150, 1f); 90 } 91 }
参考博文:https://www.wanpishe.top/detail?blogId=f80863fd-f8d5-4da0-8f4e-6537ba13bd91