List of utility methods to do Bitmap Resize
Bitmap | getResizedBitmap(Bitmap bm, int newHeight, int newWidth) get Resized Bitmap final int w = bm.getWidth(); final int h = bm.getHeight(); final float sw = ((float) newWidth) / w; final float sh = ((float) newHeight) / h; Matrix matrix = new Matrix(); matrix.postScale(sw, sh); return Bitmap.createBitmap(bm, 0, 0, w, h, matrix, false); |
Bitmap | getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio) Downsize the bitmap to at most the indicated ratio of the max specified size if (null == bm) { return bm; int height = bm.getHeight(); int width = bm.getWidth(); if (0 == height || 0 == width) { return bm; double ratio = height / width; int newHeight = (int) (Math.min(maxX, maxY) * maxRatio); int newWidth = (int) (newHeight / ratio); if (newHeight >= height && newWidth >= width) { return bm; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); try { Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); return resizedBitmap; } catch (Exception e) { return bm; |
Bitmap | getSafeResizingBitmap(String strImagePath, int nMaxResizedWidth, int nMaxResizedHeight, boolean checkOrientation) Get the image bitmap that resizing to maximum size of limit. BitmapFactory.Options options = getBitmapSize(strImagePath); if (options == null) return null; int nSampleSize; int degree = 0; if (checkOrientation) { degree = getExifDegree(strImagePath); if (degree == 90 || degree == 270) { nSampleSize = getSafeResizingSampleSize(options.outHeight, options.outWidth, nMaxResizedWidth, nMaxResizedHeight); } else { nSampleSize = getSafeResizingSampleSize(options.outWidth, options.outHeight, nMaxResizedWidth, nMaxResizedHeight); options.inJustDecodeBounds = false; options.inSampleSize = nSampleSize; options.inDither = false; options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inPurgeable = true; Bitmap photo = BitmapFactory.decodeFile(strImagePath, options); if (checkOrientation && (degree == 90 || degree == 270)) { return getRotatedBitmap(photo, degree); return photo; |