user1013448
user1013448

Reputation: 249

Getting image size from ByteArray

I am wondering if there is any way to determine the width and height of an image that is decoded to a ByteArray. For example in the below, any way to determine these values for data?

var data:ByteArray = new ByteArray();

data = encoded_image.decode(byteArrayData);

Upvotes: 5

Views: 2276

Answers (2)

SketchBookGames
SketchBookGames

Reputation: 516

you can read it from the header. headers are different for each file type though. look at custom image decoders, this is one of the things they do.

here is one for pngs:

http://ionsden.com/content/pngdecoder

Upvotes: 0

Cristi Mihai
Cristi Mihai

Reputation: 2565

You can do it like this:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded)
loader.loadBytes(byteArrayData);

-

function onLoaded(e:Event):void
{
    var loader:Loader = Loader(e.target.loader);
    var bitmapData:BitmapData = Bitmap(e.target.content).bitmapData;

    width = bitmapData.width;
    height = bitmapData.height;

    // cleanup
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoaded);
}

The downside is that the whole image is going to be decoded, so if you don't actually need the image, but only the width and height, you might actually want to look in the byte array and decode the file format. (More tricky, but

Upvotes: 3

Related Questions