我正在寻找一种有效的方法来从ActionScript 3中的BitMapData对象过滤特定颜色。目前,我使用readByTe32()的循环。这大约需要一秒钟的过程,这是不可接受的。我一直在尝试使Palettemap()工作,但到目前为止尚未掌握其API(任何真正有用的链接?Google都使我失望了...)。

这是我目前的逻辑,我想改进:

var n:int = bitmapData.width;
for (var i:int = 0; i < n; i++) {
 var m:int = bitmapData.height;
 for (var j:int = 0; j < m; j++) {
  var color:int = bitmapData.getPixel(i, j);
  if (color == 0xCACACA) {
   bitmapData.setPixel32(i, j, 0x00000000);
  }
 }
}

我可以通过使用向量获得更好的性能,但这只是更好的...

var v:Vector.<uint> = bitmapData.getVector(bitmapData.rect);
var n:int = bitmapData.width * bitmapData.height;
for (var i:int = 0; i < n; i++) {
 var color:uint = v[i];
 v[i] = color == 0xFFCACACA ? 0x00000000 : color;
}
bitmapData.setVector(bitmapData.rect, v);

我真的认为必须有一种更好的方法来做到这一点,只需花费几毫秒100毫秒。如果有人可以为我解锁Bitmapdata的奥秘,那么您将成为我人民的新领导者。

ps我正在使用bitmapdata.lock()和unlock();我只是没有张贴样板的东西。

有帮助吗?

解决方案

一种简单的方法是使用 临界点 方法。一开始有点麻烦,但是很快(我认为您会得到的很快)

这将将每个红色像素(仅考虑一个值为RED符合0xffff0000的像素)为蓝色(0xff0000ff)。

var colorToReplace:uint = 0xffff0000;
var newColor:uint = 0xff0000ff;
var maskToUse:uint = 0xffffffff;

var rect:Rectangle = new Rectangle(0,0,bitmapData.width,bitmapData.height);
var p:Point = new Point(0,0);
bitmapData.threshold(bitmapData, rect, p, "==", colorToReplace, 
        newColor, maskToUse, true);

其他提示

Flash具有像名为“语言”的着色器的API 像素弯曲者 在这种情况下,这可能对您有用。这是Adobe的一个教程 将像素弯曲器过滤器应用于Flash中的图像.

否则,您可以一次处理行。 (请注意,您的代码有一个轻微的错误是重新确定宽度的每次迭代的高度):

private var currentRow:Number = 0;
private var timer:Timer;
public function processImage(event:Event=null):void
{
    var m:int = bitmapData.height;
    for (var j:int = 0; j < m; j++)
    {
        if (bitmapData.getPixel(currentRow, j) == 0xCACACA)
        {
            bitmapData.setPixel32(currentRow, j, 0x00000000);
        }
    }

    currentRow++;
    if(currentRow < bitmapData.width)
    {
        timer = new Timer(1, 500);
        timer.addEventListener(TimerEvent.COMPLETE, processImage);
        timer.start();
    }
}

处理将需要更长的时间,但至少您的显示不会被阻止。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top