Encoding Bitmaps and Byte Arrays
Hello,
I've been banging my head repeatedly on my desk for the last couple of days, trying to get my program to work, with no avail. With no further option, I decided i'd ask the experts.
I am making a program which will ask the user to select an image, then encode it to a binary string and store in an XML file. It will then be able to 'decode' the fil and reconstruct it.
I've followed a few guides online, but I can't seem to get anything to work. My understanding is that I need to break the Bitmap into it's BitmapData components, convert that to a 'byteArray' which is saved, (or optionally encoded) then that same byteArray data is retrieved and made into a movie clip onto the stage. However, no matter what I do, I can't get it to properly recreate the bitmap once i've grabbed the data... It always throws a "end of file
In order to get the basics, i've cut my code down to the bare minimum - which is just to load the image from the local filesystem, grab it's bitmapdata, clone it and place it into a new Bitmap which is added to the stage. However, I only ever get returned a "End of file encountered" error message - which seems to indicate to me it's not properly populating the byteArray. (It certainly looks much too short when traced)
Please help!
Here's my code for the basic loader -
import flash.display.Sprite;
import flash.events.*;
import flash.net.*;
//load image
var loader:Loader = new Loader();
var url = "C:\\Data\\cutcord.jpg";
var urlReq:URLRequest = new URLRequest(url);
configureLoader(loader.contentLoaderInfo);
loader.load(urlReq);
function completeHandler(event:Event):void {
var imageIn:BitmapData = event.target.content.bitmapData;
var byteArray:ByteArray = imageIn.getPixels(imageIn.rect);
var newBitmapData:BitmapData = imageIn.clone();
newBitmapData.setPixels(imageIn.rect, byteArray);
var newBitmap:Bitmap = new Bitmap(newBitmapData,"auto",true);
this.addChild(newBitmap);
}
function configureLoader(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
}