请告诉我的算法来计算宽高比?我需要等的输出:4:3,16:9 [英] Whats the algorithm to calculate aspect ratio? I need an output like: 4:3, 16:9

查看:1285
本文介绍了请告诉我的算法来计算宽高比?我需要等的输出:4:3,16:9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用它使用JavaScript来裁剪图像,以适应整个窗口。

I plan to use it with javascript to crop an image to fit the entire window.

我AP preciate所有帮助你可以提供。

I appreciate all help you can provide.

编辑:我会使用第三部分成分只接受在像格式的纵横比:4:3,16:9

I'll be using a 3rd part component that only accepts the aspect ratio in the format like: 4:3, 16:9

推荐答案

据我了解,你正在寻找一个可用的纵横比整数:整数类似的解决方案 16:9 ,而不是浮动:1 1.77778:1 。

I gather you're looking for an usable aspect ratio integer:integer solution like 16:9 rather than a float:1 solution like 1.77778:1.

如果是这样,你需要做的是找到最大公约数(GCD),并通过该分两个值。 GCD的最高数整除两个号码。所以和GCD 6图10是图2中,GCD为44和99 11

If so, what you need to do is find the greatest common divisor (GCD) and divide both values by that. The GCD is the highest number that evenly divides both numbers. So the GCD for 6 and 10 is 2, the GCD for 44 and 99 is 11.

例如,一个1024x768的显示器有256个GCD当由分两个值你获得4×3或4:3

For example, a 1024x768 monitor has a GCD of 256. When you divide both values by that you get 4x3 or 4:3.

A(递归)GCD算法:

A (recursive) GCD algorithm:

function gcd (a,b):
    if b = 0:
        return a
    return gcd (b, a mod b)

在C:

static int gcd (int a, int b) {
    return (b == 0) ? a : gcd (b, a%b);
}

int main(void) {
    printf ("gcd(1024,768) = %d\n",gcd(1024,768));
}

和这里的一些完整的HTML / Javascript的,显示检测到屏幕大小,并计算出宽高比的一种方式。这在FF3,我不能确定是什么支持其他浏览器对 screen.width screen.height

And here's some complete HTML/Javascript which shows one way to detect the screen size and calculate the aspect ratio from that. This works in FF3, I'm unsure what support other browsers have for screen.width and screen.height.

<html><body>
    <script type="text/javascript">
        function gcd (a, b) {
            return (b == 0) ? a : gcd (b, a%b);
        }
        var w = screen.width;
        var h = screen.height;
        var r = gcd (w, h);
        document.write ("<pre>");
        document.write ("Dimensions = ", w, " x ", h, "<br>");
        document.write ("Gcd        = ", r, "<br>");
        document.write ("Aspect     = ", w/r, ":", h/r);
        document.write ("</pre>");
    </script>
</body></html>

它输出(我奇怪的宽屏幕显示器上):

It outputs (on my weird wide-screen monitor):

Dimensions = 1680 x 1050
Gcd        = 210
Aspect     = 8:5

别人认为我测试的:

Others that I tested this on:

Dimensions = 1280 x 1024
Gcd        = 256
Aspect     = 5:4

Dimensions = 1152 x 960
Gcd        = 192
Aspect     = 6:5

Dimensions = 1280 x 960
Gcd        = 320
Aspect     = 4:3

Dimensions = 1920 x 1080
Gcd        = 120
Aspect     = 16:9

我希望我有最后一个主场,但是,没有,这是一个工作的机器很遗憾。

I wish I had that last one at home but, no, it's a work machine unfortunately.

如果你发现了纵横比你做的是不支持的图形调整大小的工具则是另一回事。我怀疑是最好的选择会有补充信拳击线(就像你在你的旧电视机的顶部和底部得到,当你观看宽屏幕电影就可以了的)。我想补充他们在顶部/底部或侧面(哪一个结果最少的字母拳击线),直到图像符合要求。

What you do if you find out the aspect ratio is not supported by your graphic resize tool is another matter. I suspect the best bet there would be to add letter-boxing lines (like the ones you get at the top and bottom of your old TV when you're watching a wide-screen movie on it). I'd add them at the top/bottom or the sides (whichever one results in the least number of letter-boxing lines) until the image meets the requirements.

您可能要考虑的一件事是画面的质量一直在变,从16:9到5:4 - 我还记得令人难以置信的高高瘦瘦的牛仔我用信拳击之前,我的青春在电视上观看进行了介绍。你可能会更好每个具有纵横比1不同的图像,并传送下来的电线之前刚刚调整正确的实际屏幕尺寸。

One thing you may want to consider is the quality of a picture that's been changed from 16:9 to 5:4 - I still remember the incredibly tall, thin cowboys I used to watch in my youth on television before letter-boxing was introduced. You may be better off having one different image per aspect ratio and just resize the correct one for the actual screen dimensions before sending it down the wire.

这篇关于请告诉我的算法来计算宽高比?我需要等的输出:4:3,16:9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆