Actionscript 3:如何从 BitmapData 中删除所有黑色像素?
假设我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 getColorBoundsRect 来查找BitmapData 中不同颜色像素的尺寸:
这将跟踪littleBmdBounds:(x=10, y=10, w=30, h=60)
接下来,我们需要复制其中的内容将这些边界添加到新的 BitmapData 中:
最后,使用阈值删除任何剩余的黑色并使其透明:
You can use getColorBoundsRect to find the dimensions of the differently-colored-pixels inside your BitmapData:
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:
Finally, use threshold to remove any remaining black and make it transparent: