Priyank Patel
Priyank Patel

Reputation: 6996

Action Script ExternalInterface.call() Issue

I am using the following AS code to call a javascript function which will return me the required value.

package 
{
    import flash.display.Sprite;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.events.MouseEvent;
    import flash.net.FileReference;
    import flash.utils.ByteArray;
    import com.adobe.images.JPGEncoder;
    import flash.external.ExternalInterface;

    public class Main extends Sprite
    {
        private var camera:Camera = Camera.getCamera();
        private var video:Video = new Video();
        private var bmd:BitmapData = new BitmapData(320,240);
        private var bmp:Bitmap;
        private var fileReference:FileReference = new FileReference();
        private var byteArray:ByteArray;
        private var jpg:JPGEncoder = new JPGEncoder();
        var id:int;





        public function Main():void
        {

            saveButton.visible = false;
            discardButton.visible = false;

            saveButton.addEventListener(MouseEvent.MOUSE_UP, saveImage);
            discardButton.addEventListener(MouseEvent.MOUSE_UP, discard);
            capture.addEventListener(MouseEvent.MOUSE_UP, captureImage);

            if (camera != null)
            {
                video.smoothing = true;
                video.attachCamera(camera);
                video.x = 140;
                video.y = 40;
                addChild(video);
            }
            else
            {
                trace("No Camera Detected");
            }
        }

        private function captureImage(e:MouseEvent):void
        {
            bmd.draw(video);
            bmp = new Bitmap(bmd);
            bmp.x = 140;
            bmp.y = 40;
            addChild(bmp);

            capture.visible = false;
            saveButton.visible = true;
            discardButton.visible = true;
        }

        private function saveImage(e:MouseEvent):void
        {
            byteArray = jpg.encode(bmd);
            id=ExternalInterface.call("getID()");

            fileReference.save(byteArray, id+".jpg");

            removeChild(bmp);
            saveButton.visible = false;
            discardButton.visible = false;
            capture.visible = true;
        }

        private function discard(e:MouseEvent):void
        {
            removeChild(bmp);
            saveButton.visible = false;
            discardButton.visible = false;
            capture.visible = true;
        }
    }
}

However when the function is called , it seems it is returning me a 0 always ,,,,, tried hard to understand the issue , but i guess i am not able to figure it out.I tried to alert the value returned by javascript function , it is proper,

function getID() {

    var idno = $('#ctl00_ContentPlaceHolder1_memberidcam').val();
    alert(idno);
    return idno;

}

Please somebody help me. Thanks

Upvotes: 0

Views: 402

Answers (2)

Jamil Geor
Jamil Geor

Reputation: 337

It could be an issue with the type you are returning from your JavaScript. The val() method is probably just returning a string.

So try changing your JavaScript to convert the value to an integer:

function getID() {
    var idno = $('#ctl00_ContentPlaceHolder1_memberidcam').val();
    return parseInt(idno);
}

Upvotes: 2

evilpenguin
evilpenguin

Reputation: 5478

Check your AllowScriptAccess parameter from the embed code. It influences whether the swf is permitted to interact with JS through ExternalInterface:

http://helpx.adobe.com/flash/kb/control-access-scripts-host-web.html

Also, best practice would be to check if ExternalInterface is available:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#available

Upvotes: 2

Related Questions