Load the image bytecode in String and convert to Bitmap in Flash AS3

Asked

Viewed 697 times

3

I need to load the image bytecodes inside a String and then convert 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);

}

When I encode the image for Base64 and open it using Steve Webster’s library, it works correctly.

1 answer

2

Flashplayer itself has a class for you to convert a string to 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 have not tested the example below but this is the path you must 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);
}
  • Thank you for the reply Jan Cassio!

  • Unfortunately it hasn’t helped yet. See the following procedure I want to do: "Bytecodeimage -> String -> Bitmap = goes wrong". When I do "Bytecodeimagem64 -> String -> Decodificar64 -> Bitmap works"! Another observation is that the Base64decoder class is unfortunately for Flex and I am using Flash that does not use the mx.util package. Thanks anyway!

  • In this case, you can use any Base64 for AS3, take a look at this class, think it has everything you need. https://gist.github.com/burakkirkil/4051420

  • Sorry but it is not a Base64 image that I want to turn into String and then into Bitmap, but a common image. I quoted this encoding because when I use it it works normally and I have no doubts regarding this library. Still thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.