In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

カラーハーフトーン (Webカメラ)

Get Adobe Flash player
by tmdf 07 Jul 2009
    Embed
/**
 * Copyright tmdf ( http://wonderfl.net/user/tmdf )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/mYzp
 */

package {
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.utils.ByteArray;
	
	[SWF(width=320, height=240, backgroundColor="#FFFFFF", frameRate=30)]
	
	public class Main extends Sprite {
		
		//ドット数
		private var dot_x:uint = 64;
		private var dot_y:uint = 48;
		
		//描画タイプ
		private var type:uint = 0;
		
		private var video:Video;
		
		public function Main():void {
			
			//カメラ取得
			var camera:Camera = Camera.getCamera();
			
			//カメラがある場合
			if (camera != null) {
				
				//カメラのサイズ、fpsを設定
				camera.setMode(160, 120, 30);
				
				//CameraとVideoを関連付け
				video = new Video(camera.width, camera.height);
				video.attachCamera(camera);
				
				//リスナー登録
				this.addEventListener(Event.ENTER_FRAME, Update);
				stage.addEventListener(KeyboardEvent.KEY_DOWN, Change);
			}
		}
		
		
		//描画更新
		private function Update(e:Event):void {
			
			//描画 初期化
			var draw:Graphics = this.graphics;
			draw.clear();
			
			//カメラからの映像を縮小してビットマップに
			var src:BitmapData = new BitmapData(dot_x, dot_y, false);
			var mx:Matrix = new Matrix();
			mx.scale(src.width / video.width, src.height / video.height);
			src.draw(video, mx);
			
			//色情報を配列に格納
			var argb:ByteArray = new ByteArray();
			argb = src.getPixels(new Rectangle(0, 0, src.width, src.height));
			
			//色情報 変数
			var gray:Number;
			var r:Number;
			var g:Number;
			var b:Number;
			var color:int;
			
			//描画 変数
			var ex:Number;
			var ey:Number;
			var ew:Number;
			var eh:Number;
			
			//ドットの最大サイズ
			var mw:Number = stage.stageWidth / src.width;
			var mh:Number = stage.stageHeight / src.height;
			
			for (var i:uint = 0; i < src.width * src.height; i ++) {
				
				//RGBを取得
				r = argb[i * 4 + 1];
				g = argb[i * 4 + 2];
				b = argb[i * 4 + 3];
				
				//グレースケール化(NTSC係数による加重平均法、1=黒 ~ 0:白)
				gray = 1 - (r * 0.298912 + g * 0.586611 + b * 0.114478) / 255;
				
				//色とサイズを指定
				if (type == 49 || type == 97) {
					color = r << 16 | g << 8 | b;
				}
				else {
					color = 0x000000;
				}
				ew =  mw * gray;
				eh =  mh * gray;
				
				//座標を指定
				ex = (i % src.width) * mw + (mw - ew) / 2;
				ey = Math.floor(i / src.width) * mh + (mh - eh) / 2;
				
				//円を描画
				draw.beginFill(color);
				draw.drawEllipse(ex, ey, ew, eh);
			}
		}
		
		
		//描画タイプ変更
		private function Change(e:KeyboardEvent):void {
			
			//更新
			type = e.keyCode;
		}
	}
}