FlyPaper

Things that tend to stick

Flash Notes: Importing and displaying images via Binary ByteArray


   Feb 09

Flash Notes: Importing and displaying images via Binary ByteArray

After weeks of sickening trial and error, I have finally figured out a method of importing and displaying ByteArrays as images.

 

To do this, you’ll need the base64 converter library created by the folks at Dynamic Flash available here.

We will continue this under the assumption that you are familiar with class paths and importing external classes.

On to the code:

The full source code is available here: BytaArrayToImage.zip.

There are many methods to get your data, however for this example, we will be using a predetermined ByteArray in an XML file along with some parameters to tell the image where to go and how big to be. This is a very simplified proof of concept, but it will open the door to better things.

package {
    import flash.display.MovieClip;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.*;
    import flash.display.Loader;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.ByteArray;
    import com.dynamicflash.util.Base64;
    import flash.xml.XMLDocument;

    public class ByteArrayToImage extends MovieClip  {

        //xml doc object
        public var mainXML:XML;
        public var theImage:Loader;
        //loader for images.
        var imageLoader:Loader;

        public function ByteArrayToImage(){
            init();
        }

        private function init(){
            digestXML();
        }

        private function digestXML(){
            //build a loader object for the XML file
            var loader:URLLoader = new URLLoader();
            //load the file
            loader.load(new URLRequest("testXML.xml"));
            //once complete, run xmlLoaded function
            loader.addEventListener(Event.COMPLETE, xmlLoaded);
        }

        private function xmlLoaded(evt:Event){
            //create an XML document  to laod the data into
            var mainXMLDoc:XMLDocument = new XMLDocument(evt.target.data);
            //load said data
            mainXML = new XML(mainXMLDoc);
            //parse thru the data and load the images
            for (var i:int = 0; i < mainXML.image.length(); i++){
                // it is important to notice the use of the base64Decoder class on the image string.  This will not work without it.
                loadImage(mainXML.image[i].@imageId, Base64.decodeToByteArray(mainXML.image[i].imageBytes), mainXML.image[i].@xPos, mainXML.image[i].@yPos, mainXML.image[i].@width);
            }
        }

        private function loadImage(imageName:String, imageArray:ByteArray, xPosition:int, yPosition:int, imageWidth:int):void {

            imageLoader = new Loader();
            imageLoader.loadBytes(imageArray);

            var imageToLoad = mainMC;
            imageToLoad.x = xPosition;
            imageToLoad.y = yPosition;
            imageToLoad.width = imageWidth;
            imageToLoad.scaleY < imageToLoad.scaleX ? imageToLoad.scaleX = imageToLoad.scaleY : imageToLoad.scaleY = imageToLoad.scaleX;
            imageToLoad.addChild(imageLoader);

        }

    }
}

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments

  1. ALBERT says:


    Pillspot.org. Canadian Health&Care.Special Internet Prices.No prescription online pharmacy.Best quality drugs. Online Pharmacy. Buy drugs online

    Buy:Viagra.Viagra Super Active+.Viagra Soft Tabs.VPXL.Maxaman.Propecia.Viagra Super Force.Viagra Professional.Cialis Professional.Soma.Tramadol.Levitra.Zithromax.Cialis Soft Tabs.Super Active ED Pack.Cialis.Cialis Super Active+….

Leave a Reply