动作3:如何从的BitmapData删除所有黑色像素? [英] Actionscript 3: How to remove all black pixels from BitmapData?

查看:102
本文介绍了动作3:如何从的BitmapData删除所有黑色像素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让说我有一个的BitmapData不同像素重新presenting一个对象,并围绕它的一些黑点,我想删除。

Let say I have a BitmapData with different pixels representing an object, and some black pixels around it that I want to remove.

我希望获得一个新的BitmapData,与宽度由非黑色像素psented对象重新$ P $的和高度。

I would like to obtain a new BitmapData, with width and height of the object represented by non-black pixels.

例如,让说我有一个的BitmapData 400x400px,而是由非黑色像素psented对象重新$ P $占据RECT:X = 100,Y = 100,宽度= 200,高度= 200。我想获得新的BitmapData重新presenting的RECT,全黑的像素应该被删除。当然,我没有坐标,该矩形,我需要以某种方式作出区别了全黑色的BitmapData和原来的之间,构建一个新的BitmapData(不同的宽度和高度)。

For example, let say I have a BitmapData 400x400px, but the object represented by non-black pixels occupies the rect: x=100, y=100, width=200, height=200. I want to get new BitmapData representing that rect, all black pixels should be removed. Of course, i have no coordinates for that rectangle, i need somehow to make difference between a full black bitmapdata and the original one, and construct a new bitmapdata (different width and height).

这是如何做到这一点的任何想法吗?

Any idea on how to do this please ?

推荐答案

您可以使用<一个href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html#getColorBoundsRect%28%29"相对=nofollow> getColorBoundsRect 找到不同彩色像素的尺寸,你的BitmapData内侧:

You can use getColorBoundsRect to find the dimensions of the differently-colored-pixels inside your BitmapData:

//some fake data
var yourBigBmd:BitmapData = new BitmapData( 300, 300, false, 0 );
yourBigBmd.fillRect( new Rectangle( 10, 10, 30, 60 ), 0xFF0000 );
//a little notch
yourBigBmd.fillRect( new Rectangle( 10, 10, 10, 10), 0x00000 );

var blackColor:uint = 0x000000;
var littleBmdBounds:Rectangle = yourBigBmd.getColorBoundsRect( 0xFFFFFF, blackColor, false );
trace( "littleBmdBounds: " + littleBmdBounds );

这将跟踪的 littleBmdBounds:(X = 10,Y = 10,W = 30,H = 60)

接下来,我们需要复制什么在这些边界进入一个新的BitmapData:

Next, we need to copy what's in those bounds into a new BitmapData:

var littleBmd:BitmapData = new BitmapData( littleBmdBounds.width, littleBmdBounds.height, true, 0 );
var mx:Matrix = new Matrix();
mx.translate( -littleBmdBounds.x, -littleBmdBounds.y );
littleBmd.draw( yourBigBmd, mx );

最后,使用阈值以除去任何剩余的黑色和使它透明

Finally, use threshold to remove any remaining black and make it transparent:

var blackAlphaColor:uint = 0xFF000000;
var transparentColor:uint = 0x00000000;
littleBmd.threshold( littleBmd, littleBmd.rect, littleBmd.rect.topLeft, "==", blackAlphaColor, transparentColor )

这篇关于动作3:如何从的BitmapData删除所有黑色像素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆