实例包含摄像头打开、关闭、侦测等功能,源代码如下:
<?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"
creationComplete="application1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import flash.media.Video;
import mx.events.FlexEvent;
import mx.utils.StringUtil;
private var _video:Video = null;
private var camera:Camera = null;
private var timerid:uint;
private var isBusyCamera:Boolean = false;
private var times:uint=0;
protected function button1_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
if(videoDisplay.numChildren == 2){
trace(videoDisplay.numChildren);
if(videoDisplay.numChildren == 2){
videoDisplay.removeChildAt(1);
}
trace(videoDisplay.numChildren);
_video.clear();
_video.attachCamera(null);
_video = null;
camera = Camera.getCamera(null);
camera = null;
}
}
protected function button2_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
trace("Camera.isSupported: ",Camera.isSupported);
trace("Camera.names.length",Camera.names.length);
if(camera == null){
if(Camera.isSupported && Camera.names.length >0){
camera = Camera.getCamera();
}
}
trace(camera);
if (camera) {
camera.addEventListener(ActivityEvent.ACTIVITY, camera_activity);
camera.addEventListener(StatusEvent.STATUS, camera_status);
if(_video == null){
_video = new Video();
}
_video.attachCamera(camera);
timerid=setInterval(callback, 2000);
_video.width = videoDisplay.width;
_video.height = videoDisplay.height;
trace(videoDisplay.numChildren);
videoDisplay.addChild(_video);
trace(videoDisplay.numChildren);
} else {
trace("You don't seem to have a camera.");
}
}
private function callback():void
{
trace("currentFPS=" + camera.currentFPS.toString());
if (camera.currentFPS > 0)
{
//视频设备可用
clearInterval(this.timerid);
this.isBusyCamera=false;
trace("摄像头正常");
}
else
{
times++;
trace("times=" + times.toString());
if (times > 2)
{
//视频设备忙
clearInterval(timerid);
this.isBusyCamera=true;
trace("摄像头被占用");
}
}
}
private function camera_activity(evt:ActivityEvent):void {
var str:String = "[{0}] activating:{1}\n";
textArea.text += StringUtil.substitute(str,
evt.type,
evt.activating);
}
private function camera_status(evt:StatusEvent):void {
var str:String = "[{0}] code:'{1}', level:'{2}'\n";
textArea.text += StringUtil.substitute(str,
evt.type,
evt.code,
evt.level);
switch (evt.code) {
case "Camera.Muted":
trace("User denied access to camera.");
break;
case "Camera.Unmuted":
trace("User allowed access to camera.");
break;
}
}
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:VGroup>
<s:Panel x="53" y="26" width="394" height="290" title="My Camera">
<s:VideoDisplay id="videoDisplay" x="10" y="10" width="371" height="238"/>
</s:Panel>
<s:Button label="停止" click="button1_clickHandler(event)"/>
<s:Button label="开始" click="button2_clickHandler(event)"/>
<s:TextArea id="textArea" width="100%" height="{videoDisplay.height}" editable="false"
verticalScrollPolicy="on"/>
</s:VGroup>
</s:Application>
本文提供了一个使用Flex进行摄像头操作的示例代码,包括摄像头的开启、关闭及状态侦测等功能。通过实例展示了如何利用Flash Media API实现摄像头视频流的获取与处理。
593

被折叠的 条评论
为什么被折叠?



