« The Laughing Man Hacks You | メイン | Flex Module for Apache and IIS »

Camera Puzzle

del.icio.us it!   hatena bookmark

とあるところで、とある人にWPFのデモを見せていただいた。
さすがWindowsネイティブで動いてるだけあってすげーね。
3Dもいいけど、テキストの扱いがすばらしい。Flashでもあれくらいうまくテキストが扱えるようになればいいのに。

見せてもらった物に対して、これFlashでも出来ますよ的な発言をたくさんしてしまったので、作ってみました第一弾!
私のうっとうしい発言に付き合ってくれた、とある人、ありがとうございます。

WPFでビデオを分割してパズルにしてたので、おなじよーなものを作ってみた。
パズルのロジックがメンドーだったのでビデオを分割してD&D出来るようにしてみた。
左側がソースの動画。右側が分割した動画。


Camera Puzzle
http://labs.un-q.net/as3/camerapuzzle/index.html


カメラがある場合はカメラの動画を、ない場合はFLVを再生します。

なんかyoutubeのFLV直接読み込んでcopyPixelsとかしようと思ったら怒られた。
どうやらセキュリティの問題みたいね。

あとFLVの再生がたまに途中で止まる。
これはよくわからん。

次は3D系に挑戦かな

そーす


package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.*;
import flash.media.Camera;
import flash.media.Video;
import flash.utils.ByteArray;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.net.NetConnection;
import flash.net.NetStream;

public class Puzzle extends Sprite {

private var video:Video;
private var camera:Camera;
private var rowCount:int;
private var lineCount:int;

private var videobmd:BitmapData;
private var videobm:Bitmap;

private var con:NetConnection;

private const PIECE_ROW:int = 5;
private const PIECE_LINE:int = 5;
private const FLV:String = "./video.flv";


public function Puzzle() {

trace("------- start Puzzle ---------");

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

// camera
camera = Camera.getCamera();

if (camera != null) {
setupCamera();
trace("camera");
} else {
setupNetStream();
trace("FLV");
}

}


private function setupCamera():void {
video = new Video(camera.width * 2, camera.height * 2);
video.attachCamera(camera);
addChild(video);
onSetup();
}


private function setupNetStream():void {
con = new NetConnection();
con.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
con.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
con.connect(null);
}


private function netStatusHandler(e:NetStatusEvent):void {
trace(e.info.code);
switch (e.info.code) {
case "NetConnection.Connect.Success":
connectStream();
break;
case "NetStream.Play.StreamNotFound":
trace("Unable to locate video : " + FLV);
break;
}
}


private function connectStream():void {
var stream:NetStream = new NetStream(con);
stream.checkPolicyFile = true;
stream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
video = new Video();
video.attachNetStream(stream);
stream.play(FLV);
addChild(video);
onSetup();
}

private function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler: " + event);
}

private function asyncErrorHandler(event:AsyncErrorEvent):void {
// ignore AsyncErrorEvent events.
}


private function onSetup():void {

var w:int = video.width / PIECE_ROW;
var h:int = video.height / PIECE_LINE;

rowCount = 0;
lineCount = 0;

for (var i:int; i createPiece(i,w,h);
}

videobmd = new BitmapData(video.width, video.height ,false,0xaaaaaa);
videobm = new Bitmap(videobmd);

addEventListener(Event.ENTER_FRAME, btmCapture);
}


private function createPiece(c:int, w:int, h:int):void {

var tmpx:int;
var tmpy:int;
var bm:Bitmap;
var bmd:BitmapData;
var bmContainer:Sprite;

bmd = new BitmapData(w,h,false,0xaaaaaa);
bm = new Bitmap(bmd);
bmContainer = new Sprite();

addChild(bmContainer);
bmContainer.addChild(bm);

if (rowCount >= PIECE_ROW) {
lineCount++;
rowCount = 0;
}

tmpx = w*rowCount;
tmpy = h*lineCount;

var capture:Function = function(e:Event):void {
bmd.copyPixels(videobmd, new Rectangle(tmpx,tmpy,w,h), new Point(0,0));
}

bmContainer.x = (video.width + 10) + tmpx + rowCount;
bmContainer.y = tmpy + lineCount;

bmContainer.addEventListener(Event.ENTER_FRAME, capture);
bmContainer.addEventListener(MouseEvent.MOUSE_DOWN, containerDown);
bmContainer.addEventListener(MouseEvent.MOUSE_UP, containerUp);

rowCount++;
}

private function containerDown(e:MouseEvent):void {
setChildIndex(Sprite(e.target), numChildren-1);
e.target.startDrag();
}

private function containerUp(e:MouseEvent):void {
e.target.stopDrag();
}


private function btmCapture(e:Event):void {
videobmd.draw(video, null, null, null, new Rectangle(0, 0, video.width, video.height), false);
}

}
}



トラックバック

このエントリーのトラックバックURL:
http://un-q.net/mt-tb.cgi/105

コメントを投稿

crossreview