I started with one of the examples that comes with Processing added the G4P library (Select Sketch | Import Library | Add Library | G4P from the menu). I added a G4P button to the sketch, an event handler for the button and I end up with the code below.
BTW it works and saves a numbered image every time the button is clicked.
BTW it works and saves a numbered image every time the button is clicked.
- /**
- * Getting Started with Capture.
- *
- * Reading and displaying an image from an attached Capture device.
- */
- import processing.video.*;
- import g4p_controls.*;
- Capture cam;
- GButton btnSave;
- int n = 1;
- void setup() {
- size(640, 480, P2D);
- btnSave = new GButton(this, width-44, height-30, 38, 24);
- btnSave.setText("SAVE");
- 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);
- }
- public void handleButtonEvents(GButton button, GEvent event) {
- if (button == btnSave) {
- saveFrame("frame_" + n + ".jpg");
- n++;
- }
- }