読者です 読者をやめる 読者になる 読者になる

かわいいサンタと2ショットが撮れるアプリ

AIR Android Flex

12月に入ったということもあり、クリスマスな季節!!
っということで、サンタさんと写真が撮れるアプリを公開しました。

アンドロイドマーケットで「サンタ」で検索すると出るかと思います。
雪のアイコンをタップすると雪が降ります。
今回作成していて、どうにもJPGエンコードとSDカードへの保存が遅くてなんとかなならないかなぁって思います。この部分

stream.openAsync( file ,FileMode.WRITE );
img = jpge.encode(bmd);
stream.writeBytes(img);

すべてのソースはこちら。

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        xmlns:local="*"
        chromeColor="#FC0000" creationComplete="view1_creationCompleteHandler(event)"
        title="萌サンタカメラ">
    <fx:Script>
        <![CDATA[
            import com.flashandmath.dg.display.SnowDisplayAlt;
            import flash.net.navigateToURL;
            import mx.events.FlexEvent;
            import mx.graphics.codec.JPEGEncoder;
            import spark.events.IndexChangeEvent;
            [Embed(source='assets/1.png')]private var image_1:Class;
            [Embed(source='assets/2.png')]private var image_2:Class;
            [Embed(source='assets/3.png')]private var image_3:Class;
            [Embed(source='assets/4.png')]private var image_4:Class;
            [Embed(source='assets/5.png')]private var image_5:Class;
            [Embed(source='assets/6.png')]private var image_6:Class;
            [Embed(source='assets/7.png')]private var image_7:Class;
            [Embed(source='assets/8.png')]private var image_8:Class;
            private var cam:Camera;
            private var stream:FileStream;
            private var file:File;
            private var fname:String;
            private var bmd:BitmapData   = new BitmapData( 480 , 640 );
            private var img:ByteArray    = new ByteArray();
            private var jpge:JPEGEncoder = new JPEGEncoder(80);
            protected function view1_creationCompleteHandler(event:FlexEvent):void
            {
                //カメラの作成
                if( Camera.isSupported ){
                    cam = Camera.getCamera();
                    cam.setMode(640,480,10);
                    var svd:StageVideo = new StageVideo();
                    var vd:Video =  new Video(cam.width,cam.height);
                    vd.rotation = 90;
                    vd.x = 480;
                    vd.attachCamera(cam);
                    myUI.addChild(vd);
                    stream = new FileStream();
                    stream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS , onComp );
                    stream.addEventListener(IOErrorEvent.IO_ERROR , onIoError );
                    previewImage.addEventListener(FlexEvent.UPDATE_COMPLETE , onImageLoaded );
                }
            }
            //カメラのキャプチャ
            protected function onCaptureClick(e:Event):void{
                mySound2.play();
                previewImage.source = null;
                bmd.draw(myGroup);
                previewImage.source = new Bitmap( bmd );
            }
            //キャプチャ完了イベント
            protected function onImageLoaded( e:Event ):void{
                previewGroup.visible = true;
                saveBtn.enabled      = true;
                cancelBtn.enabled    = true;
            }
            //画像保存ボタンクリック
            protected function saveBtn_clickHandler(event:MouseEvent):void
            {
                saveBtn.enabled      = false;
                cancelBtn.enabled    = false;
                nowSaving.visible    = true;
                previewGroup.visible = false;
                //ファイル名の作成
                var date:Date = new Date();
                fname = "" + date.getFullYear() + "-"
                    + date.getMonth() + "-"
                    + date.getDay() + "-"
                    + date.getHours() + "-"
                    + date.minutes + "-"
                    + date.getSeconds() + ".jpg";
                //ファイルストリームの作成
                //file = File.desktopDirectory.resolvePath( fname );
                file = File.documentsDirectory.resolvePath( fname );
                stream.openAsync( file ,FileMode.WRITE );
                img = jpge.encode(bmd);
                stream.writeBytes(img);
            }
            private function onComp( e:Event ):void{
                nowSaving.visible    = false;
                stream.close();
            }
            private function onIoError( e:Event ):void{
                nowSaving.visible    = false;
                stream.close();
            }
            //キャンセルボタンクリック
            protected function cancelBtn_clickHandler(event:MouseEvent):void
            {
                myGroup.visible   = true;
                nowSaving.visible = false;
                previewGroup.visible = false;
                mySound3.play();
            }
            private var image_i:int = 1;
            protected function myImage_clickHandler(event:MouseEvent):void
            {
                myFade.play([myImage]);
                mySound4.play();
                image_i ++;
                if( image_i > 8 ){
                    image_i = 1;
                }
                myImage.source = new this['image_' + image_i ]();
            }
            private var snow:SnowDisplayAlt;
            private var frame:Shape;
            protected function makeSnow():void{
                snow = new SnowDisplayAlt();
                snow.maxParticleSize = 30;
                snow.minParticleSize = 10;
                snow.x = 10;
                snow.y = 10;
                snow.width = 700;
                snow.height = 600;
                snow.perlinBitmapFront.y = snow.y + snow.display.displayHeight + 2;
                snow.perlinBitmapFront.x = snow.x;
                snow.perlinBitmapBack.y  = snow.y + snow.display.displayHeight + 2;
                snow.perlinBitmapBack.x  = snow.x + snow.perlinBitmapFront.width + 2;
                myUI2.addChild(snow);
            }
            protected function snowBtn_clickHandler(event:MouseEvent):void
            {
                if( snowBtn.alpha != 1){
                    makeSnow();
                    snowBtn.alpha = 1;
                }else{
                    myUI2.removeChild( snow );
                    snowBtn.alpha = 0.8;
                }
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <!---->
        <mx:SoundEffect id="mySound2" source="assets/shatter.mp3" useDuration="false" target="{this}"/>
        <mx:SoundEffect id="mySound3" source="assets/03.mp3" useDuration="false" target="{this}"/>
        <mx:SoundEffect id="mySound4" source="assets/04.mp3" useDuration="false" target="{this}"/>
        <!---->
        <s:Parallel id="myWipe" duration="500">
            <s:Fade alphaFrom="0" alphaTo="1" duration="500"/>
            <s:Move yFrom="600" yTo="40" duration="500"/>
        </s:Parallel>
        <s:Parallel id="myWipeHide" target="{previewGroup}" effectEnd="{previewGroup.visible = false;}">
            <s:Fade alphaFrom="1" alphaTo="0" duration="1000"/>
            <s:Move yFrom="0" yTo="{-this.height}"/>
        </s:Parallel>
        <s:Parallel id="myWipeHide2" target="{previewGroup}" effectEnd="{previewGroup.visible = false;}">
            <s:Fade alphaFrom="1" alphaTo="0" duration="1000"/>
        </s:Parallel>
        <s:Fade id="myFade" duration="1000" alphaFrom="0" alphaTo="1"/>
    </fx:Declarations>
    <!---->
    <s:Rect width="100%" height="100%">
        <s:fill>
            <s:LinearGradient>
                <s:GradientEntry color="0xff0000" ratio="0"/>
                <s:GradientEntry color="0x330000" ratio="1"/>
            </s:LinearGradient>
        </s:fill>
    </s:Rect>
    <!---->
    <s:Group id="myGroup" width="480" height="640" y="40" clipAndEnableScrolling="true" hideEffect="Fade" showEffect="Fade">
        <mx:UIComponent id="myUI"/>
        <s:Image x="8" y="0" source="assets/bg1.png"/>
        <s:Image id="myImage" bottom="0" right="0" click="myImage_clickHandler(event)" creationCompleteEffect="{myFade}" source="@Embed('assets/1.png')"/>
        <mx:UIComponent id="myUI2"/>
    </s:Group>
    <s:Image x="107" y="520" click="onCaptureClick(event)" source="assets/cameraBtn.png"/>
    <s:Image id="snowBtn" x="10" y="19" alpha="0.8" click="snowBtn_clickHandler(event)" source="@Embed('assets/snowBtn.png')"/>
    <!---->
    <s:Group id="previewGroup" showEffect="{myWipe}" width="480" height="640" visible="false" y="40">
        <s:Image id="previewImage" width="100%" height="100%"/>
        <s:Button id="saveBtn" icon="@Embed('assets/Checked.png')"   x="9"  y="521" width="219" label="保存" click="saveBtn_clickHandler(event)"/>
        <s:Button id="cancelBtn" x="256" y="521" width="214" label="キャンセル"
                  click="cancelBtn_clickHandler(event)" icon="@Embed('assets/Delete.png')"/>
    </s:Group>
    <s:Button id="nowSaving" label="保存中" visible="false" verticalCenter="0" horizontalCenter="0"/>
</s:View>