Francisc
Francisc

Reputation: 80395

Flex 4 fileReference selected image file dimmensions (width and height)

I use a fileReference.browse() to select an image file from the harddrive.

How can I check the Width and Height of the selected image file please?

Thank you!

Upvotes: 3

Views: 7663

Answers (3)

Mark Bulleit
Mark Bulleit

Reputation: 11

You should be able to read the image.sourceWidth and image.sourceHeight if you wait for the image source property to update. This will give you the unscaled original values.

<fx:Script>
    <![CDATA[

        import mx.events.FlexEvent;     

        private function browseImage(event:MouseEvent):void {
            var arr:Array = [];
            arr.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
            imageFileReference.browse(arr);         
        }

        private function imageSelect(evt:Event):void {
            imageFileReference.load();
        }

        private function imageComplete(evt:Event):void {
            image.source = smallImageFileReference.data;
            image.addEventListener(FlexEvent.UPDATE_COMPLETE, getImageSize);                
        }

        private function getImageSize(evt:FlexEvent):void {
            image.removeEventListener(FlexEvent.UPDATE_COMPLETE, getImageSize);         
            imageWidth.text = image.sourceWidth + "px";
            imageHeight.text = image.sourceHeight + "px";
        }
    ]]>

</fx:Script>

<fx:Declarations>
    <net:FileReference id="imageFileReference" 
        select="imageSelect(event)" 
        complete="imageComplete(event)"/>
</fx:Declarations>

<s:VGroup width="100%" height="100%">

    <s:HGroup width="100%" verticalAlign="middle">
        <s:Label fontWeight="bold" text="Width:" />
        <mx:Text id="imageWidth" />
    </s:HGroup> 

    <s:HGroup width="100%" verticalAlign="middle">
        <s:Label fontWeight="bold" text="Height:" />
        <mx:Text id="imageHeight" />
    </s:HGroup>

    <s:Image id="image" maxHeight="200" maxWidth="200" />
    <s:Button label="Browse for Image" click="browseImage(event)" />

</s:VGroup>

Upvotes: 1

andi
andi

Reputation: 98

Load the fileReference.data into a Loader using loadBytes(). Then you'll have: sourceBMP:Bitmap = loader.content as Bitmap;

Here is a sample code:

MXML part:

<fx:Declarations>
    <net:FileReference id="fileReference"
        select="fileReference_select(event);"
        complete="fileReference_complete(event);" />
</fx:Declarations>
<s:Button id="uplaodImageBtn"
    label="Upload Image"
    click="uplaodImageBtn_clickHandler()"/>

AS3 part:

private function uplaodImageBtn_clickHandler() : void {
    var arr:Array = [];
    arr.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
    fileReference.browse(arr);
}

private function fileReference_select(evt:Event):void {
    fileReference.load();
}

private function fileReference_complete(event:Event):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
    loader.loadBytes(fileReference.data);
}

public function loader_complete (event:Event) : void {
    var sourceBMP:Bitmap = event.currentTarget.loader.content as Bitmap;
    Alert.show(sourceBMP.width + ', ' +sourceBMP.height);
}

Upvotes: 8

JeffryHouser
JeffryHouser

Reputation: 39408

From the context of Flex, I'm pretty sure that onc you get the results back from a browserr, it is only a byteArray. In theory if you use that byteArray as the source for an image tag you'll be able to get the height and width that way, once you add that image to a container.

Otherwise, I do not believe there is an easy way to get such metadata info from local files using Flex.

Upvotes: 1

Related Questions