简体   繁体   English

修剪透明PNG周围的空白

[英]Trim whitespace around a transparent PNG

So, I've managed to get myself stuck in a situation where a database full of images ( transparent images of various products ) needs to be placed on the stage, all of which need to be aligned the same by the products height. 因此,我设法让自己陷入这样一种情况,即需要在舞台上放置一个充满图像的数据库(各种产品的透明图像),所有这些都需要按照产品高度对齐。

My problem is that the png's of products are 'floating' and I have no control of where about's in the png it will sit ( could be tight at the top and loads of room at the bottom, vice versa) 我的问题是,png的产品是“漂浮的”,我无法控制它所处的png位置(顶部可能很紧,底部可能是负载,反之亦然)

Does anyone know an existing way to find out the png's 'true' height ( width is an extra ). 有没有人知道找出png'真'高度的现有方法(宽度是额外的)。 I've thought about looping through the bitmap data and checking but wondered if anyone has invented this wheel already? 我已经考虑过循环位图数据和检查,但想知道是否有人已经发明了这个轮子?

eg Example with room at top / Example with none, really 例如,房间在顶部的 示例 / 示例没有,真的

you can use the method of the BitmapData class getColorBoundsRect() to get the rectangle of non-transparent content. 您可以使用BitmapData类的方法getColorBoundsRect()来获取非透明内容的矩形。 The documentation gives this example too: 文档也给出了这个例子:

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/BitmapData.html

Thanks, Alistair 谢谢,Alistair

As Alistair says, the getColorBoundsRect will ultimately be your best option. 正如Alistair所说,getColorBoundsRect最终将是您的最佳选择。

I haven't looked into it too much, but I'm not sure if getColorBoundsRect allows you to "select all non 100% alpha-ed pixels". 我没有太多关注,但我不确定getColorBoundsRect是否允许你“选择所有非100%alpha-ed像素”。 If it doesn't you can easily use the BitmapData.threshold method to get you to that stage. 如果没有,您可以轻松使用BitmapData.threshold方法来进入该阶段。

I'd do something like duplicate the bitmap, run the threshold method on it turning all non alpha-ed pixels bright green, then run getColorBoundsRect selecting all the green pixels you just created. 我做了类似复制位图的操作,运行阈值方法将所有非alpha-ed像素变为亮绿色,然后运行getColorBoundsRect选择刚刚创建的所有绿色像素。

This is a solution I came up with, in case anyone needs: 这是我提出的解决方案,以防任何人需要:

    public static function trimAlpha(source:BitmapData):BitmapData {
        var notAlphaBounds:Rectangle = source.getColorBoundsRect(0xFF000000, 0x00000000, false);
        var trimed:BitmapData = new BitmapData(notAlphaBounds.width, notAlphaBounds.height, true, 0x00000000);
        trimed.copyPixels(source, notAlphaBounds, new Point());
        return trimed;  
    }

The solution I eventually came to was the below, most likely not the most performant way but it works. 我最终得到的解决方案是下面的,很可能不是最高效的方式,但它的工作原理。

    /**
     * Cuts off the transparency around a bitmap, returning the true width and height whilst retaining transparency
     *  
     * @param input Bitmap
     * 
     */
    private function trimTransparency(input:BitmapData,colourChecker:uint = 0x00FF00):Bitmap {

        //Keep a copy of the original
        var orignal:Bitmap = new Bitmap(input);

        //Clone the orignal with a white background
        var clone:BitmapData = new BitmapData(orignal.width, orignal.height,true,colourChecker);
        clone.draw(orignal);

        //Grab the bounds of the clone checking against white
        var bounds:Rectangle = clone.getColorBoundsRect(colourChecker, colourChecker, false);

        //Create a new bitmap to return the changed bitmap
        var returnedBitmap:Bitmap = new Bitmap();
        returnedBitmap.bitmapData = new BitmapData(bounds.width, bounds.height,true,0x00000000);
        returnedBitmap.bitmapData.copyPixels(orignal.bitmapData,bounds, new Point(0,0));    
        return returnedBitmap;


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM