1。使用BitmapFactory.Options裁圖,指定最大寬和最大高。
public static Bitmap getBitmapFromMedia(Context context, String pathName, int maxWidth, int maxHeight) { Bitmap bitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); try { options.inJustDecodeBounds = true; BitmapFactory.decodeFile(pathName, options); options.inJustDecodeBounds = false; int outputWidth = options.outWidth; int outputHeight = options.outHeight; if (outputWidth < maxWidth && outputHeight < maxHeight) { bitmap = BitmapFactory.decodeFile(pathName); } else { int inSampleSize = 0; int widthSmapleSize = (int) (outputWidth / maxWidth); int heightSmapleSize = (int) (outputHeight / maxHeight); MCLogUtil.i("ImageUtil", "outputWidth = " + outputWidth + " widthSmapleSize = " + widthSmapleSize); MCLogUtil.i("ImageUtil", "outputHeight = " + outputHeight + " heightSmapleSize = " + heightSmapleSize); if (widthSmapleSize >= heightSmapleSize) { inSampleSize = widthSmapleSize; } else { inSampleSize = heightSmapleSize; } if (inSampleSize < 3) options.inSampleSize = 2; else if (inSampleSize < 5) options.inSampleSize = 4; else if (inSampleSize < 8) options.inSampleSize = 8; else options.inSampleSize = inSampleSize; bitmap = BitmapFactory.decodeFile(pathName, options); } } catch (OutOfMemoryError oom) { System.gc(); return null; } catch (Exception e) { return null; } return bitmap; }
inSampleSize的官方解釋:For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. 解釋起來應該是:當inSampleSize爲4時,一個2000*1000的圖片,將被縮小爲500*250,相應地,它的像素數和內存佔用都被縮小爲了原來的1/16,小於1的當1處理。
2。使用Matrix裁圖。按短邊裁圖。
public static Bitmap resizeAccordShortestEdgeBitmap(Bitmap bitmap, int upperLimitSize) throws Exception { if (bitmap == null) { return null; } int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleWidth = 0f; float scaleHeight = 0f; if (width <= height) { float proportion = ((float) height) / ((float) width); int newHeight = (int) ((upperLimitSize) * proportion); scaleWidth = ((float) upperLimitSize) / width; scaleHeight = ((float) newHeight) / height; } else { float proportion = ((float) width) / ((float) height); int newWidth = (int) ((upperLimitSize) * proportion); scaleWidth = ((float) newWidth) / width; scaleHeight = ((float) upperLimitSize) / height; } Matrix matrix = new Matrix(); // resize the bit map matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); return resizedBitmap; }
3。質量壓縮。使用官方壓縮方式。
public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)
format | The format of the compressed image |
---|---|
quality | Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting |
stream | The outputstream to write the compressed data. |
例子:
boolean isCompressed = sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
PNG是無損的,將忽略質量設置。