Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.Matrix; import android.graphics.RectF; import android.media.ExifInterface; public class Main { private static final int ORIENTATION_90 = 90; private static final int ORIENTATION_180 = 180; private static final int ORIENTATION_270 = 270; /** * Scale a bitmap and correct the dimensions * * @param bitmap Bitmap to scale * @param width width for scaling * @param height height for scaling * @param orientation Current orientation of the Image * @return Scaled bitmap */ public static Bitmap getScaledBitmap(Bitmap bitmap, int width, int height, int orientation) { Matrix m = new Matrix(); m.setRectToRect(new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight()), new RectF(0, 0, width, height), Matrix.ScaleToFit.CENTER); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { m.postRotate(ORIENTATION_90); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { m.postRotate(ORIENTATION_180); } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { m.postRotate(ORIENTATION_270); } return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); } }