[英]Trim whitespace around a transparent 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.