AS3 - 将网络摄像头视频附加到动画片段

时间:2011-12-30 17:13:26

标签: flash actionscript-3 actionscript

我的任务是编写一个Flash应用程序代码,让用户可以使用网络摄像头拍摄快照,将其附加到动画片段,并通过添加菜单上提供的头发,帽子,眼镜等来修改自画像。

我只是想修改两个很棒的脚本:对于网络摄像头http://www.riacodes.com/flash/captures-images-from-the-webcam-and-save-them-to-the-desktop/,拖放功能使用免费变换工具http://blog.ryantan.net/2010/08/free-transform-manager/

当我将它用作文档类时,我设法让网络摄像头正常工作。当我更改我的代码以便Free Transform Tool代码在文档类中并且webcam脚本在Webcamactions.as文件中时,我无法再控制动画片段...动画片段(例如“imagecanvas”我附加了视频)。我的Mac的网络摄像头指示灯表示该功能有效,但视频没有出现在动画片段中。

文档类---------- TestFTM.as

package {
    import Webcamactions;
    import com.ryan.geom.FreeTransformEvent;
    import com.ryan.geom.FreeTransformManager;

    import fl.controls.CheckBox;
    import fl.motion.easing.Circular;
    import fl.motion.MatrixTransformer;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.text.TextField;
    import flash.ui.Mouse;

    public class TestFTM extends MovieClip {

        public var myVar:Webcamactions = new Webcamactions();

        private static var _instance:TestFTM;

        public static function get instance():TestFTM {
            return _instance;
        }

        public var fts:FreeTransformManager;

        public function TestFTM():void {
            _instance = this;

            // Set up FreeTransformManager
            fts = new FreeTransformManager(false);

            // Register objects
            fts.registerSprite(box, { minW:50, maxW:500 } );

        }
    }
}

WEBCAM CLASS ------- Webcamactions.as

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.display.MovieClip;

    public class Webcamactions extends MovieClip
    {
        public var camera:Camera = Camera.getCamera();
        public var video:Video = new Video();
        public var bmd:BitmapData = new BitmapData(400,480);
        public var bmp:Bitmap;
        public var fileReference:FileReference = new FileReference();
        public var byteArray:ByteArray;
        public var jpg:JPGEncoder = new JPGEncoder();

        public var nowebcam:MovieClip = new MovieClip();
        public var capture:MovieClip = new MovieClip();
        public var imagecanvas:MovieClip = new MovieClip();
        public var mask_object:MovieClip = new MovieClip();

        public function Webcamactions():void {
            nowebcam.visible = false;

            capture.addEventListener(MouseEvent.MOUSE_UP, captureImage);

            if (camera != null)
            {

                camera.setMode(640,480,30,false); // setMode(videoWidth, videoHeight, video fps, favor area)

                video.smoothing = true;
                video.attachCamera(camera);
                video.width = 640;
                video.height = 480;
                video.x = -120;
                video.y = 0;
                imagecanvas.addChild(video);
            }
            else
            {
                nowebcam.visible = true;    
                trace("No Camera Detected");
            }
        }

        public function captureImage(e:MouseEvent):void
        {
            bmd.draw(video);

            while (imagecanvas.numChildren) {
                imagecanvas.removeChildAt (0);
            }

            bmp = new Bitmap(bmd);
            trace(bmp.width);
            trace(bmp.height);
            bmp.width=800;
            bmp.height=960;
            bmp.x = -120;

            imagecanvas.addChild(bmp);
            capture.visible = true;

        }
    }

我尽量保持清醒,如果有人能给我一些如何继续的提示,我真的很感激。提前谢谢。

1 个答案:

答案 0 :(得分:-1)

我可能在这里愚蠢,但它看起来不像你在webcamactions中的显示列表中添加imagecanvas。

此外,当您不需要时间轴时,最好在MovieClip上使用Sprites。