Skip to content

Instantly share code, notes, and snippets.

@cocopon
Last active October 18, 2018 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cocopon/ccd12f258ff015763dc7dd5d692b2b06 to your computer and use it in GitHub Desktop.
Save cocopon/ccd12f258ff015763dc7dd5d692b2b06 to your computer and use it in GitHub Desktop.
// Processingでカメラの映像を遅延させるサンプル
// 遅延分の画像をPGraphicsの配列で保持しておくのがポイント
// カメラの映像を扱うためのライブラリをインポート
import processing.video.*;
// カメラ
Capture cam;
// 保持する枚数
int COUNT = 100;
// カメラ画像を保持する配列
// 先頭の 0 番目が一番古くて、後ろにいくほど新しい画像になるように使っていく
PGraphics gs[] = new PGraphics[COUNT];
void setup() {
size(640, 480);
// ビデオを初期化
cam = new Capture(this);
cam.start();
}
void draw() {
// 準備中の場合は何もしない
if (!cam.available()) {
return;
}
background(0);
// 一番古い画像が空っぽでなければ、キャンバスに描く
if (gs[0] != null) {
image(gs[0], 0, 0);
}
// カメラ画像の配列をひとつずつ繰り上げていく
// 0 番目に 1 番目の画像を入れて、
// 1 番目に 2 番目の画像を入れて、
// …
for (int i = 0; i < COUNT - 1; i++) {
gs[i] = gs[i + 1];
}
// 新たにキャンバスを生成する
PGraphics g = createGraphics(300, 200);
g.beginDraw();
// カメラの画像を取得して、生成したキャンバスに描く
cam.read();
g.image(cam, 0, 0, 300, 200);
g.endDraw();
// 最新のカメラ画像を描いたキャンバスを、配列の末尾に置く
gs[COUNT - 1] = g;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment