Question :
I need to load the bytecodes of the image into a String
and then convert it to Bitmap
. I am using the code below but without success:
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest("imagebytecode.txt"));
urlLoader.addEventListener(Event.COMPLETE, loaded);
function loaded(e:Event):void {
var str:String = urlLoader.data;
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes(str);
var bitData:BitmapData = new BitmapData(100, 100);
var rec:Rectangle = new Rectangle(0, 0, 100, 100);
bitData.setPixels(rec, byteArray);
var bit:Bitmap = Bitmap(bitData);
}
How much do I encode the image to Base64
and open it using the Steve Webster library, works correctly.
Answer :
The flashplayer itself has a class for you to convert a string to the base64 format and also has a class to do the reverse process.
In your case, this code did not work, because you need to decode (or interpret) the string.
I did not test the example below but this is the path you should follow
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest("imagebytecode.txt"));
urlLoader.addEventListener(Event.COMPLETE, loaded);
function loaded(e:Event):void
{
var str:String = urlLoader.data;
var base64:Base64Decoder = new Base64Decoder;
// decodifica e adiciona o resultado no buffer
base64.decode(str);
// obtem uma amostra em bytes do resultado
var bytes:ByteArray = base64.toByteArray();
var rect:Rectangle = new Rectangle(100, 100);
var bitmapData:BitmapData = new BitmapData(rect.width, rect.height);
// escreve a amostra dentro do bitmap data
bitmapData.setPixels(rect, bytes);
// e finalmente, a imagem
var bitmap:Bitmap = new Bitmap(bitmapData);
}