Render sketch to pdf example

//Add an pdf export function on keyPressed to the Opencv find Contours sketch

import gab.opencv.*;
import processing.video.*;
import processing.pdf.*;

boolean record;

Capture cam;
Movie video;
OpenCV opencv;

ArrayList contours;

void setup() {
  size(640, 480);
  cam = new Capture(this, 640, 480, 30);
  cam.start();

  opencv = new OpenCV(this, 640, 480);
  opencv.loadCascade(OpenCV.CASCADE_FULLBODY );
  opencv.startBackgroundSubtraction(5, 3, 0.1);

}

void draw() {
  if (record) {
    // Note that #### will be replaced with the frame number. Fancy!
    beginRecord(PDF, "frame-####.pdf"); 
  }
  background(255);
  if(cam.available()){
    cam.read(); 
  }

  //image(cam, 0, 0);  
  opencv.loadImage(cam); 

  opencv.gray();
  opencv.threshold(70);

  noFill();
  stroke(255, 0, 0);
  strokeWeight(4);
  for (Contour contour : opencv.findContours()) {
       stroke(0, 255, 0);
    contour.draw();

    stroke(255, 0, 0);
    beginShape();
    for (PVector point : contour.getPolygonApproximation().getPoints()) {
      vertex(point.x, point.y);
    }
    endShape();
  }
  if (record) {
    endRecord();
    record = false;
  }
}

void movieEvent(Capture m) {
  m.read();
}

// Use a keypress so thousands of files aren't created
void keyPressed() {
  record = true;
}