Quantcast
Channel: Processing Forum
Viewing all articles
Browse latest Browse all 1768

Trying to compile two sketches on one

$
0
0
So, I am new to processing and I am trying to do a sketch that uses the webcam to take a picture and within that image sample color from a specified area (pixel). What I did was make two sketches separately: one that takes the picture and saves it, and a second one that takes the saved picture and samples color from the specified area.
Now what i want to do is compile it into one sketch...

This is the one that captures the image and saves it

/**
 * Getting Started with Capture.
 *
 * Reading and displaying an image from an attached Capture device.
 */
 
 
 
import processing.video.*;

Capture cam;

void setup() {
  size(640, 480, P2D);
 

  String[] cameras = Capture.list();
 
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }
   
    // The camera can be initialized directly using an element
    // from the array returned by list():
    cam = new Capture(this, cameras[0]);
    cam.start();    
  }     
}

void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  image(cam, 0, 0);
  // The following does the same, and is faster when just drawing the image
  // without any additional resizing, transformations, or tint.
  //set(0, 0, cam);
}

void keyPressed() {
 if (key == ' ')
saveFrame("../pics/1.jpg");
}


And this is the one that samples the color

/**
 * Pixel Array.
 *
 * Click and drag the mouse up and down to control the signal and
 * press and hold any key to see the current pixel being read.
 * This program sequentially reads the color of every pixel of an image
 * and displays this color to fill the window. 
 */

// The next line is needed if running in JavaScript Mode with Processing.js
/* @pjs preload="sea.jpg"; */

PImage img;
int direction = 0;
float signal;

void setup() {
  size(640, 480);
  noFill();
  stroke(255);
  frameRate(30);
}

void draw() {
  img = loadImage("../pics/1.jpg");
  redraw();
 
  if (signal > img.width*img.height-1 || signal < 0) {
    direction = direction * -1;
  }

  if (mousePressed) {
    int mx = constrain(mouseX, 0, img.width-1);
    int my = constrain(mouseY, 0, img.height-1);
    signal = my*img.width + mx;
  } else {
    signal += 0.33*direction;
  }

  int sx = int(signal) % img.width;
  int sy = int(signal) / img.width;

  if (keyPressed) {
    set(0, 0, img);  // fast way to draw an image
    point(sx, sy);
    rect(sx - 5, sy - 5, 10, 10);
  } else {
    color c = img.get(sx, sy);
    background(c);
  }
}



I have saved the sketches in different folders of the same name of the sketch and the pictures are saved in one folder up from the sketches in a pics folder

I would really appreciate any help that i can get from you guys

Viewing all articles
Browse latest Browse all 1768

Trending Articles