Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

How in processing can I get it to choose one of two different codes for webcam?

$
0
0

**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!


Viewing all articles
Browse latest Browse all 2896

Trending Articles