If you want to convert it back to pixels, I'd say go with the Loader
approach.

Other than that, you'd have to write a JPEG decoder (seems like a waste of
time, considering you can already use a Loader, which would also be faster,
probably).

Now, if you are only interested in getting the width and height, that data
is in the SOFO block (SOFO stands for start of frame 0).

SOFO is marked by this sequence of bytes:

0xFF 0xC0

So, basically, you can loop through your bytearray until you find this
sequence.

Then offset your current position as necessary and read the height and
width.

If you take a look at the JPEG encoder from as3CoreLib, you can see you have
to skip 5 bytes from the beggining of the SOFO marker to get the height (2
bytes) and the width (2 bytes).

http://code.google.com/p/as3corelib/source/browse/trunk/src/com/adobe/images/JPGEncoder.as

Now, I haven't read the JPEG spec, so I'm not sure if this is a fixed
offset. I'm assuming it is, but you'd better check it if you want to use
this code. I've tried it with a couple of JPEG's and it worked fine,
though.

Also, you might want to validate if what you've got is a JPEG (check the
first 2 bytes for 0xff, 0xd8); you might also want to check that you're not
reading out of the buffer bounds.


function readJPEGSize(data:ByteArray):void {
var len:int = 0;
var i:int = 0;
 var offset:int = 0;
while(data.bytesAvailable > 1) {
// look for 0xffc0
if(data[i] == 0xFF && data[i + 1] == 0xC0) {
offset = i;
break;
}
i++;
}

/*
writeSOF0(image.width,image.height);
                        writeWord(0xFFC0); // marker
                        writeWord(17);   // length, truecolor YUV JPG
                        writeByte(8);    // precision
                        writeWord(height);
                        writeWord(width);
 Skip 5 bytes:
2 SOFO marker
2 length
1 precision
*/
var h:int = 0;
var w:int = 0;
 if(offset > 0) {
offset += 5;
data.position = offset;
h = data.readUnsignedShort();
w = data.readUnsignedShort();
 trace(w,h);
}
}

Cheers
Juan Pablo Califano


2010/5/24 Karim Beyrouti <[email protected]>

> Hi All
>
> do any of you how to convert a ByteArray (of a JPG) back into a BitmapData
> object?
>
> Actually - i just need to get the dimensions of the bitmap before trashing
> it. I know you can do 'myloader.loadBytes( byteArray )' - but i am avoiding
> that for now - as i just need to get the size of the image.
>
> Any pointers?
>
>
> mucho thanks
>
>
> - karim
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to