Java tutorial
//package com.java2s; //License from project: LGPL import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.InputStream; public class Main { public static Bitmap scaleToFillBitmap(Bitmap dst, InputStream is) { Bitmap src = BitmapFactory.decodeStream(is); float scaled = 1.0f; if ((float) dst.getWidth() / (float) src.getWidth() < (float) dst.getHeight() / (float) src.getHeight()) { scaled = (float) dst.getHeight() / (float) src.getHeight(); } else { scaled = (float) dst.getWidth() / (float) src.getWidth(); } Bitmap bmpScaled = Bitmap.createScaledBitmap(src, (int) Math.ceil(src.getWidth() * scaled), (int) Math.ceil(src.getHeight() * scaled), true); int offsetX = 0; int offsetY = 0; offsetX = bmpScaled.getWidth() - dst.getWidth() != 0 ? (bmpScaled.getWidth() - dst.getWidth()) / 2 : 0; offsetY = bmpScaled.getHeight() - dst.getHeight() != 0 ? (bmpScaled.getHeight() - dst.getHeight()) / 2 : 0; return Bitmap.createBitmap(bmpScaled, offsetX, offsetY, dst.getWidth(), dst.getHeight()); } public static Bitmap scaleToFillBitmap(Bitmap dst, byte[] byImage) { Bitmap src = BitmapFactory.decodeByteArray(byImage, 0, byImage.length); float scaled = 1.0f; if ((float) dst.getWidth() / (float) src.getWidth() < (float) dst.getHeight() / (float) src.getHeight()) { scaled = (float) dst.getHeight() / (float) src.getHeight(); } else { scaled = (float) dst.getWidth() / (float) src.getWidth(); } Bitmap bmpScaled = Bitmap.createScaledBitmap(src, (int) Math.ceil(src.getWidth() * scaled), (int) Math.ceil(src.getHeight() * scaled), true); int offsetX = 0; int offsetY = 0; offsetX = bmpScaled.getWidth() - dst.getWidth() != 0 ? (bmpScaled.getWidth() - dst.getWidth()) / 2 : 0; offsetY = bmpScaled.getHeight() - dst.getHeight() != 0 ? (bmpScaled.getHeight() - dst.getHeight()) / 2 : 0; return Bitmap.createBitmap(bmpScaled, offsetX, offsetY, dst.getWidth(), dst.getHeight()); } }