4

我正在尝试将 BitmapData 的内容绘制到另一个尚未创建的内容中。
但在绘制之前,我需要缩放和旋转图像,然后绘制它。
我的问题是我不知道转换后 BitmapData 的大小,所以我无法创建新的来绘制它。

这个方法说明了我的意思:

public function getTransformedBitmapData(origin:BitmapData):BitmapData
{
    var matrix:Matrix = new Matrix();

    // ajusting the anchor point and rotating
    matrix.translate(-origin.width / 2, -origin.height / 2);
    matrix.rotate(Math.PI / 4); // 45 deg
    matrix.translate(origin.width / 2, origin.height / 2);

    // scaling
    matrix.scale(1.5, 1.5);

    // Calculating the size of the new BitmapData
    var width:Number = 0; // I don't know this value!
    var height:Number = 0; // I don't know this value!

    // Creating and drawing (with transformation)
    var result:BitmapData = new BitmapData(width, height, true, 0);
    result.draw(origin, matrix);

    return result;
}

有人知道我应该怎么做才能在转换后找出(计算)这个图像的大小?


这张图片说明了旋转的作用,以及我想知道的内容:

在此处输入图像描述

4

3 回答 3

1

好的,使用@ansiart 答案作为起点,我设法以这种方式计算尺寸:

public function getTransformedBitmapData(origin:BitmapData):BitmapData
{
    var matrix:Matrix = new Matrix();

    // ajusting the anchor point and rotating
    matrix.translate(-origin.width / 2, -origin.height / 2);
    matrix.rotate(Math.PI / 4); // 45 deg
    matrix.translate(origin.width / 2, origin.height / 2);

    // scaling
    matrix.scale(1.5, 1.5);

    // Finding the four corners of the bounfing box after transformation
    var topLeft:Point = matrix.transformPoint(new Point(0, 0));
    var topRight:Point = matrix.transformPoint(new Point(origin.width, 0));
    var bottomLeft:Point = matrix.transformPoint(new Point(0, origin.height));
    var bottomRight:Point = matrix.transformPoint(new Point(origin.width, origin.height));

    // Calculating "who" is "where"
    var top:Number = Math.min(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
    var bottom:Number = Math.max(topLeft.y, topRight.y, bottomLeft.y, bottomRight.y);
    var left:Number = Math.min(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);
    var right:Number = Math.max(topLeft.x, topRight.x, bottomLeft.x, bottomRight.x);

    // Ajusting final position
    matrix.translate(-left, -top);

    // Calculating the size of the new BitmapData
    var width:Number = right - left;
    var height:Number = bottom - top;

    // Creating and drawing (with transformation)
    var result:BitmapData = new BitmapData(width, height, false, 0);
    result.draw(origin, matrix);

    return result;
}

我认为这可能有点矫枉过正,但有效。

于 2012-07-15T05:27:21.440 回答
0

无需计算新的位图大小。您可以创建与原始大小相同的新 bitmapData,并将其设置transparency为 true,并将fillColor参数设置为 0x00(这是 32 位图像上的 alpha 值)。这样,新的bitmapData将只填充转换后的对象,图像数据结构的其余部分不会受到影响。

或者还有另一种方法,通过将原始 bitmapData 与缩放比例相乘来定义占位符位图数据width大小height

于 2012-07-14T05:35:51.640 回答
0

矩阵有一个 transformPoint 方法。这将使确定 topLeft/bottomRight 变得容易。

var matrix:Matrix = new Matrix();

// do stuff to matrix
var topLeft:Point   = matrix.transformPoint(new Point(0,0));
var bottomRight:Point   = matrix.transformPoint(new Point(width,height));

// determine bounds of rectangle based on points (note: bottomRight might be now the topLeft, so need to do testing

但是,如果您要绘制到这个新位图中,请确保根据 tx/ty 进行偏移。

于 2012-07-14T05:45:18.050 回答