如何在移动项目中使用Flex 4.5.1捕获图像?

时间:2011-11-28 13:03:57

标签: actionscript-3 flex flexbuilder

我是Flex和ActionScript编程的新手,并尽可能快地学习它。我尝试了很少的示例应用程序。现在,我试图通过PC附带的网络摄像头使用ActionScript捕获图像。

我写了以下代码......

             

        protected var myCam:CameraUI;

        protected function view1_viewActivateHandler(event:ViewNavigatorEvent):void
        {
            if (CameraUI.isSupported){
                currentState = "normal";
                myCam = new CameraUI();
                myCam.addEventListener(MediaEvent.COMPLETE, onComplete);
            }
            else currentState = "unsupported";
        }

        protected function btnPic_clickHandler(event:MouseEvent):void
        {
            img.filters = [];
            myCam.launch(MediaType.IMAGE);
        }

        protected function onComplete(evt:MediaEvent):void
        {
            img.source = evt.data.file.url;
        }

        protected function applyFilter():void
        {
            if (img.filters.length==0)
            {
                var matrixArray:Array = [.33,.33,.33,0,0,
                    .33,.33,.33,0,0,
                    .33,.33,.33,0,0,
                    0,0,0,1,0];
                var blackWhiteFilter:ColorMatrixFilter = new ColorMatrixFilter(matrixArray);
                img.filters = [blackWhiteFilter];
                btnBW.label = "COLOR";
            }
            else 
            {
                img.filters = [];
                btnBW.label = "B/W FILTER";
            }
        }
    ]]>
</fx:Script>

<s:states>
    <s:State name="normal"/>
    <s:State name="unsupported"/>
</s:states>

<s:layout>
    <s:VerticalLayout paddingTop="20" paddingBottom="20" paddingLeft="10" paddingRight="20" gap="40" 
                      horizontalAlign="center" verticalAlign="middle"/>
</s:layout>

<s:Label text="This device does not support Camera." width="95%" includeIn="unsupported"/>
<s:HGroup includeIn="normal">
    <s:Button id="btnPic" click="btnPic_clickHandler(event)" label="TAKE A PICTURE"/>
    <s:Button id="btnBW" click="applyFilter()" label="B/W FILTER" />
</s:HGroup>

<s:Image id="img" height="649" y="124" width="460" x="10" includeIn="normal"/>    

但没有检测到相机.. currentsState总是不受支持......有什么不对......

使用FlexMobileProject捕获移动设备的图像/视频的任何其他方式..?

任何与Flex Mobile Development相关的博客/教程都将提供帮助......谢谢......

1 个答案:

答案 0 :(得分:1)

来自文档: “使用Camera类从客户端系统或设备摄像头捕获视频。使用Video类在本地监视视频。”

您应该阅读的第一件事是关于Camera Class

然后查看Video Class

文档示例:

    package {
        import flash.display.Sprite;
        import flash.display.StageAlign;
        import flash.display.StageScaleMode;
        import flash.events.*;
        import flash.media.Camera;
        import flash.media.Video;

public class CameraExample extends Sprite {
    private var video:Video;

    public function CameraExample() {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;

        var camera:Camera = Camera.getCamera();

        if (camera != null) {
            camera.addEventListener(ActivityEvent.ACTIVITY, activityHandler);
            video = new Video(camera.width * 2, camera.height * 2);
            video.attachCamera(camera);
            addChild(video);
        } else {
            trace("You need a camera.");
        }
    }

    private function activityHandler(event:ActivityEvent):void {
        trace("activityHandler: " + event);
    }
}
}

看看你是否能让这一点工作,然后提出另一个关于过滤pic并保存它的问题。