content: package com.handsome.bitmapcompress;
  
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.graphics.Canvas;
  import android.graphics.Rect;
  
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  
  /**
   * =====作者=====
   * 许英俊
   * =====时间=====
   * 2017/12/23.
   */
  
  public class CompressUtils {
  
      static {
          System.loadLibrary("native-lib");
      }
  
      /**
       * LibJpeg压缩
       *
       * @param bitmap
       * @param quality
       * @param fileNameBytes
       * @param optimize
       * @return
       */
      public static native int compressBitmap(Bitmap bitmap, int quality, byte[] fileNameBytes,
                                              boolean optimize);
  
  
      /**
       * 质量压缩
       *
       * @param bitmap
       * @param quality
       * @param file
       */
      public static void compressQuality(Bitmap bitmap, int quality, File file) {
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
          try {
              FileOutputStream fos = new FileOutputStream(file);
              fos.write(baos.toByteArray());
              fos.flush();
              fos.close();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      /**
       * 尺寸压缩
       *
       * @param bitmap
       * @param file
       */
      public static void compressSize(Bitmap bitmap, File file) {
          int ratio = 8;//尺寸压缩比例
          Bitmap result = Bitmap.createBitmap(bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, Bitmap.Config.ARGB_8888);
          Canvas canvas = new Canvas(result);
          Rect rect = new Rect(0, 0, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio);
          canvas.drawBitmap(bitmap, null, rect, null);
  
          compressQuality(result, 100, file);
      }
  
      /**
       * 采样率压缩
       *
       * @param filePath
       * @param file
       */
      public static void compressSample(String filePath, File file) {
          int inSampleSize = 8;//采样率设置
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = false;
          options.inSampleSize = inSampleSize;
          Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
  
          compressQuality(bitmap, 100, file);
      }
  }
  