天天看點

Flex(flash)檢測攝像頭的3種狀态(是否被占用,沒安裝攝像頭,正常)

在視訊程式的編寫過程中,我們經常要使用攝像頭,在使用攝像頭前有必要對攝像頭的現有狀态做個檢測:

1.被占用

2.沒安裝攝像頭

3.正常

看下面代碼:

<?xml version="1.0" encoding="utf-8"?> 

    layout="absolute" creationComplete="init()">

    <mx:Script> 

        <![CDATA[

            import mx.controls.Alert; 

            private var camera:Camera; 

            private var isExistsCamera:Boolean=false; 

            private var isBusyCamera:Boolean=false; 

            private var intervalId:uint; 

            private var times:int;

            private function init():void 

            { 

                camera=Camera.getCamera(); 

               if (camera == null) 

                { 

                   if (Camera.names.length <= 0) 

                    { 

                        this.isExistsCamera=false; 

                        Alert.show("沒安裝攝像頭"); 

                    } 

                } 

                else 

                    this.vdpaly.attachCamera(this.camera); 

                    this.isCameraBusy(); 

            }

            private function isCameraBusy():void 

                this.intervalId=setInterval(callback,50); 

            private function callback():void 

                trace("currentFPS=" + camera.currentFPS.toString());

               if (camera.currentFPS > 0) 

                    //視訊裝置可用 

                    clearInterval(this.intervalId); 

                    this.isBusyCamera=false; 

                    Alert.show("攝像頭正常"); 

                    times++; 

                    trace("times=" + times.toString());

                    if (times > 30) 

                        //視訊裝置忙 

                        clearInterval(intervalId); 

                        this.isBusyCamera=true; 

                        Alert.show("攝像頭被占用"); 

             private function cl():void 

                Alert.show(this.camera.currentFPS.toString()); 

        ]]> 

    </mx:Script>

    <mx:VideoDisplay   id="vdpaly"  click="cl()" x="44" y="64" width="232" height="236"/>

</mx:WindowedApplication>

代碼說明:

camera == null,那麼就是沒安裝攝像頭

如果攝像頭被占用,那麼camera.currentFPS 肯定不會大 于0,而是等于0

    本文轉自 OldHawk  部落格園部落格,原文連結:http://www.cnblogs.com/taobataoma/archive/2010/12/05/1896747.html,如需轉載請自行聯系原作者