Lektion 4
Processing beinhaltet schon eine Library um Kameras oder Filme abzuspielen.
Kamera-Filter Beispiel
import processing.video.*;
Capture video;
int filterMode = 0;
void setup()
{
size(640, 480);
/*
String[] camList = Capture.list();
println(camList);
video = new Capture(this, 640,480,"yourCam");
*/
video = new Capture(this, 640, 480);
video.start();
}
void draw()
{
if (video.available() == false)
return;
video.read();
video.loadPixels();
int index = 0;
for (int y = 0; y < video.height; y++)
{
for (int x = 0; x < video.width; x++)
{
index = video.width * y + x;
int pixelColor = video.pixels[index];
switch(filterMode)
{
case 1:
video.pixels[index] = color(0, green(pixelColor), blue(pixelColor));
break;
case 2:
video.pixels[index] = color(red(pixelColor), 0, blue(pixelColor));
break;
case 3:
video.pixels[index] = color(red(pixelColor), green(pixelColor), 0);
break;
}
}
}
video.updatePixels();
image(video, 0, 0);
}
void keyPressed()
{
switch (key)
{
case ' ':
filterMode = 0;
break;
case 'r':
filterMode = 1;
break;
case 'g':
filterMode = 2;
break;
case 'b':
filterMode = 3;
break;
}
}
Kamera-Pixelator Beispiel
import processing.video.*;
Capture video;
int gridSize = 30;
void setup()
{
size(640, 480);
video = new Capture(this, 640, 480);
video.start();
noStroke();
}
void draw()
{
if (video.available() == false)
return;
video.read();
background(0);
video.loadPixels();
int index = 0;
for (int y = 0; y < video.height; y += gridSize)
{
for (int x = 0; x < video.width; x += gridSize)
{
int avgAreaColor = getAverage(video, x, y, gridSize, gridSize);
fill(red(avgAreaColor), green(avgAreaColor), blue(avgAreaColor));
rect(x, y, gridSize, gridSize);
//ellipse(x,y,gridSize,gridSize);
}
}
}
void keyPressed()
{
switch (key)
{
case '1':
gridSize++;
break;
case '2':
gridSize--;
break;
}
if(gridSize > 100)
gridSize = 100;
else if (gridSize < 1)
gridSize = 1;
}
int getAverage(PImage img, int startX, int startY, int w, int h)
{
// check for borders
if(startX + w >= img.width)
w = img.width - 1 - startX;
if (startY + h >= img.height)
h = img.height - 1 -startY;
int index;
int pixelCount = 0;
float redSum = 0;
float greenSum = 0;
float blueSum = 0;
int pixelColor;
for (int y = startY; y < h + startY; y++)
{
for (int x = startX; x < w + startX; x++)
{
index = img.width * y + x;
pixelColor = img.pixels[index];
redSum += red(pixelColor);
greenSum += green(pixelColor);
blueSum += blue(pixelColor);
pixelCount++;
}
}
return color(redSum * 1.0 / pixelCount, greenSum * 1.0 / pixelCount, blueSum * 1.0 / pixelCount);
}