package com.sohu.sns.avatar
{
    import flash.display.*;
    import flash.events.*;
    import flash.media.*;
    import flash.system.*;

    public class CameraSnapper extends Sprite implements IEditSource
    {
        private var os:String;
        private var camera:Camera;
        private var bitmapData:BitmapData;
        private var video:Video;
        public static var NOCAMERA:String = "nocamera";
        public static const MAC_CAM:String = "USB Video Class Video";

        public function CameraSnapper()
        {
            return;
        }// end function

        public function getEditSource() : void
        {
            if (this.camera)
            {
                this.bitmapData = new BitmapData(this.video.width, this.video.height);
                this.bitmapData.draw(this.video, null, null, null, null, true);
            }
            return;
        }// end function

        private function connectCamera() : void
        {
            this.camera.setMode(640, 480, 21);
            this.camera.setQuality(0, 100);
            this.video.attachCamera(this.camera);
            return;
        }// end function

        public function init() : void
        {
            this.os = Capabilities.manufacturer;
            this.video = new Video();
            addChild(this.video);
            this.initCamera();
            return;
        }// end function

        public function get editData() : BitmapData
        {
            return this.bitmapData;
        }// end function

        private function initCamera() : void
        {
            var _loc_1:String = null;
            var _loc_2:* = undefined;
            if (this.os == "Adobe Macintosh")
            {
                for (_loc_2 in Camera.names)
                {
                    
                    if (Camera.names[_loc_2] == MAC_CAM)
                    {
                        _loc_1 = _loc_2.toString();
                        break;
                    }
                }
                this.camera = Camera.getCamera(_loc_1);
            }
            else
            {
                this.camera = Camera.getCamera();
            }
            if (this.camera != null && Camera.names.length >= 1)
            {
                this.connectCamera();
            }
            else
            {
                this.dispatchEvent(new Event(NOCAMERA));
            }
            return;
        }// end function

    }
}
