2

这里的问题是我有一个大小为 x x y 的显示窗口,我需要在窗口内显示图像而不进行任何滚动,并保持 4:3 的纵横比。我有以下代码片段:

// Lock the current height, calculate new width of the canvas and scale the viewport.
// get width of the movie canvas
qreal width = canvas_->width();

// Find the height of the video
qreal height = (width/4.0) * 3;

// find original width and height for video to calculate scaling factor
qreal videoWidth = movieData_->GetWidth();
qreal videoHeight = movieData_->GetHeight();

// calculate scaling factor
qreal scaleFactorWidth = width/videoWidth;
qreal scaleFactorHeight = height/videoHeight;

当然,通过使用高度或宽度作为“锚点”,新图像将导致滚动(假设原始图像首先大于窗口)。如何找到适合预定尺寸的宽高比 4:3 的尺寸?

编辑 我需要传入 x 和 y 的比例因子来进行缩放

canvas_->scale(scaleFactorWidth, scaleFactorHeight);
4

4 回答 4

5

只需取两个计算值中的最小值:

scale = min(scaleFactorWidth, scaleFactorHeight)

或(如果你想要外装)

scale = max(scaleFactorWidth, scaleFactorHeight)
于 2009-09-02T09:52:17.767 回答
2
    struct dimensions resize_to_fit_in(struct dimensions a, struct dimensions b) {
        double wf, hf, f;
        struct dimensions out;

        wf = (double) b.w / a.w;
        hf = (double) b.h / a.h;

        if (wf > hf)
                f = hf;
        else
                f = wf;

        out.w = a.w * f;
        out.h = a.h * f;

        return out;
}

这里是一个 C 版本,其中返回的尺寸将是尺寸“a”,适合尺寸“b”,而不会丢失纵横比。

于 2009-09-02T10:14:57.127 回答
1

w找到宽度和高度这两个值中的最大值h。假设您的最大宽度x高度为 100 x80。请注意,100/80 = 1.25。

情况 1:如果w/h > 1.25,则除以w100 得到原始尺寸与新尺寸的比率。然后乘以h这个比率。

情况 2:否则,然后除以h80 以获得原始尺寸与新尺寸的比率。然后乘以w这个比率。

于 2009-09-02T09:55:07.917 回答
1

这是您所要求的 ActionScript 版本(在保持纵横比的同时调整大小)......应该不难移植到任何内容:

    private static function resizeTo(dispObj:DisplayObject, width:Number, height:Number) : void
    {
        var ar:Number = width / height;
        var dispObjAr:Number = dispObj.width/dispObj.height;
        if (ar < dispObjAr)
        {
            dispObj.width = width;
            dispObj.height = width / dispObjAr;
        }
        else
        {
            dispObj.height = height;
            dispObj.width = height * dispObjAr;
        }
        return;
    }

编辑:为了保持 4:3,源图像需要是 4:3

于 2009-09-02T10:01:55.517 回答