分享

java图片高质量缩放类

 ada_lib 2015-03-25

找了很多都不理想,最后找个到老外写的,不得不承认老外写的确实牛B。

Java代码  收藏代码
  1. package com.test;  
  2.   
  3. import com.sun.image.codec.jpeg.JPEGImageEncoder;  
  4. import com.sun.image.codec.jpeg.JPEGCodec;  
  5. import com.sun.image.codec.jpeg.JPEGEncodeParam;  
  6. import javax.swing.*;  
  7. import java.io.File;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.awt.*;  
  11. import java.awt.image.BufferedImage;  
  12. import java.awt.image.Kernel;  
  13. import java.awt.image.ConvolveOp;  
  14.   
  15. public class ImageUtil {  
  16.   
  17.     public static void resize(File originalFile, File resizedFile,  
  18.             int newWidth, float quality) throws IOException {  
  19.   
  20.         if (quality > 1) {  
  21.             throw new IllegalArgumentException(  
  22.                     "Quality has to be between 0 and 1");  
  23.         }  
  24.   
  25.         ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());  
  26.         Image i = ii.getImage();  
  27.         Image resizedImage = null;  
  28.   
  29.         int iWidth = i.getWidth(null);  
  30.         int iHeight = i.getHeight(null);  
  31.   
  32.         if (iWidth > iHeight) {  
  33.             resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)  
  34.                     / iWidth, Image.SCALE_SMOOTH);  
  35.         } else {  
  36.             resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,  
  37.                     newWidth, Image.SCALE_SMOOTH);  
  38.         }  
  39.   
  40.         // This code ensures that all the pixels in the image are loaded.  
  41.         Image temp = new ImageIcon(resizedImage).getImage();  
  42.   
  43.         // Create the buffered image.  
  44.         BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),  
  45.                 temp.getHeight(null), BufferedImage.TYPE_INT_RGB);  
  46.   
  47.         // Copy image to buffered image.  
  48.         Graphics g = bufferedImage.createGraphics();  
  49.   
  50.         // Clear background and paint the image.  
  51.         g.setColor(Color.white);  
  52.         g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));  
  53.         g.drawImage(temp, 0, 0, null);  
  54.         g.dispose();  
  55.   
  56.         // Soften.  
  57.         float softenFactor = 0.05f;  
  58.         float[] softenArray = { 0, softenFactor, 0, softenFactor,  
  59.                 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };  
  60.         Kernel kernel = new Kernel(3, 3, softenArray);  
  61.         ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);  
  62.         bufferedImage = cOp.filter(bufferedImage, null);  
  63.   
  64.         // Write the jpeg to a file.  
  65.         FileOutputStream out = new FileOutputStream(resizedFile);  
  66.   
  67.         // Encodes image as a JPEG data stream  
  68.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
  69.   
  70.         JPEGEncodeParam param = encoder  
  71.                 .getDefaultJPEGEncodeParam(bufferedImage);  
  72.   
  73.         param.setQuality(quality, true);  
  74.   
  75.         encoder.setJPEGEncodeParam(param);  
  76.         encoder.encode(bufferedImage);  
  77.     } // Example usage  
  78.   
  79.     public static void main(String[] args) throws IOException {  
  80. //       File originalImage = new File("C:\\11.jpg");  
  81. //       resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);  
  82. //       resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);  
  83.          File originalImage = new File("C:\\1207.gif");  
  84.          resize(originalImage, new File("c:\\1207-0.jpg"),150, 0.7f);  
  85.          resize(originalImage, new File("c:\\1207-1.jpg"),150, 1f);  
  86.     }  
  87. }  

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约