Actionscript将背景转换为透明

时间:2009-03-23 00:41:02

标签: actionscript-3 colors

该函数的目的是将背景转换为透明然后返回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;
        }

请建议。

3 个答案:

答案 0 :(得分:0)

Theo,函数似乎可以运行,但需要一些时间,但是没有bitmapdata返回。

我通过以下代码收到bitmpadata:

bitmapData = transparentConverter( bitmapData );
var bitmap:Bitmap = new Bitmap( bitmapData );
image1.source = bitmap;

图像不会出现。

而且,我可以跟踪(c1),并获得一长串数据。 谢谢你的回复。

答案 1 :(得分:0)

你可以试试这个:

// 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);

答案 2 :(得分:0)

Theo,谢谢你的努力。我认为问题是功能运行时间太长。在我尝试您的脚本后,会出现一条警告消息。它说该程序运行时间超过15秒,并告诉我停止运行。

我觉得这个功能应该没问题,但也许不太实际。

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

我认为问题可以结束。

相关问题