1. 程式人生 > >flex 4 攝像頭拍照

flex 4 攝像頭拍照


<?xml version="1.0" encoding="utf-8"?>  
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
               xmlns:s="library://ns.adobe.com/flex/spark"   
               xmlns:mx="library://ns.adobe.com/flex/mx"   
               minWidth="955" minHeight="600">  
    <fx:Script>  
        <![CDATA[  
            import mx.events.CloseEvent;  
            import mx.rpc.events.FaultEvent;  
            import mx.rpc.events.ResultEvent;  
            import mx.controls.Alert;  
              
            private static const DEFAULT_CAMERA_WIDTH:Number = 320;  
            private static const DEFAULT_CAMERA_HEIGHT:Number = 240;  
              
            //定義一個攝像頭  
            private var camera:Camera;   
            //定義一個本地視訊  
            private var localVideo:Video;   
            //定義視訊截圖  
            private var videoBitmapData:BitmapData;   
              
              
            //初始化攝像頭  
            private function initCamera():void  
            {  
                //開啟攝像頭  
                camera = Camera.getCamera("0");  
                if(camera == null && Camera.names.length <= 0                                                             )  
                {  
                    Alert.show("沒有找到攝像頭,是否重新查詢!", "提示", Alert.OK|Alert.NO, this, onInitCamera);  
                    return;  
                }  
                  
                camera.addEventListener(StatusEvent.STATUS,onCameraStatusHandler);  
                //將攝像頭的捕獲模式設定為最符合指定要求的本機模式.  
                camera.setMode(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT,30);  
                //設定每秒的最大頻寬或當前輸出視訊輸入訊號所需的畫面質量  
                camera.setQuality(144,85);  
                localVideo = new Video();  
                localVideo.width = DEFAULT_CAMERA_WIDTH;  
                localVideo.height = DEFAULT_CAMERA_HEIGHT;  
                //正在捕獲視訊資料的 Camera 物件  
                localVideo.attachCamera(camera);  
                cameraVideo.addChild(localVideo);  
                //USB 視訊裝置  
                trace(Camera.names.length+"");  
            }  
              
            //拍照按鈕事件,進行視訊截圖  
            private function snapshotPicture():void  
            {  
                videoBitmapData = new BitmapData(DEFAULT_CAMERA_WIDTH,DEFAULT_CAMERA_HEIGHT);  
                videoBitmapData.draw(cameraVideo,new Matrix());  
                  
                var videoBitmap:Bitmap = new Bitmap(videoBitmapData);  
                imgPicture.addChild(videoBitmap);  
//              imgPicture.source = videoBitmap;  
            }  
              
              
            //檢測攝像頭許可權事件  
            private function onCameraStatusHandler(event:StatusEvent):void  
            {  
                if(!camera.muted)  
                {  
                    shootingBtn.enabled = true;  
                }  
                else  
                {  
                    Alert.show("無法連結到活動攝像頭,是否重新檢測,請充許使用攝像頭!", "提示", Alert.OK|Alert.NO, this, onInitCamera);  
                }  
                camera.removeEventListener(StatusEvent.STATUS, onCameraStatusHandler);  
            }  
              
            //當攝像頭不存在,或連線不正常時重新獲取  
            private function onInitCamera(event:CloseEvent):void  
            {  
                if(event.detail == Alert.OK)  
                {  
                    initCamera();  
                }  
            }  
              
        ]]>  
    </fx:Script>  
      
    <s:Group x="10" y="10" height="600" width="800">  
        <s:Panel title="視訊拍照" backgroundColor="#00ffffff" cornerRadius="0" width="340" height="325" >  
            <s:Group width="320" height="240">  
                <s:VideoDisplay x="0" y="0" width="320" height="240" id="cameraVideo" />  
            </s:Group>  
            <s:controlBarContent>  
                <s:Button id="shootingBtn" label="拍照" click="snapshotPicture()"/>  
                <s:Button id="connectionBtn" label="連線" click="initCamera()"/>  
            </s:controlBarContent>  
        </s:Panel>  
          
        <s:Panel title="拍照圖片" x="360" y="10" width="180" height="200" >  
            <s:Image id="imgPicture" x="0" y="0" width="160" height="120" />  
        </s:Panel>  
    </s:Group>  
</s:Application>