Base64 Decode

I wrote a piece of AS3 code that takes a base64 Encoded image from a piece of xml (jabber vcard stanza) and puts it on the stage.Because the mx.utils.Base64Encoder package is still undocumented (or not implemented yet ?) I used one I found at http://blog.jasonnussbaum.com/
I’m also still figuring out the whole E4X so I cheated a bit and removed the namespace xmlns=”vcard-temp” from the vCard tag.
[ Update: namespace� now works, so the cheat is gone.]

Basically you decode the Base64 Encoded String and copy it into a byteArray.
I use a loop to go through every char of the string en use writeByte to add it to the ByteArray, but maybe there’s a better/easier way.
You then use a Loader object to load in the byteArray you just created. Because the Loader object inherits from flash.display.DisplayObject you add it straight to the Stage using addChild.

Download example (includes Base64 class)




3 Responses to “Base64 Decode”

  1. GagNet says:

    Is your example for Flash or Flex? (or something else)

    This would be awesome if I got it to work in Flash but I’m having no luck.

  2. patrick says:

    The example was for flash, I think it was for a beta of flash player 9
    Anyway, the Base64 Decoder now sits in :
    mx.utils.Base64Decoder;
    So you don’t really need my class anymore.

    I’ll see if I can update the example to a Flash cs3 .fla

  3. chrisaiv says:

    I was able get your files to work in Flash CS3 by changing how you imported the TextField class. Flash also seems to prefer “utils” over “util”

    package {
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.events.*;
    import flash.utils.ByteArray;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.text.TextFormat;
    import flash.text.TextField;

    public class xmpp_test extends Sprite {

    [Embed(source="file://D:/Fonts/TabardEF/TabardEF-Medium.ttf", fontFamily="tabard")]
    public var tabard:String;

    private var iqPacket:XML;
    private var myLoader:URLLoader;
    private var avatarString:String;
    private var bitLoader:Loader ;
    private var debugField:TextField;

    public function xmpp_test() {
    stage.scaleMode = StageScaleMode.NO_SCALE;
    stage.align = StageAlign.TOP_LEFT;

    /**/
    debugField = new TextField();

    var format:TextFormat = new TextFormat();
    format.font = “tabard”;
    format.color = 0xFFFFFF;
    format.size = 12;
    debugField.defaultTextFormat = format;
    addChild(debugField);
    /**/
    debugField.text = “Reading xml”;
    getIQPacket();

    }

    private function getIQPacket():void{
    myLoader = new URLLoader();
    myLoader.addEventListener(“complete”, xmlLoaded);
    var request:URLRequest = new URLRequest(“iq.xml”);
    myLoader.load(request);

    }

    private function xmlLoaded(evtObj:Event):void {
    var vc:Namespace = new Namespace(“vcard-temp”);
    default xml namespace = vc;
    iqPacket = XML(myLoader.data);
    avatarString = iqPacket.vCard.PHOTO.BINVAL.toString();

    var decodedString:String = Base64.Decode(avatarString);
    var imageBytes:ByteArray = new ByteArray();
    for(var i:Number=0; i < decodedString.length ; i++){
    imageBytes.writeByte(decodedString.charCodeAt(i));
    }

    bitLoader = new Loader();
    bitLoader.loadBytes(imageBytes);
    addChild(bitLoader);

    // delete(imageBytes);
    }
    }
    }