将使用Flash捕获的图像传递到Asp.net页面

时间:2012-03-21 05:23:21

标签: flash actionscript

我正在使用ActionScript 3通过用户网络摄像头在我的Asp.net页面中捕获图像。这是我正在使用的代码

   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;
        }
    }
}

此处使用文件参考保存图像,提示用户保存。但是我想要将图像发送到asp.net页面或服务器,以便我可以获取图像并将其保存到数据库中,所以你们可以指导我如何继续, 任何建议都非常感谢。 感谢

1 个答案:

答案 0 :(得分:1)

你可以这样做:

(未经测试的代码)

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);

}

你的事件处理程序:

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   
}

当然你也必须做一半的服务器,但这应该在Flash客户端端做你需要的,可能还有一些小的推文。