Advertisement
xeromino

webcam

Mar 12th, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. // Step 1. Import the video library
  2. import processing.video.*;
  3.  
  4. int x=0, y=0;
  5. int rez = 5;
  6. int r = 2, sz = 2;
  7. float angle, d;
  8.  
  9. // Step 2. Declare a Capture object
  10. Capture cam;
  11.  
  12. //Step 2b. Declare a PImage to hold the mirrored web cam image
  13. PImage img;
  14.  
  15. void setup() {  
  16.   size(480, 360);
  17.   background(0);
  18.  
  19.   // Step 3. Initialize Capture object via Constructor
  20.   cam = new Capture(this, width, height);  
  21.   cam.start();
  22.  
  23.   // Step 3b. Initialize 'mirror' image
  24.   img = createImage(width, height, RGB);
  25. }
  26.  
  27. void captureEvent(Capture cam) {  
  28.   // Step 4. Read the image from the camera.  
  29.   cam.read();
  30. }
  31.  
  32. // Step 5. Display stuff.
  33. void draw() {
  34.   //background(0);
  35.   //image(cam, 0, 0);
  36.   flipImage();
  37.   //image(img, 0, 0);
  38.   //slitScan();
  39.   //dots();
  40.   //pixelated();
  41.   //if (frameCount%10==0) tile();
  42.   //paint();
  43.  
  44.   kaleidoscope();
  45. }
  46.  
  47. void flipImage() {
  48.   img.loadPixels();
  49.   cam.loadPixels();
  50.   for (int y = 0; y<height; y++) {
  51.     for (int x = 0; x<width; x++) {
  52.       img.pixels[y*width+x] = cam.pixels[y*width+(width-1-x)];
  53.     }
  54.   }
  55.   img.updatePixels();
  56. }
  57.  
  58. void slitScan() {
  59.   copy(img, x, 0, 1, height, x, 0, 1, height);
  60.   x = x + 1;
  61.   if (x>width) x = 0;
  62.   copy(img, 0, 0, width, height, 0, 0, width/5, height/5);
  63. }
  64.  
  65. void dots() {
  66.   // draw 50 random dots each frame
  67.   for (int i=0; i<50; i++) {
  68.     int x = (int) random(width);
  69.     int y = (int) random(height);
  70.     color c = img.get(x, y);
  71.     fill(c, 150);
  72.     noStroke();
  73.     float sz = random(2, 10);
  74.     ellipse(x, y, sz, sz);
  75.   }
  76.   copy(img, 0, 0, width, height, 0, 0, width/5, height/5);
  77. }
  78.  
  79. void pixelated() {
  80.   background(0);
  81.   // map the resolution to the mouse position
  82.   rez = (int) map(mouseX, 0, width, 1, 30);
  83.   //rez = 10;
  84.   // loop through the whole image to create the small boxes
  85.   for (int x = 0; x < width; x += rez) {
  86.     for (int y = 0; y <width; y += rez) {
  87.       int cx = x + rez/2;
  88.       int cy = y + rez/2;
  89.       color c = img.get(cx, cy);
  90.       fill(c);
  91.       noStroke();
  92.       rect(x, y, rez, rez);
  93.       //float br = brightness(c);
  94.       //float sz = map(br, 0, 255, 0.5, 1)*rez;
  95.       //ellipse(cx, cy, sz, sz);
  96.     }
  97.   }
  98.   if (mousePressed) {
  99.     copy(img, mouseX, mouseY, 150, 150, mouseX, mouseY, 150, 150);
  100.   }
  101. }
  102.  
  103. void tile() {
  104.   // how many tiles in one direction?
  105.   int units = 4;
  106.   // width of the tile
  107.   int w = width/units;
  108.   // height of the tile
  109.   int h = height/units;
  110.   // copying the webcam image to the tile
  111.   copy(img, 0, 0, width, height, x, y, w, h);
  112.   // moving to the next tile
  113.   x = x + w;
  114.   // if at the right border then move to start of new row
  115.   if (x>width) {
  116.     x = 0;
  117.     y = y+h;
  118.     // if at the bottom right corner then move to upper left corner again
  119.     if (y >= height) y = 0;
  120.   }
  121. }
  122.  
  123. void paint() {
  124.   if (mousePressed) {
  125.     PImage tmp = img.get(mouseX, mouseY, 50, 50);
  126.     tint(255, 150);
  127.     image(tmp, mouseX, mouseY);
  128.   }
  129.   // resized actual video
  130.   copy(img, 0, 0, width, height, 0, 0, width/5, height/5);
  131.   // dot representing mouse position on the resized video in the top left corner
  132.   fill(255);
  133.   stroke(0);
  134.   float px = map(mouseX, 0, width, 0, width/rez);
  135.   float py = map(mouseY, 0, height, 0, height/rez);
  136.   ellipse(px, py, 5, 5);
  137. }
  138.  
  139. void keyPressed() {
  140.   save(random(99999)+".png");
  141. }
  142.  
  143. void kaleidoscope() {
  144.   PImage tmp = cam.get();
  145.   tmp.resize(width/2, height/2);
  146.   //tint(255,50);
  147.  
  148.   image(tmp, 0, 0);
  149.  
  150.   scale(-1, 1);
  151.   image(tmp, -width, 0);
  152.   scale(-1, 1);
  153.  
  154.   scale(-1, -1);
  155.   image(tmp, -width, -height);
  156.   scale(-1, -1);
  157.  
  158.   scale(1, -1);
  159.   image(tmp, 0, -height);
  160.   scale(1, -1);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement