actionscript-3apache-flexflex3externalinterface

Read ByteArray from Javascript in Flex AS function


I need to pass ByteArray from Javascript function to Flex Actionscript function. Have tried using the below JS:

function deviceResp(s){ 
    var obj = document.getElementById('smera');
    obj.deviceRespFx(s);    
}

My AS function :

public function imageRespFxs(value:String):void{
    Alert.show(value.length);
}

EDIT: Callback Function is loaded on "creationComplete"

   public function initDevice():void{                  
                               ExternalInterface.addCallback("deviceRespFx",imageRespFxs);

        }

As the above method is inturn converting a byte array to a String so there is a loss of data, could any one please assist on how to pass a Byte Array from JS to AS function.


Solution

  • All thanks to Sam DeHaan,

    Have finally managed to get it working with Base64 as hinted by Sam comments.

    here is the peice of code might be helpful for others:

    Firstly in my Java Code I convert the Image into Base64 String like this :

      private String convertBase64String(BufferedImage image){
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Base64OutputStream bi64 = new Base64OutputStream(baos);
        try{
        ImageIO.write(imgRegistration1, "png",bi64);
        baos.flush();               
        String data = baos.toString("UTF-8");
        return data;
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        return null;
    
    }
    

    and I send this to FLex AS method and Decode the Same like this:

      import mx.utils.Base64Decoder;
      private var base64Dec:Base64Decoder;
      public function imageRespFxs(value:String):void{                
        var byteArr:ByteArray;
                base64Dec = new Base64Decoder();
                base64Dec.decode(value);
                byteArr = base64Dec.toByteArray();
                imgId.load(byteArr);
    
            }
    

    Thanks