美文网首页
17.3.27 图片压缩教程

17.3.27 图片压缩教程

作者: 薛定谔的猴子 | 来源:发表于2017-03-23 18:20 被阅读59次

有时候上传图片的时候,文件太大,等到要加载的时候,因为体积太大而加载速度变慢,同时也会占用服务器的空间,因此图片的压缩就很有必要了

以下这个工具类是公共类,可直接使用
链接下载

链接:http://pan.baidu.com/s/1geLNgCr 密码:hf9j
import java.awt.Image;  
import java.awt.image.BufferedImage;  
import java.io.File;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
  
import javax.imageio.ImageIO;  
  
import com.sun.image.codec.jpeg.JPEGCodec;  
import com.sun.image.codec.jpeg.JPEGEncodeParam;  
import com.sun.image.codec.jpeg.JPEGImageEncoder;  


public class Picture {
    /** 
     * 等比例压缩图片文件<br> 先保存原文件,再压缩、上传 
     * @param oldFile  要进行压缩的文件 
     * @param newFile  新文件 
     * @param width  宽度 //设置宽度时(高度传入0,等比例缩放) 
     * @param height 高度 //设置高度时(宽度传入0,等比例缩放) 
     * @param quality 质量 
     * @return 返回压缩后的文件的全路径 
     * @throws Exception 
     */  
    public static String zipImageFile(File oldFile,File newFile, int width, int height,  
            float quality) throws Exception {  
        
         if (oldFile == null) {  
             return null;  
         }  
         String newImage = null;  
         try {  
             /** 对服务器上的临时文件进行处理 */  
             Image srcFile = ImageIO.read(oldFile);  
             int w = srcFile.getWidth(null);  
         //  System.out.println(w);  
             int h = srcFile.getHeight(null);  
         //  System.out.println(h);  
   
             /** 宽,高设定 */  
             BufferedImage tag = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);  
             tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);  
             //String filePrex = oldFile.substring(0, oldFile.indexOf('.'));  
             /** 压缩后的文件名 */  
             //newImage = filePrex + smallIcon+ oldFile.substring(filePrex.length());  
   
             /** 压缩之后临时存放位置 */  
             FileOutputStream out = new FileOutputStream(newFile);  
   
             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);  
             JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);  
             /** 压缩质量 */  
             jep.setQuality(quality, true);  
             encoder.encode(tag, jep);  
             out.close();  
         } catch (FileNotFoundException e) {  
             e.printStackTrace();  
         } catch (IOException e) {  
             e.printStackTrace();  
         }  
         return newImage;  
    
    }  
    public static void main(String[] args) throws Exception {
         // System.out.println(Picture.zipImageFile(new File("E:/a.jpg"),new File("E:/a2.jpg"),250, 250, 50f));  
    }

}

压缩前后对比

压缩前
压缩后

相关文章

  • 17.3.27 图片压缩教程

    有时候上传图片的时候,文件太大,等到要加载的时候,因为体积太大而加载速度变慢,同时也会占用服务器的空间,因此图片的...

  • 网址

    苹果官方swift社区 git教程 sketch中文网 Tiny:图片压缩

  • 图片压缩组件

    图片压缩 图片压缩

  • iOS 图片压缩方法

    两种图片压缩方法 两种图片压缩方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 通过...

  • SpringBoot项目中Fastdfs配置及使用2

    相关链接:FastDFS安装教程1(安装、配置、部署、防盗链、nginx、图片压缩)[https://www.ji...

  • iOS 图片压缩限制大小最优解

    iOS 图片压缩限制大小最优解 图片的两种压缩方法 1.1 压缩图片质量 1.2 压缩图片尺寸 压缩图片使图片文件...

  • iOS 图片压缩限制大小最优解

    概要: 图片的两种压缩方法1.1 压缩图片质量1.2 压缩图片尺寸压缩图片使图片文件小于指定大小2.1 压缩图片质...

  • iOS 图片压缩限制大小

    一、两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量...

  • iOS 图片压缩方法

    两种图片压缩方法 两种压缩图片的方法:压缩图片质量(Quality),压缩图片尺寸(Size)。 压缩图片质量 N...

  • 17.3.27

    书山无路勤为径,学海无涯苦作舟。爱拼才会赢。付出才存在收获,加油,不要浪费大好时光,不要让自己悔断衷肠。

网友评论

      本文标题:17.3.27 图片压缩教程

      本文链接:https://www.haomeiwen.com/subject/uaetottx.html