首页 文章

如何根据图像尺寸计算纵横比

提问于
浏览
3

我使用Jcrop裁剪图像,所以我想计算图像的高度和宽度的比例,但问题是没有最大高度和宽度的限制 .

当用户上传图像然后我想获得高度,宽度比等所以裁剪它应该是关于纵横比的裁剪例如

宽度= 835,高度= 625纵横比为167:125

我从以下链接计算了这个比例Aspect ratio calculator

我不想要新的高度,宽度 . 我只想计算比例167:125

我怎样才能做到这一点?

1 回答

  • 5

    我认为你正在寻找HCF(最高公因数),但比例(宽度:835,身高:625)将是167:125 . 这是您可以计算2个数字之间的HCF的函数 .

    private int FindHCF(int m, int n)
     {
         int temp, remainder;
         if (m < n)
         {
             temp = m;
             m = n;
             n = temp;
         }
         while (true)
         {
             remainder = m % n;
             if (remainder == 0)
                 return n;
             else
                 m = n;
             n = remainder;
         }
     }
    

    所以这是代码的其余部分

    int hcf = FindHcf(835, 625);
    int factorW = 835 / hcf;
    int factorH = 625 / hcf;
    

相关问题