Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript: image to base64 string possible?

Is it possible to convert an selected image into base64 encoded string?

Would be nice and easy solution for image uploader. :)

Thanks ;)

like image 301
Somebody Avatar asked Jul 06 '11 12:07

Somebody


2 Answers

If you're wanting to encode the byteArray of a loaded image, you could use the Base64Encoder class from mx.utils Base64Encoder.

Something like:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
loader.load(new URLRequest("img.jpg"));

function loadComplete(e:Event):void {
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
    var bmd:BitmapData = Bitmap(e.target.content).bitmapData;
    var ba:ByteArray = bmd.getPixels(new Rectangle(0,0,bmd.width,bmd.height));
    var b64:Base64Encoder = new Base64Encoder();
    b64.encodeBytes(ba);
    trace(b64.toString());
}

I had to track down the class here.

Also, there's another Base64 class that I found but haven't tested here...but looks like it works similarly.

Hope that helps.

like image 154
Corey Avatar answered Sep 20 '22 10:09

Corey


You can save an image as a Base64-string, but I wouldn't recommend it. I have tried doing this and it slows down your application a lot.

If you still want to do this, you should download the Base64-class at this link: http://garry-lachman.com/2010/04/21/base64-encoding-class-in-actionscript-3/

If you then get the bitmapData from your image, you can call for the .getPixels()-method, which returns a bytearray. This bytearray can be converted to a Base64-string using the class in the link.

If you want to load images from a Base64-string, you can create a Loader-object and use the loadBytes()-method to load in the byteArray you get by decoding your Base64-string.

Hope this helps :)

like image 25
Michiel Standaert Avatar answered Sep 18 '22 10:09

Michiel Standaert