Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; public class Main { public static Bitmap createScaledRotatedBitmapFromFile(String filePath, int reqWidth, int reqHeight, int orientation) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); Bitmap bitmap; int rotationAngle = 0, outHeight = 0, outWidth = 0; if (reqWidth >= reqHeight) { if (options.outWidth >= options.outHeight) { if (orientation != 1) { rotationAngle = 180; } else { } bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight); } else { bitmap = decodeSampledBitmapFromFile(filePath, reqHeight, reqWidth); } outHeight = bitmap.getWidth(); outWidth = bitmap.getHeight(); } else { if (options.outWidth > options.outHeight) { if (orientation != 1) { rotationAngle = 90; } else { rotationAngle = 270; } bitmap = decodeSampledBitmapFromFile(filePath, reqHeight, reqWidth); } else { bitmap = decodeSampledBitmapFromFile(filePath, reqWidth, reqHeight); } outHeight = bitmap.getWidth(); outWidth = bitmap.getHeight(); } if (rotationAngle == 0) { return bitmap; } Matrix matrix = new Matrix(); matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2); Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outHeight, outWidth, matrix, true); if (bitmap != rotateBitmap) { bitmap.recycle(); } return rotateBitmap; } public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } }