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

Trying to switch automatically between webcams

$
0
0
Im trying to write a processing script that will randomly cycle through a series of webcams, i can get the values to change of random but it doesn't seem to effect the camera changing. I'm a little stuck so any help is appreciated. 

  1. import processing.video.*;

  2. Capture cam1;
  3. Capture cam2;

  4. void setup() {
  5.   size(1000, 1000);

  6.   String[] cameras = Capture.list();
  7.   
  8.   if (cameras.length == 0) {
  9.     println("There are no cameras available for capture.");
  10.     exit();
  11.   } else {
  12.     println("Available cameras:");
  13.     for (int i = 0; i < cameras.length; i++) {
  14.       println(cameras[i]);
  15.     }
  16.     
  17.     // The camera can be initialized directly using an 
  18.     // element from the array returned by list():
  19.     cam1 = new Capture(this, cameras[6]);
  20.     println(cameras[3]);
  21.      
  22.     cam2 = new Capture(this, cameras[18]);
  23.     println(cameras[18]);
  24.             
  25.   }      
  26. }

  27. void draw() {
  28.   if (cam1.available() == true) {
  29.     cam1.read();
  30.   }
  31.   if (cam2.available() == true) {
  32.     cam2.read();
  33.   }
  34.   
  35.   image(cam1, 0, 0);
  36.   image(cam2, 320, 0);
  37.   image(cam1, 0, 200);
  38.   image(cam2, 320, 200);
  39.   // The following does the same, and is faster when just drawing the image
  40.   // without any additional resizing, transformations, or tint.
  41.   //set(0, 0, cam);
  42.   int random = int(random(2));
  43.   println(random);
  44.   randomCamera(random);
  45. }


  46. void randomCamera(int rand){
  47.     
  48.     switch(rand) {
  49.       case 0: 
  50.         cam1.start(); 
  51.         cam2.stop();
  52.         break;
  53.       case 1: 
  54.         cam2.start();
  55.         cam1.stop();
  56.         break;
  57.       default:             // Default executes if the case labels
  58.         println("None");   // don't match the switch parameter
  59.         break;
  60.     } 
  61.     
  62. }

Viewing all articles
Browse latest Browse all 1768

Trending Articles