Actionscript: image to base64 string possible?

*爱你&永不变心* 提交于 2019-12-19 18:22:37

问题


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

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

Thanks ;)


回答1:


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.




回答2:


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 :)



来源:https://stackoverflow.com/questions/6596341/actionscript-image-to-base64-string-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!