图片压缩-Java

话不多说,直接上代码(转载谁的忘记了。就不附链接了)

package com.luoy.util;

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:\\图片\\my1.png");
      byte[] b;
      try{
         b = compress(file, "G:\\1.jpg",700, 700);
         byteToFile(b, "G:\\", "my1.png");
         System.out.println(b);
      }catch(FileNotFoundException e){
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

   }
}

你可能感兴趣的:(压缩,图片)