Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.BitmapFactory; public class Main { public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { //https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/RequestHandler.java#L161 if (reqHeight == 0) { inSampleSize = (int) Math.floor((float) width / (float) reqWidth); } else if (reqWidth == 0) { inSampleSize = (int) Math.floor((float) height / (float) reqHeight); } else { int heightRatio = (int) Math.floor((float) height / (float) reqHeight); int widthRatio = (int) Math.floor((float) width / (float) reqWidth); inSampleSize = Math.max(heightRatio, widthRatio); } } return inSampleSize; } }