Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 937
user1013448 Avatar asked Oct 23 '22 20:10

user1013448


1 Answers

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

like image 146
Cristi Mihai Avatar answered Oct 31 '22 11:10

Cristi Mihai