繁体   English   中英

修剪透明PNG周围的空白

[英]Trim whitespace around a transparent PNG

因此,我设法让自己陷入这样一种情况,即需要在舞台上放置一个充满图像的数据库(各种产品的透明图像),所有这些都需要按照产品高度对齐。

我的问题是,png的产品是“漂浮的”,我无法控制它所处的png位置(顶部可能很紧,底部可能是负载,反之亦然)

有没有人知道找出png'真'高度的现有方法(宽度是额外的)。 我已经考虑过循环位图数据和检查,但想知道是否有人已经发明了这个轮子?

例如,房间在顶部的 示例 / 示例没有,真的

您可以使用BitmapData类的方法getColorBoundsRect()来获取非透明内容的矩形。 文档也给出了这个例子:

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

谢谢,Alistair

正如Alistair所说,getColorBoundsRect最终将是您的最佳选择。

我没有太多关注,但我不确定getColorBoundsRect是否允许你“选择所有非100%alpha-ed像素”。 如果没有,您可以轻松使用BitmapData.threshold方法来进入该阶段。

我做了类似复制位图的操作,运行阈值方法将所有非alpha-ed像素变为亮绿色,然后运行getColorBoundsRect选择刚刚创建的所有绿色像素。

这是我提出的解决方案,以防任何人需要:

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

我最终得到的解决方案是下面的,很可能不是最高效的方式,但它的工作原理。

    /**
     * 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