Java 图片压缩

图片压缩

因为做APP ,APP 上面传来的图片很大 有好几M 的图片,如果不进行压缩处理,后面再回传到APP里面,那么久非常的耗流量。所以百度查了一个图片压缩的算法来,自己整理了一下。话不多说上代码

package com.qx.client.common.utils;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import com.sun.image.codec.jpeg.*;

/**
 * 图片压缩处理
 * @author 崔素强
 */
public class ImageCompressionUtil{


   public static byte[] compress(File file,
                                 String tempPath,
                                 int w,
                                 int h) throws FileNotFoundException{

      return compress(new FileInputStream(file), tempPath, w, h);
   }

   /**
    * 
    * @param file
    * @param tempPath
    * @param w
    * @param h
    * @return
    */
   public static byte[] compress(InputStream in,
                                 String tempPath,
                                 int w,
                                 int h){
      try{
         Image img = ImageIO.read(in);
         int width = img.getWidth(null); // 得到源图宽
         int height = img.getHeight(null); // 得到源图长
         if(width / height > w / h){
            h = (int) (height * w / width);
         }else{
            w = (int) (width * h / height);
         }
         return resize(img, tempPath, w, h);
      }catch(IOException e){
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      return null;
   }

   /**
    * 强制压缩/放大图片到固定的大小
    * @param tempPath 临时目录
    * @param w int 新宽度
    * @param h int 新高度
    */
   private static byte[] resize(Image img,
                               String tempPath,
                               int w,
                               int h)
         throws IOException{
      // SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的 优先级比速度高 生成的图片质量比较好 但速度慢
      BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
      image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
      File destFile = new File(tempPath);
      FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
      // 可以正常实现bmp、png、gif转jpg
      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
      encoder.encode(image); // JPEG编码
      out.close();
      byte[] b = fileToByte(destFile);
      destFile.deleteOnExit();
      return b;
   }

   public static byte[] fileToByte(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(IOException e){
         e.printStackTrace();
      }
      return buffer;
   }

   public static File byteToFile(byte[] buf,
                                 String filePath,
                                 String fileName){
      BufferedOutputStream bos = null;
      FileOutputStream fos = null;
      File file = null;
      try{
         File dir = new File(filePath);
         if(!dir.exists() && dir.isDirectory()){
            dir.mkdirs();
         }
         file = new File(filePath + File.separator + fileName);
         fos = new FileOutputStream(file);
         bos = new BufferedOutputStream(fos);
         bos.write(buf);
      }catch(Exception e){
         e.printStackTrace();
      }finally{
         if(bos != null){
            try{
               bos.close();
            }catch(IOException e){
               e.printStackTrace();
            }
         }
         if(fos != null){
            try{
               fos.close();
            }catch(IOException e){
               e.printStackTrace();
            }
         }
      }
      return file;
   }

   /*public static void main(String[] args){
      File file = new File("G:\\图片\\73.jpg");
      byte[] b = compress(file, "G:\\1.jpg",400, 400);
      byteToFile(b, "G:\\", "my1.png");
      System.out.println(b);
   }*/
}

网上有很多压缩的算法,自己找了好多个,最后还是觉得这个是简单易懂的一个。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值