我认为以下内容应该可以给您这个想法。它不是任何特定的语言,而是类似C的伪代码。
shortSideMax = 640;longSideMax = 960;function Resize(image){ if (image.width >= image.height) { if (image.width <= longSideMax && image.height <= shortSideMax) return image; // no resizing required wRatio = longSideMax / image.width; hRatio = shortSideMax / image.height; } else { if (image.height <= longSideMax && image.width <= shortSideMax) return image; // no resizing required wRatio = shortSideMax / image.width; hRatio = longSideMax / image.height; } // hRatio and wRatio now have the scaling factors for height and width. // You want the smallest of the two to ensure that the resulting image // fits in the desired frame and maintains the aspect ratio. resizeRatio = Min(wRatio, hRatio); newHeight = image.Height * resizeRatio; newWidth = image.Width * resizeRatio; // Now call function to resize original image to [newWidth, newHeight] // and return the result.}
这段代码的效率或您拥有的代码都不会成为问题。实际调整图像大小所需的时间将使进行两次比较,两次除法和两次乘法所需的时间相形见war。
这是“更数学”的方法吗?我想是因为它会将您的四个案例分解为两个。但是方法本质上是相同的。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)