Resize images in Flex using as3
I am loading an image using a Loader. On开发者_开发知识库ce loaded, I can get the bitmap data using Bitmap(event.target.loader.content).bitmapData
.
However since the images I am loading are quite large (around 2000 x 1600), I would like to reduce the size and create a new smaller bitmap maybe 200 or 300 pixels wide, sort of like a thumbnail.
I think it has to do with creating a new BitmapData
with the new size. However I am not able to get that working properly.
Any ideas?
Not tested, but this should give you an idea:
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;
}
You can also use this code from the BitmapImage class. I've updated it to support the drawWithQuality method of the BitmapData class. See the StageQuality class for values.
Note: You'll need to set the SWF version to at least 16 to use the quality API. However, the compiler doesn't seem to throw an error either way.
/**
* @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;
}
And to use it:
var bitmapData:BitmapData = resample(sourceBitmapData, desiredWidth, desiredHeight, StageQuality.HIGH_16X16);
精彩评论