forked from: forked from: forked from: webcam
// forked from youpy's forked from: forked from: webcam
// forked from llcheesell's webcam
package {
import flash.display.Sprite;
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;
public class CameraView extends Sprite {
private var video:Video;
private var cameraBmp:Bitmap;
private var cameraBmpData:BitmapData;
private var w:int = 320;
private var h:int = 240;
private var time:int = 0;
private var targetY:int = 0;
private var line:Sprite = new Sprite();
public function CameraView() {
var camera:Camera = Camera.getCamera();
if (camera != null) {
video = new Video(w, h);
video.attachCamera(camera);
addEventListener(Event.ENTER_FRAME, tick);
} else {
trace("error");
}
cameraBmpData = new BitmapData(w, h, false, 0xaaaaaa);
cameraBmp = new Bitmap(cameraBmpData);
addChild(cameraBmp);
//line.alpha = 0.2;
line.graphics.lineStyle(1, 0xffff00);
line.graphics.moveTo(0, 0);
line.graphics.lineTo(w, 0);
line.graphics.endFill();
addChild(line);
}
private function tick(event:Event):void {
time ++;
if(video == null) {
return;
}
targetY = Math.abs(Math.sin((time % h) / h * 2 * Math.PI) * h)
line.y = targetY;
var lines:Array = [];
var y_pixels:Array = [];
var x:int, y:int;
cameraBmpData.draw(video);
for(x = 0; x < w; x ++) {
y_pixels = [];
for(y = 0; y < h; y ++) {
y_pixels.push(cameraBmpData.getPixel(x, y));
}
lines.push(y_pixels);
}
lines = lines.sort(sortOnFirstItem);
for(x = 0; x < w; x ++) {
for(y = 0; y < h; y ++) {
cameraBmpData.setPixel(x, y, lines[x][y]);
}
}
}
private function sortOnFirstItem(aArray:Array, bArray:Array):Number {
var y:int = (targetY >= h) ? (h - 1) : targetY;
var a:int = aArray[y];
var b:int = bArray[y];
if(a > b) {
return 1;
} else if(a < b) {
return -1;
} else {
return 0;
}
}
}
}