一个好用的java图片缩放及质量压缩方法
发布时间: 2018-10-08      作者:admin    浏览:256 次

由于网站需要对上传的图片进行宽度判断缩放和质量压缩,以提升整体加载速度,于是我在网上找处理方法,
网上大多数是谷歌图片处理组件Thumbnails的介绍。最开始我用Thumbnails尝试,但不知道什么原因,没有任何效果,也不报错。
由于时间的关系,就没再深入研究,另寻他路。后来找到了下面的方法,这个方法优点在于完全基于Java自带API,调用也非常简单,如果只是缩放和压缩,这个方法足够了。

代码:

 /**
      * 缩放图片(压缩图片质量,改变图片尺寸)
      * 若原图宽度小于新宽度,则宽度不变!
      * @param newWidth 新的宽度
      * @param quality 图片质量参数 0.7f 相当于70%质量
      */
     public static void imageResize(File originalFile, File resizedFile,
                               int maxWidth,int maxHeight, float quality) throws IOException {
 
         if (quality > 1) {
             throw new IllegalArgumentException(
                     "图片质量需设置在0.1-1范围");
         }
 
         ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
         Image i = ii.getImage();
         Image resizedImage = null;
 
         int iWidth = i.getWidth(null);
         int iHeight = i.getHeight(null);
 
         int newWidth = maxWidth;
         if(iWidth < maxWidth){
             newWidth = iWidth;
         }
 
 
         if (iWidth >= iHeight) {
             resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
                     / iWidth, Image.SCALE_SMOOTH);
         }
 
 
 
         int newHeight = maxHeight;
         if(iHeight < maxHeight){
             newHeight = iHeight;
         }
 
         if(resizedImage==null && iHeight >= iWidth){
             resizedImage = i.getScaledInstance((newHeight * iWidth) / iHeight,
                     newHeight, Image.SCALE_SMOOTH);
         }
 
         // This code ensures that all the pixels in the image are loaded.
         Image temp = new ImageIcon(resizedImage).getImage();
 
         // Create the buffered image.
         BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
                 temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
 
         // Copy image to buffered image.
         Graphics g = bufferedImage.createGraphics();
 
         // Clear background and paint the image.
         g.setColor(Color.white);
         g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
         g.drawImage(temp, 0, 0, null);
         g.dispose();
 
         // Soften.
         float softenFactor = 0.05f;
         float[] softenArray = { 0, softenFactor, 0, softenFactor,
                 1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
         Kernel kernel = new Kernel(3, 3, softenArray);
         ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
         bufferedImage = cOp.filter(bufferedImage, null);

         // Write the jpeg to a file.
         FileOutputStream out = new FileOutputStream(resizedFile);
 
         // Encodes image as a JPEG data stream
         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
 
         JPEGEncodeParam param = encoder
                 .getDefaultJPEGEncodeParam(bufferedImage);
 
         param.setQuality(quality, true);
 
         encoder.setJPEGEncodeParam(param);
         encoder.encode(bufferedImage);
     } // Example usage

调用:

//图片压缩处理(缩放+质量压缩以减小高宽度及数据量大小)
	imageResize(imgFile,imgFile,1200,3000,0.9f);//宽度大于1200的,缩放为1200,高度大于3000的缩放为3000,按比例缩放,质量压缩掉10%
分享到:

朋友圈

新浪微博

QQ空间

豆瓣网

QQ好友