BitMap 压缩指定大小的文件

private Bitmap compressImage(Bitmap tempBitmap) {

   final int IMAGE_SIZE = 255;//指定文件大小
   boolean landscape = bitmap.getWidth() > bitmap.getHeight();

   float scale_factor;
   if (landscape) scale_factor = (float)IMAGE_SIZE / bitmap.getHeight();
   else scale_factor = (float)IMAGE_SIZE / bitmap.getWidth();
   Matrix matrix = new Matrix();
   matrix.postScale(scale_factor, scale_factor);

   Bitmap croppedBitmap;
   if (landscape){
      int start = (tempBitmap.getWidth() - tempBitmap.getHeight()) / 2;
      croppedBitmap = Bitmap.createBitmap(tempBitmap, start, 0, tempBitmap.getHeight(), tempBitmap.getHeight(), matrix, true);
   } else {
      int start = (tempBitmap.getHeight() - tempBitmap.getWidth()) / 2;
      croppedBitmap = Bitmap.createBitmap(tempBitmap, 0, start, tempBitmap.getWidth(), tempBitmap.getWidth(), matrix, true);
   }
   return croppedBitmap;
}

你可能感兴趣的:(BitMap 压缩指定大小的文件)