Priyank Patel
Priyank Patel

Reputation: 7006

Passing Image Captured Using Flash To Asp.net Page

I am using ActionScript 3 to capture image via users webcam in one of my Asp.net Page. Here is the code i am using

   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 com.dynamicflash.util.Base64;

     import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLVariables;





    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();
        private var _uploadPath:String = "/Upload.aspx";







        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);
            var base64Bytes:String = Base64.encodeByteArray(byteArray);
            var vars:URLVariables = new URLVariables();
            vars.imageData = base64Bytes;

            // and send it over the wire via HTTP POST
            var url:URLRequest = new URLRequest(_uploadPath);
            url.data = vars;
            url.method = URLRequestMethod.POST;

            var loader:URLLoader = new URLLoader();
            loader.load(url);



            //fileReference.save(byteArray, "Image.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;
        }
    }
}

Here the image is saved using file reference, which prompts the user to save. However i want that the image should be sent to asp.net page or server, so that i can get the image and save it to the database so can you guys guide me as to how i could proceed , Any suggestions are highly appreciated. Thanks

Upvotes: 0

Views: 991

Answers (1)

ToddBFisher
ToddBFisher

Reputation: 11610

you can do something like:

(untested code)

import flash.events.Event;
import flash.events.IOErrorEvent;

private function saveImage(e:MouseEvent):void
{
    byteArray = jpg.encode(bmd);

    //Get the URLRequest ready
    var jpgURLRequest:URLRequest = 
        new URLRequest("http://site.com/upload.aspx?args=here");        
    jpgURLRequest.requestHeaders.push(
        new URLRequestHeader("Content-type", "application/octet-stream"));
    jpgURLRequest.method = URLRequestMethod.POST;

    //Add jpg byte array to URL request
    jpgURLRequest.data = byteArray;

    //Get the URLLoader ready
    sendJPGLoader = new URLLoader(jpgURLRequest);
    sendJPGLoader.dataFormat = URLLoaderDataFormat.BINARY;
    sendJPGLoader.addEventListener(Event.COMPLETE, uploadJPGComplete);
    sendJPGLoader.addEventListener(IOErrorEvent.IO_ERROR, uploadJPGIOError);

    //Try to send image
    sendJPGLoader.load(jpgURLRequest);

}

And your event handlers:

private function uploadJPGComplete(evt:Event):void {
    //Handle server response via evt.target.data

    //Optionally do your stuff after uploading, if it makes sense here
    removeChild(bmp);
    saveButton.visible = false;
    discardButton.visible = false;
    capture.visible = true;
}

private function uploadJPGIOError(evt:IOErrorEvent):void {
    //Something went wrong while trying to talk to the server   
}

Of course you'll have to do the server half as well, but this should do what you need on the Flash client's end, with possibly some minor tweeking involved.

Upvotes: 1

Related Questions