We are about to switch to a new forum software. Until then we have removed the registration on this forum.
**Hello, so i'm fairly new to processing and wanted to incorporate more than one function when the webcam is on. I have some examples of code I'll use but I wondered if there is a way for processing to randomly choose whether to use one example or the next? I've tried looking at functions but I don't seem to be getting anywhere. **
// Example one
import processing.video.*;
Capture video;
PImage img1;
int w=640, h=480;
boolean bright = true;
boolean greyScale;
int shiftAmount = 4;
int grid = 1;
void setup() {
size(640, 480);
video = new Capture(this, 640, 480);
video.start();
}
void draw() {
loadPixels(); // Fills pixelarray
float mouseMap = (int) map(mouseX, 0, width, 0, 255*3); // Brightness threshold mapped to mouse coordinates
if(shiftAmount > 45 || shiftAmount < 0){shiftAmount = 0;};
for (int y = 0; y< h; y++)
{
for (int x = 0; x< w; x++)
{
color c = video.pixels[y*video.width+x];
int a = (c >> 150) & 0xFF;
int r = (c >> 12) & 0xFF;
int g = (c >> 2) & 0xFF;
int b = c & 0xFF;
if (y %grid == 0) {
if (bright)
{
if (r+g+b > mouseMap) {
pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
}
}
if (!bright)
{
if (r+g+b < mouseMap) {
pixels[y*w+x] = c << shiftAmount; // Bit-shift based on shift amount
}
}
}
}
}
updatePixels();
if (greyScale) {
filter(GRAY);
}
println("Shift amount: " + shiftAmount + " Frame rate: " + (int) frameRate + " Greyscale: " + greyScale) ;
}
void keyPressed()
// Keyboard controls
{
switch(keyCode) {
case UP:
shiftAmount++;
break;
case DOWN:
shiftAmount--;
break;
case LEFT:
if (grid > 1) {
grid--;
}
break;
case RIGHT:
grid++;
break;
case TAB:
if (bright) {
bright = false;
}
if (!bright) {
bright = true;
}
break;
case ENTER:
if (!greyScale) {
greyScale = true;
break;
}
if (greyScale) {
greyScale = false;
break;
}
}
}
void captureEvent(Capture c) {
c.read();
}
// Example Two
import processing.video.*;
Capture cam;
void setup() {
size(640, 480);
String[] cameras = Capture.list();
cam = new Capture(this, width, height);
cam.start();
ellipseMode(CENTER);
}
void draw() {
noStroke();
background(255);
for (int i = 0; i < width; i = i+20) {
for (int j = 0; j < height; j = j+20) {
fill(cam.get(i, j) * 4);
ellipse(i, j, 20, 20);
}
}
if (cam.available() == true) {
cam.read();
}
//image(cam, 0, 0);
}
IF anyone would could help and tell me how I would merge both the codes so either one runs or the other each time I randomly load processing that would be a huge help!
Answers
Thanks for the response, does this input look right? I'm getting a message about cannot find a class or type named "globalvariables"
Here I made some minimum changes so it can run.
Kf
thank you so much really appreciate it!
This means you sould remove the calls to
size()
from setupSketch0() and setupSketch1(). You should only have the one size() function call in the real setup().This was supposed to inform you that you should put all the global variables from all the sketches in at the global level - and remove this placeholder, of course.