我正在使用Loader加载图像。加载后,我可以使用Bitmap(event.target.loader.content).bitmapData
获取位图数据。
然而,由于我加载的图像非常大(约2000 x 1600),我想减小尺寸并创建一个新的较小的位图,可能是200或300像素宽,有点像缩略图。
我认为这与使用新尺寸创建新BitmapData
有关。但是我无法正常工作。
有什么想法吗?
答案 0 :(得分:5)
未经测试,但这应该给你一个想法:
public function scaleBitmap(src: BitmapData, ratio: Number): BitmapData
{
var bmd: BitmapData = new BitmapData(src.width * ratio, src.height * ratio);
var m: Matrix = new Matrix();
m.scale(ratio, ratio);
bmd.draw(src, m);
return bmd;
}
答案 1 :(得分:2)
您也可以使用BitmapImage类中的此代码。我已经更新它以支持BitmapData类的drawWithQuality方法。有关值,请参阅StageQuality类。
注意:您需要将SWF版本设置为至少16才能使用质量API。但是,编译器似乎不会抛出任何错误。
/**
* @private
* Utility function used for higher quality image scaling. Essentially we
* simply step down our bitmap size by half resulting in a much higher result
* though taking potentially multiple passes to accomplish.
*
* source spark.primitives.BitmapImage
*/
protected static function resample(bitmapData:BitmapData, newWidth:uint,
newHeight:uint, quality:String = null):BitmapData
{
var finalScale:Number = Math.max(newWidth/bitmapData.width,
newHeight/bitmapData.height);
var finalData:BitmapData = bitmapData;
// ERROR HERE MEANS
// Property drawWithQuality not found on flash.display.BitmapData and there is no default value.
//
// Solution
// add -swf-version=16 or greater to your compiler arguments
//
// https://bugbase.adobe.com/index.cfm?event=bug&id=3219149
if (finalScale > 1)
{
finalData = new BitmapData(bitmapData.width * finalScale,
bitmapData.height * finalScale, true, 0);
if (quality) {
finalData.drawWithQuality(bitmapData, new Matrix(finalScale, 0, 0,
finalScale), null, null, null, true, quality);
}
else {
finalData.draw(bitmapData, new Matrix(finalScale, 0, 0,
finalScale), null, null, null, true);
}
return finalData;
}
var drop:Number = .5;
var initialScale:Number = finalScale;
while (initialScale/drop < 1)
initialScale /= drop;
var w:Number = Math.floor(bitmapData.width * initialScale);
var h:Number = Math.floor(bitmapData.height * initialScale);
var bd:BitmapData = new BitmapData(w, h, bitmapData.transparent, 0);
if (quality) {
bd.drawWithQuality(finalData, new Matrix(initialScale, 0, 0, initialScale),
null, null, null, true, quality);
}
else {
bd.draw(finalData, new Matrix(initialScale, 0, 0, initialScale),
null, null, null, true);
}
finalData = bd;
for (var scale:Number = initialScale * drop;
Math.round(scale * 1000) >= Math.round(finalScale * 1000);
scale *= drop)
{
w = Math.floor(bitmapData.width * scale);
h = Math.floor(bitmapData.height * scale);
bd = new BitmapData(w, h, bitmapData.transparent, 0);
if (quality) {
bd.drawWithQuality(finalData, new Matrix(drop, 0, 0, drop), null, null, null, true, quality);
}
else {
bd.draw(finalData, new Matrix(drop, 0, 0, drop), null, null, null, true);
}
finalData.dispose();
finalData = bd;
}
return finalData;
}
使用它:
var bitmapData:BitmapData = resample(sourceBitmapData, desiredWidth, desiredHeight, StageQuality.HIGH_16X16);