<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:net="flash.net.*" 
    layout="vertical" fontSize="12" viewSourceURL="srcview/index.html">
    
    <mx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.utils.XMLUtil;
            import mx.utils.ObjectProxy;
            import mx.collections.XMLListCollection;
            
            import mx.core.UIComponent;            
               import com.jooce.net.uploadFile;   
               import com.adobe.images.JPGEncoder;  
            import mx.rpc.events.AbstractEvent;
            
            private var file:FileReference = new FileReference();
            private var bitmapData:BitmapData;
            
            /*啟用並偵測sWebcam*/
            private function videoDisplay_creationComplete():void {
                var camera:Camera = Camera.getCamera();
                if (camera) {
                    videoDisplay.attachCamera(camera);
                } else {
                   Alert.show("偵測不到webCam");
                }
            }
            
            /*快照*/
            private function takePhoto():void{
                bitmapData = new BitmapData(320, 240);
                var bmp:Bitmap = new Bitmap(bitmapData);
                var holder:UIComponent = new UIComponent();
                holder.addChild(bmp);
                                
                //若要拍整個場景就改用bitmapData.draw(stage)                
                bitmapData.draw(videoDisplay);
                swfLoader.source = new Bitmap(bitmapData);
            }
            
            /*上傳儲存 */                    
            private function savejpg():void{
                //若要拍整個場景就改用bitmapData.draw(stage)               
                bitmapData.draw(videoDisplay);
                var loader: URLLoader = new URLLoader();                    
                loader.addEventListener( Event.COMPLETE, onURLLoaderComplete );   
                loader.addEventListener( IOErrorEvent.IO_ERROR, onURLLoaderFailure );    
                
                //把bitmapdata用JPGEncoder轉為JPG格式,JPGEncoder(圖片品質),再丟到ByteArray   
                var byteArray:ByteArray = new JPGEncoder(85).encode(bitmapData);   
               
                //產生流水號檔名   
                  var filename:String=(Math.floor(Math.random()*10000000*10)>>0).toString();
                   filename=filename+".jpg";
               
               //把byteArray用uploadFile()上傳到後端並產生實體檔案
               uploadFile(loader, 'http://localhost:1625/src/savefile.aspx', byteArray, filename);   
            }   
            
             private  function onURLLoaderComplete(evt:Event):void{
                   Alert.show("上傳成功");   
              }   
               private  function onURLLoaderFailure(evt:Event):void{
                   Alert.show("上傳失敗");
               } 

            
        ]]>
    </mx:Script>
    <mx:ApplicationControlBar width="691">
        <mx:Button id="button"
                label="重新偵測webCam"
                click="videoDisplay_creationComplete();" />
         <mx:Button label="快照" click="takePhoto()"/>
            <mx:Button label="儲存" click="savejpg()"/>
            <mx:Spacer width="100%"/>
            <mx:Label text="Flex Webcam快照【http://www.minwt.com】"/>
    </mx:ApplicationControlBar>
    
    <mx:HBox>
        <mx:Panel title="webCam" width="340" height="280"
             verticalScrollPolicy="off" horizontalScrollPolicy="off">
             <mx:VideoDisplay id="videoDisplay"
                creationComplete="videoDisplay_creationComplete();"
                width="320"
                height="240" />    
         </mx:Panel>
          <mx:Panel title="快照結果" width="340" height="280"
               verticalScrollPolicy="off" horizontalScrollPolicy="off">         
             <mx:SWFLoader id="swfLoader"/>    
         </mx:Panel>         
     </mx:HBox>
              
</mx:Application>