Java tutorial
//package com.java2s; public class Main { public static double calScaleRatio(int originalWidth, int originalHeight, int targetWidth, int targetHeight) { if (originalWidth == 0 || originalHeight == 0) { return 1; } else { double ratio = Math.sqrt((targetWidth * targetHeight * 1.0) / (originalWidth * originalHeight)); if (ratio > 1) { return 1; } else { return ratio; } } } }