This example refines the image down to 80x80. SmoothImage is derived from
Image. I tested it against a 7000x6000 8.5mb jpg with no problems. It also
scales to the smaller side if they aren't equal.
main code...
private var uploadReference:FileReference = new FileReference();
uploadReference.addEventListener(Event.SELECT, loadImage);
uploadReference.addEventListener(Event.CANCEL, cancelImage);
uploadReference.addEventListener(Event.COMPLETE,
uploadCompleted);
private function loadPicture(evt:MouseEvent):void{
var imageArray:Array = new Array();
imageArray.push(new FileFilter("My Image",
".png;*.jpg;*.jpeg;*.gif"));
uploadReference.browse(imageArray);
}
private function loadImage(evt:Event):void{
uploadReference.load();
}
private function uploadCompleted(e:Event):void{
if(FileReference(e.target).data.length == 0){
return;
}
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
loader.loadBytes(FileReference(e.target).data);
}
private function onImageLoad(e:Event):void {
var imageBitmapData:BitmapData =
Bitmap(e.target.content).bitmapData;
var loader:Loader = Loader(e.target.loader);
var smoother:SmoothImage = new SmoothImage();
smoother.load(new Bitmap(imageBitmapData, "auto",
true));
smoother.content.width = 80;
smoother.content.height = 80;
smoother.width = 80;
smoother.height = 80;
if(imageBitmapData.width > 80 || imageBitmapData.height
> 80){
var imageSmoothed:SmoothImage = new
SmoothImage();
imageSmoothed.maintainAspectRatio = true;
imageSmoothed.load(new Bitmap(imageBitmapData,
"auto", true));
var maxSize:Number =
Math.max(imageBitmapData.width, imageBitmapData.height);
if(maxSize == imageBitmapData.width){
var widthScale:Number = 80 /
imageBitmapData.width;
var newWidth:Number =
imageBitmapData.width * widthScale;
var newHeight:Number =
imageBitmapData.height * widthScale;
imageSmoothed.content.width =
newWidth;
imageSmoothed.content.height =
newHeight;
imageSmoothed.width = newWidth;
imageSmoothed.height =
newHeight;
var finalBitmap:BitmapData =
new BitmapData(newWidth, newHeight);
finalBitmap.draw(imageSmoothed);
imageBitmapData = null;
imageBitmapData = finalBitmap;
}else{
var heightScale:Number = 80 /
imageBitmapData.height;
var newWidth2:Number =
imageBitmapData.width * heightScale;
var newHeight2:Number =
imageBitmapData.height * heightScale;
imageSmoothed.content.width =
newWidth2;
imageSmoothed.content.height =
newHeight2;
imageSmoothed.width = newWidth2;
imageSmoothed.height =
newHeight2;
var finalBitmap2:BitmapData =
new BitmapData(newWidth2, newHeight2);
finalBitmap2.draw(imageSmoothed);
imageBitmapData = null;
imageBitmapData = finalBitmap2;
}
}
// some image...
imageToDisplay.source = new Bitmap(imageBitmapData);
}
--- In [email protected], Aaron Hardy <aaronius...@...> wrote:
>
> Hey flexers,
>
> We're working on a project currently where we would like to allow users to
> add photos from their local drive to the stage. We also want to support
> gigantic photos. I understand the max bitmap size is 4095x4095 (or
> dimensions that equal the same number of pixels) in flash player 10. If the
> user has a photo bigger than this, we'd be happy to size the image down to
> this limit and allow the user to work with their image at the restricted
> size. However, I'm stuck trying to figure out how to size down the image
> before creating a bitmap object. I have a bytearray of the image data but
> now I need the image resized down to the bitmap limit before creating a
> bitmap from the bytearray. Somewhat a chicken-or-the-egg problem. Does
> anyone have a nice tool or method to accomplish this? Anyone know how
> Buzzword does this?
>
> Thanks.
>
> Aaron
>