Advertisement
xeromino

cam

Mar 9th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. // Step 1. Import the video library
  2. import processing.video.*;
  3.  
  4. // Step 2. Declare a Capture object
  5. Capture cam;
  6.  
  7. //Step 2b. Declare a PImage to hold the mirrored web cam image
  8. PImage img;
  9.  
  10. void setup() {  
  11.   size(640, 480);  
  12.  
  13.   // Step 3. Initialize Capture object via Constructor
  14.   cam = new Capture(this, width, height);  
  15.   cam.start();
  16.  
  17.   // Step 3b. Initialize 'mirror' image
  18.   img = createImage(width, height, RGB);
  19.  
  20. }
  21.  
  22. // An event for when a new frame is available
  23. void captureEvent(Capture cam) {  
  24.   // Step 4. Read the image from the camera.  
  25.   cam.read();
  26. }
  27. void draw() {
  28.   // Step 4b. flip the image so it mirrors the users
  29.   flipImage();
  30.   // Step 5. Draw the (flipped) image.
  31.   image(img,0,0);
  32. }
  33.  
  34. void flipImage() {
  35.   img = createImage(width, height, RGB);
  36.   img.loadPixels();
  37.   cam.loadPixels();
  38.   for (int y = 0; y<height; y++) {
  39.     for (int x = 0; x<width; x++) {
  40.       img.pixels[y*width+x] = cam.pixels[y*width+(width-1-x)];
  41.     }
  42.   }
  43.   img.updatePixels();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement