开发者

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

开发者 https://www.devze.com 2023-01-11 05:46 出处:网络
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?

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!


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);
}


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消