Actionscript 3:如何从 BitmapData 中删除所有黑色像素?

发布于 2024-08-29 09:09:05 字数 317 浏览 2 评论 0原文

假设我有一个 BitmapData,其中不同的像素代表一个对象,并且它周围有一些我想要删除的黑色像素。

我想获得一个新的 BitmapData,其中对象的宽度和高度由非黑色像素表示。

例如,假设我有一个 400x400px 的 BitmapData,但由非黑色像素表示的对象占据了矩形:x=100,y=100,width=200,height=200。我想获得代表该矩形的新 BitmapData,所有黑色像素都应该被删除。当然,我没有该矩形的坐标,我需要以某种方式区分全黑位图数据和原始位图数据,并构造一个新的位图数据(不同的宽度和高度)。

请问您知道如何执行此操作吗?

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

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

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离线来电— 2024-09-05 09:09:05

您可以使用 getColorBoundsRect 来查找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 中:

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

最后,使用阈值删除任何剩余的黑色并使其透明:

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

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

This will trace littleBmdBounds: (x=10, y=10, w=30, h=60)

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文