簡體   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