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 压缩指定大小的文件
最新推荐文章于 2017-11-28 13:48:09 发布