Search code examples
arraysactionscript-3bitmapdata

How to get size of bitmapdata converted into bytearray


I have bitmapdata object converted to bytearray:

var bytes:ByteArray = bitmapDataA.getPixels(bitmapDataA.rect);

and I save it as .jpg file for later:

writeStream.writeBytes(bytes, 0, bytes.bytesAvailable);

Now, when I want to retrieve the data:

var file:File = File.documentsDirectory.resolvePath("data/"+photoNameVec[loadCounter]);         
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var fileContent:ByteArray = new ByteArray;
fileStream.readBytes(fileContent);
fileStream.close();         
var bmpData:BitmapData = new BitmapData(XXX, YYY);
bmpData.setPixels(bmpData.rect, fileContent);
addChild(new Bitmap(bmpData));

But I need to get exact XXX and YYY to show the graphics correctly. How can I do this?

EDIT:

As there was no way to determine bitmapdata width and height based on bytearray, I decided to transform all pictures I take to 1024px x 768px:

var bitmapDataA:BitmapData = new BitmapData(mpLoaderInfo.width, mpLoaderInfo.height);
var matrix:Matrix = new Matrix();
matrix.scale( 1024 / mpLoaderInfo.width, 768 / mpLoaderInfo.height);
var bitmapDataB:BitmapData = new BitmapData(1024,768);
bitmapDataB.draw(mpLoaderInfo.content, matrix, null, null, new Rectangle(0,0,1024,768), true);

Still waiting for tips on getting Width x Height from bytearray created by getPixels method of bitmapdata.


Solution

  • I guess that's the most suiting solution :) Simply expand the the bytearray with extra values from which I shall gather width and height.

    http://www.ghostwire.com/blog/archives/as3-serializing-bitmaps-storing-bitmapdata-as-raw-binarybytearray/

    Ps.: Creating and reading uncompressed files is way faster than making png/jpg