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

Re : creating a capture button

$
0
0
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.


  1. /**
  2.  * Getting Started with Capture.
  3.  *
  4.  * Reading and displaying an image from an attached Capture device.
  5.  */
  6. import processing.video.*;
  7. import g4p_controls.*;

  8. Capture cam;
  9. GButton btnSave;
  10. int n = 1;

  11. void setup() {
  12.   size(640, 480, P2D);
  13.   btnSave = new GButton(this, width-44, height-30, 38, 24);
  14.   btnSave.setText("SAVE");
  15.   String[] cameras = Capture.list();
  16.   if (cameras.length == 0) {
  17.     println("There are no cameras available for capture.");
  18.     exit();
  19.   }
  20.   else {
  21.     println("Available cameras:");
  22.     for (int i = 0; i < cameras.length; i++) {
  23.       println(cameras[i]);
  24.     }
  25.     // The camera can be initialized directly using an element
  26.     // from the array returned by list():
  27.     cam = new Capture(this, cameras[0]);
  28.     cam.start();
  29.   }
  30. }

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

  40. public void handleButtonEvents(GButton button, GEvent event) {
  41.   if (button == btnSave) {
  42.     saveFrame("frame_" + n + ".jpg");
  43.     n++;
  44.   }
  45. }


Viewing all articles
Browse latest Browse all 1768

Trending Articles