检测并删除图像中的特定像素
是否可以(动态)检测图像的白色像素并将其删除?实际上,我在舞台上加载了一些图像,但它们是正方形的,而实际图像不是正方形大小的。所以我想让点击区域完全符合图像的形状,而不是它们所具有的白色边框。
ps:在动作脚本中:)
Is it possible to detect (dynamically) the white pixels of an image and delete them? Actually i have some images that i load on stage but they are square without the actual image be a square sized. So i want to make the hit area exactly the shape of the image and not the whit border they have.
ps: In actionscript :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过循环遍历像素来完成此操作,从左上角开始,一次循环一个,然后右下角,直到找到非白色像素。有点像这样...
首先使用 getPixel
现在您有了实际图像的左上角和右下角,没有白色。
使用 copyPixels
现在 您有一个 BitmapData 对象,其中仅包含您想要的像素。随意处理它,也许:
You could do it by looping through the pixels, 1 at a time starting from top-left, and bottom-right till you find a non-white pixel. Sort of like this...
First use getPixel
Now you have the top left and bottom right corners of the actual image, without the white.
Now use copyPixels
Now you have a BitmapData object with only the pixels you want. Do with it as you please, maybe:
您可以循环访问位图数据,检查每个像素并查看其颜色是否与白色边框匹配,但如果图像中存在其他白色像素,则可能会遇到麻烦,除非您只检查图像上的某些位置,并且实际上这已经开始听起来很乱。
编辑:
据我了解,您的图像外部带有白色边框,并且您不希望边框成为 hiarea 的一部分。我建议您编辑图像(在 Photoshop 外部)并删除白色边框。
然后使用 ActionScript 将这些白色边框重新创建为单独的图像。检查实际图像的大小,然后创建一个大一点的精灵并用白色填充它并不会太难。然后将实际图像放在其上面并居中。所以现在看起来图像有边框。您可以将白色填充图像和实际图像都放置在一个容器中,以便将它们作为一个整体轻松移动。您还可以轻松地将图像设置为命中区域而不是边框。
You can loop through the bitmap data, check each pixel and see if its colour matches the white border but may run into trouble if there are other white pixels in the image, unless you only check certain positions on the image and really this is starting to sound very messy.
EDIT:
From what I understand you have images with white borders along the outside and you do not want the borders to be part of the hitarea. What I propose is that you edit the images (externally in photoshop) and remove the white borders.
Then instead recreate these white borders as a seperate image with ActionScript. It would not be too hard to check the size of the actual image and then create a sprite a bit bigger and fill it with white. Then place the actual image on top of it and centered. So now it looks like the image has a border. You can place both the white fill image and the actual image in a container to make it easy to move them as one. You can also easily set the image to be the hitarea and not the border.