将图片直接上传到服务器

时间:2010-12-20 13:02:46

标签: html flex actionscript

在以下链接中http://www.tuttoaster.com/create-a-camera-application-in-flash-using-actionscript-3/如何从网络摄像头拍照后直接将图片上传到服务器

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;

public class caml 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();

    public function caml()
    {
        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);
            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;
        }


}
 }

1 个答案:

答案 0 :(得分:2)

FileReference.upload()和FileReference.download()函数是非阻塞的。在文件传输完成之前,这些函数在调用后返回。此外,如果FileReference对象超出范围,则在离开作用域时将取消尚未在该对象上完成的任何上载或下载。因此,只要可以继续上传或下载,请确保您的FileReference对象将保留在范围内。 http://help.adobe.com/en_US/AS2LCR/Flash_10.0/help.html?content=00001063.html