Echo d’image webcam / Processing

Programme d'affichage avec retard de l'image d'une webcam.

Source: videoCamDelay.pde / Processing 3.5.3

import processing.video.*;

Capture cam;
int camFps, camSelected, camDelay, camWidth, camHeight;
int imgSaved, imgToSave, imgDisplay;
PImage videoCam;
ArrayList<PImage> imgTab;
boolean saving;

void setup() {
  size(1280, 1024);
  
  String[] cameras = Capture.list();
  if (cameras == null) {
    println("Failed to retrieve the list of available cameras, will try the default...");
  } if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    printArray(cameras);
  }
  
  camWidth = 1600; // Change camera width
  camHeight = 1200; // Change camera height
  camFps = 5; // cadence d'images de la vidéo
  camSelected = 29; //numéro de la caméra choisie
  
  frameRate(camFps);
  
  camDelay = 2; //Délais avant affichage de la vidéo en seconde
  imgToSave = camFps * camDelay;
  imgTab = new ArrayList<PImage>();
  imgDisplay = 0;
  imgSaved = 0;
  saving = true;
  
  cam = new Capture(this, cameras[camSelected]);
  cam.start();
}

void draw() {
  background(0);
  if (cam.available() == true && imgTab.size() < imgToSave && saving) {
    cam.read();
    videoCam = createImage(camWidth, camHeight,RGB);
    videoCam.copy(cam, 0, 0, camWidth, camHeight, 0, 0, camWidth, camHeight);
    imgTab.add(videoCam); // Save image cam in ArrayList
    println("saving img: " + imgTab.size() + " / " + imgToSave);
  }else if(imgTab.size() == imgToSave){
    saving = false;
    println("img display: " + imgDisplay);
    image(imgTab.get(imgDisplay), 0, 0);
    
    cam.read();
    videoCam = createImage(camWidth, camHeight,RGB);
    videoCam.copy(cam, 0, 0, camWidth, camHeight, 0, 0, camWidth, camHeight);
    imgTab.set(imgDisplay, videoCam);
    println("img saved: " + imgDisplay);
    
    if(imgDisplay < imgTab.size()-1){
      imgDisplay += 1;
    }else{
      imgDisplay = 0;
    }
  }
  
  //println(frameRate);
}

Laisser un commentaire