Actionscript 3 Actionscript将背景转换为透明

Actionscript 3 Actionscript将背景转换为透明,actionscript-3,colors,Actionscript 3,Colors,该函数的目的是将背景转换为透明,然后返回bitmapdata,但它似乎不起作用。代码如下: private function transparentConverter( source:BitmapData, threshold:Number = 0.1 ):BitmapData { var bitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0x000

该函数的目的是将背景转换为透明,然后返回bitmapdata,但它似乎不起作用。代码如下:

    private function transparentConverter( source:BitmapData, threshold:Number = 0.1 ):BitmapData
        {
            var bitmapData:BitmapData = new BitmapData( source.width, source.height, true, 0x00000000  );
            bitmapData.lock();
            var color:uint = source.getPixel( 0, 0 );

            var x:uint, y:uint;
            for ( y = 0; y < source.height; y++ )
            {
                for ( x = 0; x < source.width; x++ )
                {
                    var c1:uint = source.getPixel( x, y );
                    var c2:uint = color;
                    var rx:uint = Math.abs((( c1 & 0xff0000 ) >> 16 ) - (( c2 & 0xff0000 ) >> 16 ));
                    var gx:uint = Math.abs((( c1 & 0xff00) >> 8 ) - (( c2 & 0xff00 ) >> 8 ));
                    var bx:uint = Math.abs(( c1 & 0xff ) - ( c2 & 0xff ));

                    var dist:uint = Math.sqrt( rx*rx + gx*gx + bx*bx );

                    if ( dist <= threshold )
                    {
                        bitmapData.setPixel32( x, y, 0x00000000 );
                    }else
                        {
                            bitmapData.setPixel32( x, y, c1 );
                        }
                }
            }
            bitmapData.unlock();
            return bitmapData;
        }

请给出建议。

Theo,该函数似乎可以运行,但需要一些时间,但不会返回位图数据

我通过以下代码接收bitmpadata:

bitmapData = transparentConverter( bitmapData );
var bitmap:Bitmap = new Bitmap( bitmapData );
image1.source = bitmap;
图像不会显示

而且,我可以追踪c1,得到一长串的数据。 谢谢您的回复。

您能试试这个吗:

// This bitmapData should be defined for real, wherever you get that from ...
var source:BitmapData;  

if(source == null)
   trace("The source cannot be empty");

// Here you get the transformed BitmapData
var destination:bitmapData = transparentConverter( source );

// You apply it to a Bitmap in order to visualize it
var viewBitmap:Bitmap = new Bitmap(destination);

// You add the Bitmap to the stage so you can see it
addChild(viewBitmap);

西奥,谢谢你的努力。我认为问题在于函数运行时间太长。在我尝试了你的脚本之后,出现了一条警告消息。它说程序运行需要15秒以上,并告诉我停止运行

我认为这个函数应该是可以的,但是,可能不太实用

西奥,再次感谢你的时间和建议

我想这个问题可以结束了