如何从特定形状(例如圆形或正方形)的图像中获取字节数组数据?
假设我只想修改这个圆圈内的像素,我怎样才能得到这个 Bytearray 数据?
有任何想法吗?
如何从特定形状(例如圆形或正方形)的图像中获取字节数组数据?
假设我只想修改这个圆圈内的像素,我怎样才能得到这个 Bytearray 数据?
有任何想法吗?
定义一个包含圆的矩形,相对于图像的左上角。
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);
我实际上没有使用或测试过任何这些,所以它可能不起作用。如果您使用and
代替
,使用数据也可能更容易。getVector()
setVector()