Webカメラを使ってみた

Webカメラを使って画像処理してみました。


こんな感じ
(Webカメラ必須)


結構簡単にカメラが使えるものなんですね。


ソース

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            private var camera:Camera;
            private var filterAry:Array = [0,0,0,0,0,0,0,0,0];
            private var matX:int = 3;
            private var matY:int = 3;
            private var div:int = 1;
            private var bi:int = 0;
            private var useFilter:Boolean = false;
            private var filter:ConvolutionFilter;
        
            private function init():void{
                camera = Camera.getCamera();
                if(camera){
                    camera.setQuality(0,80);
                    camera.setMode(320, 240, 30, true);
                    camera.setMotionLevel(0);
                      camera.addEventListener(ActivityEvent.ACTIVITY, onActive);
                    video.attachCamera(camera);
                }
            }
            
            private function apply():void{
                filterAry[0] = array00.value;
                filterAry[1] = array01.value;
                filterAry[2] = array02.value;
                filterAry[3] = array10.value;
                filterAry[4] = array11.value;
                filterAry[5] = array12.value;
                filterAry[6] = array20.value;
                filterAry[7] = array21.value;
                filterAry[8] = array22.value;
                matX = matrixX.value;
                matY = matrixY.value;
                div = divisor.value;
                bi = bias.value;
                filter = new ConvolutionFilter(matX, matY, filterAry, div, bi);
                useFilter = true;
            }
            
            private function normal():void{
                useFilter = false;
            }
            
            private function onActive(e:ActivityEvent):void
            {
                addEventListener(Event.ENTER_FRAME, onFrame);
            }
            
            private function onFrame(event:Event):void{
                var bmpData:BitmapData = new BitmapData(video.width, video.height);
                bmpData.draw(video);
                if(useFilter){
                    bmpData.applyFilter(bmpData, bmpData.rect, new Point(0,0), filter);
                }
                image.source = new Bitmap(bmpData);
            }
            
            
            private var edge_ary:Array = [-1,0,1,-2,0,2,-1,0,1];
            private var sharp_ary:Array = [0,-1,0,-1,5,-1,0,-1,0];
            private var embos_ary:Array = [-1,0,1,0,0,0,0,0,0];
            
            private function box_change():void{
                switch(box.selectedLabel){
                    case "エッジ抽出":
                    array00.value = edge_ary[0];
                    array01.value = edge_ary[1];
                    array02.value = edge_ary[2];
                    array10.value = edge_ary[3];
                    array11.value = edge_ary[4];
                    array12.value = edge_ary[5];
                    array20.value = edge_ary[6];
                    array21.value = edge_ary[7];
                    array22.value = edge_ary[8];
                    matrixX.value = 3;
                    matrixY.value = 3;
                    divisor.value = 8;
                    bias.value = 0;
                    break;
                    case "鮮鋭化":
                    array00.value = sharp_ary[0];
                    array01.value = sharp_ary[1];
                    array02.value = sharp_ary[2];
                    array10.value = sharp_ary[3];
                    array11.value = sharp_ary[4];
                    array12.value = sharp_ary[5];
                    array20.value = sharp_ary[6];
                    array21.value = sharp_ary[7];
                    array22.value = sharp_ary[8];
                    matrixX.value = 3;
                    matrixY.value = 3;
                    divisor.value = 1;
                    bias.value = 0;
                    break;
                    case "エンボス処理":
                    array00.value = embos_ary[0];
                    array01.value = embos_ary[1];
                    array02.value = embos_ary[2];
                    array10.value = embos_ary[3];
                    array11.value = embos_ary[4];
                    array12.value = embos_ary[5];
                    array20.value = embos_ary[6];
                    array21.value = embos_ary[7];
                    array22.value = embos_ary[8];
                    matrixX.value = 3;
                    matrixY.value = 1;
                    divisor.value = 4;
                    bias.value = 128;
                    break;
                }
            }

        ]]>
    </mx:Script>
    <mx:VideoDisplay x="32" y="69" width="414" height="316" id="video" visible="false"/>
    <mx:Image x="32" y="69" width="414" height="316" id="image"/>
    <mx:Panel x="508" y="56" width="414" height="333" layout="absolute">
        <mx:Button x="329" y="266" label="適用" id="apply_btn" click="apply()"/>
        <mx:NumericStepper x="10" y="123" width="46" id="array00" stepSize="1" value="0" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="10" y="153" width="46" id="array10" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="10" y="183" width="46" id="array20" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="247" y="209" width="44" id="matrixX" value="3" minimum="1" maximum="3" stepSize="1"/>
        <mx:NumericStepper x="329" y="209" width="44" id="matrixY" value="3" minimum="1" maximum="3" stepSize="1"/>
        <mx:NumericStepper x="243" y="123" width="44" id="divisor" value="1" minimum="1" stepSize="1" maximum="100"/>
        <mx:NumericStepper x="325" y="123" width="44" id="bias" value="0" minimum="0" stepSize="1" maximum="255"/>
        <mx:NumericStepper x="118" y="183" width="46" id="array22" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="118" y="153" width="46" id="array12" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="118" y="123" width="46" id="array02" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="64" y="183" width="46" id="array21" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="64" y="153" width="46" id="array11" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:NumericStepper x="64" y="123" width="46" id="array01" value="0" stepSize="1" minimum="-10" maximum="10"/>
        <mx:Label x="10" y="38" width="133" text="parameters" fontWeight="bold"/>
        <mx:Label x="10" y="97" width="133" text="matrix"/>
        <mx:Label x="10" y="10" width="133" text="ConvolutionFilter" fontSize="12" fontWeight="bold"/>
        <mx:Label x="243" y="183" text="matrixX"/>
        <mx:Label x="325" y="183" text="matrixY"/>
        <mx:Label x="243" y="97" text="divisor"/>
        <mx:Label x="325" y="97" text="bias"/>
        <mx:Button x="265" y="266" label="解除" click="normal()"/>
        <mx:ComboBox x="224" y="10" change="box_change()" id="box">
            <mx:dataProvider>
                <mx:Object label="テンプレ選択"/>
                <mx:Object label="エッジ抽出"/>
                <mx:Object label="エンボス処理"/>
                <mx:Object label="鮮鋭化"/>
            </mx:dataProvider>
        </mx:ComboBox>
    </mx:Panel>
</mx:Application>