[英]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.