Change capture from key event to time interval
in
Core Library Questions
•
2 years ago
HI,
I am completely new to processing. I am working on making an interactive projection and I need to have a live feed video camera capture stills of the viewers participating in the projection. A really generous individual was kind enough to send me the code (see below) which allows me to accomplish this- it works perfectly but it is necessary to press the 's' key to capture the image. I am wondering if there is any way to have the image capture happen at a set time interval instead? That way the viewer wouldn't have to press any keys to have the image capture happen. If anyone could take a look and give me any suggestions or help me out with this I would really appreciate it.
Thanks much,
mbvcloud
the code:
import processing.video.*;
import javax.swing.*;
//--change these settings if you wish
int w = 640;
int h = 480;
String folder = "/Users/" + System.getProperty("user.name") + "/Desktop/timeLapse4Md8";
//--do not touch after that
Capture cam;
NumberFormat nf = NumberFormat.getInstance();
JFileChooser fc;
int count = 0;
String num;
PImage overlay;
boolean showOverlay = false;
boolean overlayState = false;
boolean saveProcess = false;
//--setup
void setup() {
size(w, h);
cam = new Capture(this, w, h);
//--manage saved files
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
folder = file.getAbsolutePath();
}
File dir = new File(folder);
if (dir.exists()) {
String[] allFiles = dir.list();
count = allFiles.length;
}
else {
if (dir.mkdir()) println("Directory created");
}
num = formatCount(count-1);
}
//--draw
void draw() {
if (cam.available()) {
cam.read();
image(cam, 0, 0);
}
if (showOverlay) {
blend(overlay, 0, 0, w, h, 0, 0, w, h, ADD);
}
if (saveProcess) saveImage();
}
//--key events
void keyPressed() {
if (key == 's') saveProcess();
else if (key == 'l') toggleOverlayImage();
else if (key == 'c') camSettings();
}
//--other functions
//--run saving process and check if overlay has to be hidden
void saveProcess() {
overlayState = showOverlay;
showOverlay = false;
saveProcess = true;
}
//--save frame
void saveImage() {
num = formatCount(count);
saveFrame(folder + "/timeLapse-" + num + ".jpg");
//println("Frame # " + num + " saved");
count++;
saveProcess = false;
if (overlayState) toggleOverlayImage();
}
//--format the file number with 5 digits
String formatCount(int num) {
nf.setMinimumIntegerDigits(5);
nf.setMaximumIntegerDigits(5);
nf.setGroupingUsed(false);
return nf.format(num);
}
//--reload and overlay the last image to be captured
void toggleOverlayImage() {
if (count > 0) {
if (!showOverlay) overlay = loadImage(folder + "/timeLapse-" + num + ".jpg");
if (showOverlay) showOverlay = false;
else showOverlay = true;
}
}
//--display camera preferences
void camSettings() {
cam.settings();
}
I am completely new to processing. I am working on making an interactive projection and I need to have a live feed video camera capture stills of the viewers participating in the projection. A really generous individual was kind enough to send me the code (see below) which allows me to accomplish this- it works perfectly but it is necessary to press the 's' key to capture the image. I am wondering if there is any way to have the image capture happen at a set time interval instead? That way the viewer wouldn't have to press any keys to have the image capture happen. If anyone could take a look and give me any suggestions or help me out with this I would really appreciate it.
Thanks much,
mbvcloud
the code:
import processing.video.*;
import javax.swing.*;
//--change these settings if you wish
int w = 640;
int h = 480;
String folder = "/Users/" + System.getProperty("user.name") + "/Desktop/timeLapse4Md8";
//--do not touch after that
Capture cam;
NumberFormat nf = NumberFormat.getInstance();
JFileChooser fc;
int count = 0;
String num;
PImage overlay;
boolean showOverlay = false;
boolean overlayState = false;
boolean saveProcess = false;
//--setup
void setup() {
size(w, h);
cam = new Capture(this, w, h);
//--manage saved files
fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
folder = file.getAbsolutePath();
}
File dir = new File(folder);
if (dir.exists()) {
String[] allFiles = dir.list();
count = allFiles.length;
}
else {
if (dir.mkdir()) println("Directory created");
}
num = formatCount(count-1);
}
//--draw
void draw() {
if (cam.available()) {
cam.read();
image(cam, 0, 0);
}
if (showOverlay) {
blend(overlay, 0, 0, w, h, 0, 0, w, h, ADD);
}
if (saveProcess) saveImage();
}
//--key events
void keyPressed() {
if (key == 's') saveProcess();
else if (key == 'l') toggleOverlayImage();
else if (key == 'c') camSettings();
}
//--other functions
//--run saving process and check if overlay has to be hidden
void saveProcess() {
overlayState = showOverlay;
showOverlay = false;
saveProcess = true;
}
//--save frame
void saveImage() {
num = formatCount(count);
saveFrame(folder + "/timeLapse-" + num + ".jpg");
//println("Frame # " + num + " saved");
count++;
saveProcess = false;
if (overlayState) toggleOverlayImage();
}
//--format the file number with 5 digits
String formatCount(int num) {
nf.setMinimumIntegerDigits(5);
nf.setMaximumIntegerDigits(5);
nf.setGroupingUsed(false);
return nf.format(num);
}
//--reload and overlay the last image to be captured
void toggleOverlayImage() {
if (count > 0) {
if (!showOverlay) overlay = loadImage(folder + "/timeLapse-" + num + ".jpg");
if (showOverlay) showOverlay = false;
else showOverlay = true;
}
}
//--display camera preferences
void camSettings() {
cam.settings();
}
1