2012-07-14 22 views
4

我試圖將BitmapData的內容繪製到另一個,但尚未創建。
但在繪製之前,我需要縮放和旋轉圖像,並繪製它。
我的問題是,我不知道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; 
} 

有些人知道改造後我應該做些什麼來找出(計算),這個圖像的大小?


此圖片顯示在行動旋轉,我想找出什麼:

enter image description here

+0

添加屏幕截圖以瞭解所需的旋轉剪切。 – 2012-07-14 05:45:15

+0

@JasonSturges,圖片添加。這個概念很簡單,但我找不到編碼的方法。 – NemoStein 2012-07-14 06:29:44

+0

爲什麼你需要更快的東西?帶有矩陣的bmp.draw()將是該函數計算所需的99.999999% - 即,即使速度更快,速度也會更快。是的,你可以在不使用矩陣函數的情況下完成所有的矩陣運算,這會給你速度,但最好的情況是我認爲你將能夠削減.0001毫秒左右。 – ansiart 2012-07-14 22:54:48

回答

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; 
} 

我想,這可能是有點矯枉過正,但工程。

+0

「var width:Number = bottom - top;」 - 錯字?也許它應該是 - 「var width:Number = right - left;」。同樣的高度? – jayarjo 2013-03-11 18:43:26

+0

我認爲你是對的...編輯。 – NemoStein 2013-03-12 21:03:47

0

沒有必要來計算新的位圖大小。您可以創建與原始大小相同的新bitmapData,並將transparency設置爲true,將fillColor參數設置爲0x00(這是32位圖像上的Alpha值)。這樣新的bitmapData將只填充轉換後的對象,其餘的圖像數據結構不會受到影響。

或者還有另一種方法,通過將原始位圖數據widthheight乘以縮放比來定義佔位符位圖數據大小。

+0

在我的例子中,我設置了透明度和fillColor('new BitmapData(width,height,true,0);'),但這不是問題的一部分。我需要計算大小,因爲我將繪製的圖像比原始圖像更大(注意'matrix.scale(1.5,1.5);'),即使我沒有縮放它,在儘管如此,我仍然會有更大的形象。 – NemoStein 2012-07-14 05:40:33

+0

,這是downvote的原因嗎? – 2012-07-14 06:49:03

+0

也許,但downvote不是我的... – NemoStein 2012-07-14 06:53:51

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的偏移量。

+0

這並沒有爲我工作。減去點給我一個零寬度的矩形。我做錯什麼了嗎? – NemoStein 2012-07-14 06:32:55

+0

更新!我設法使用transformPoint(Point)來找出尺寸。 – NemoStein 2012-07-14 15:35:36