2010-10-19 92 views
1

如何從某個形狀的圖像(例如電路或方形圖像)獲取字節數據?As3和Image ByteArray數據

假設我只想修改這個電路內的像素,我怎麼能得到這個Bytearray數據? alt text 任何想法?

回答

2

定義一個包含相對於圖像左上角的圓的矩形。

var radius:Number = 100; 
var centerX:Number = 50; 
var centerY:NUmber = 400; 

var rect:Rectangle = new Rectangle(centerX-radius, centerY-radius, radius*2, radius*2); 

然後使用getPixels()返回矩形內的像素的一個ByteArray。 現在,您可以遍歷每個像素並檢查它是否包含在圓圈內。

var image:BitmapData; 
var pixels:ByteArray = image.getPixels(rect); 

for(var x:int; x<rect.width; x++){ 
    for(var y:int=0; y<rect.height; y++){ 
     // Read the pixels data -> 
     var pixel:uint = pixels.readUnsignedInt(); 
     // Check this pixels distance from the center to make sure it is inside the circle. 
     var dx:Number = x - radius; 
     var dy:Number = y - radius; 
     if(dx*dx+dy*dy <= radius*radius){ 
      // This pixel is inside the circle. 
      ... 
     } 
    } 
} 

然後,一旦你已經修改了它,如果你願意,你可以使用setPixels()

image.setPixels(rect, pixels); 

我還沒有實際使用或測試任何這所以可能無法正常工作寫回圖像。
如果您改爲使用getVector()setVector() ,則處理數據也可能更容易。