Webcam+Glitch V.2.0

Hello, I had posted a similar question but I still have not solved the problem. I am currently trying to get different glitches to happen while using the webcam to distort the image. My problem is that I have found a few different codes, but I am very new to the program. Basically I want the Webcam to run as normal, but every 10 seconds I wan a glitch to go off for about 2 seconds. I have found different codes for different glitches, but I cannot put them together for the life of me. I have looked up online and have found nothing that can help me so far. So basically, I would need help with three things: 1)How do I get the glitch to activate every 10 seconds for 2 seconds automatically? 2)Can I get two or more glitches to alternate randomly? 3)How do I put them together in one code?

Here's the first code that I have. This is the camera running, and white noise that goes off for a millisecond (I don't know how to make it longer) when I press the mouse button. I want the white noise to activate on its own. This is the most important part, if you can help me with this then I'd be the happiest man on earth.

import processing.video.*; 
Capture cam; 

void setup() { 
  size(1024, 768); 
  cam = new Capture(this);
  cam.start(); 
} 

void draw() { 
  if (cam.available()) { 
    // Reads the new frame
    cam.read(); 
  } 
  image(cam, 0, 0); 
} 

void mousePressed() {
  loadPixels();
  for( int i=0; i<pixels.length; i++)
    pixels[i] = color( random(255) );
  updatePixels();
}

This is a bit of extra help I need, but is not as important as the previous one. How can I add this effect, to the previous code so the white nose and this one alternates? I don't need the part of the code that saves the image when you click.

import processing.video.*;

Capture cam;

int N=0;

    void setup(){
        size (851,315);
        background(255);
        colorMode(HSB);
        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]);
}


cam = new Capture(this, cameras[0]);
cam.start(); 
    } 
}


void draw(){

    Set();
        if (cam.available() == true) {
        if(frameCount==2||frameCount%150==0){
    cam.read();
        image(cam, 0, 0);

    set(width, height, cam);
        for (int i=0; i<100;i++){
        noStroke();
        fill(random(0,360),300,300,100);
        rect ((random (0, width)),(random (0, height)),(random (0,50)),(random(0,50)));

        }
    }
}

}

    int r(int a){
        return int(random(a));
    }

        void mouseReleased (){
        N++;
        save("glitchCamera"+ (N)+".tif");
    }

    void Set() {
        for (int i=0; i<10; i++) {
        int x = r(width);
        int y = r(height);
        set(x+r(50)-1,y+r(3)-1,get(x,y,r(99),r(30)));
    }
}

Honestly if you can help me out, I swear that you can pm me and I'll go to you and get you a beer. (J.k. I don't own a car, but I will gladly give credits in my final project).

Tagged:

Answers

  • Study the following code.

    Kf

    final int kTotalGlitches=10;
    int currGlitch=0;
    
    ArrayList<GlitchClass> glitches;
    
    final int kGlitchState=0;
    final int kCamState=1;
    
    int state=kCamState;
    
    
    // ---------------------------------
    
    final int kGlitchTimeLapse=2000;  // 2 secs
    final int kCamTimeLapse=10000;    //10 secs
    int triggerTime;
    
    // ---------------------------------
    
    
    import processing.video.*; 
    Capture cam; 
    
    
    
    
    void setup() { 
      size(1024, 768); 
      imageMode(CENTER);
    
      glitches = new ArrayList <GlitchClass>();
      for (int i=0; i<kTotalGlitches; i++)
        glitches.add(new GlitchClass());
    
      cam = new Capture(this);
      cam.start();
      triggerTime=millis()+kCamTimeLapse;
    } 
    
    void draw() { 
    
      if(millis()>=triggerTime){
    
        if(state==kCamState){
          state=kGlitchState;
          triggerTime+=kGlitchTimeLapse;
          currGlitch=int(random(kTotalGlitches));  //Select new glitch
        }
        else{
          state=kCamState;
          triggerTime+=kCamTimeLapse;
        }
    
      }
    
      if (state==kCamState)
        image(cam, width/2, height/2);
      else {
        background(0);
        glitches.get(currGlitch).display();
      }
    } 
    
    void captureEvent(Capture c) {
      c.read();
    }
    
    void mousePressed() {
      loadPixels();
      for ( int i=0; i<pixels.length; i++)
        pixels[i] = color( random(255) );
      updatePixels();
    }
    
    
    //A glitch is just two ellipses overlapping on top of each other with different
    //colors and one being rotated by 90 degrees.
    class GlitchClass {
      private int x;
      private int y;
      private int rx;
      private int ry;
      color c;
      GlitchClass() {
        x=width/2;
        y=height/2;
        rx=width/4;
        ry=height/4;
        c=color(random(256), random(256), random(256),random(25,256));
      }
    
      void display() {
        pushStyle();
        noStroke();
        fill(c);
        ellipse(x, y, rx, ry);
        fill(0xffffffff-c);
        ellipse(x, y, ry, rx);
        popStyle();
      }
    }
    
  • edited March 2017

    Alright, I was able to make some changes to the code. Here is what I am trying to do:

     final int kTotalGlitches=10;
     int currGlitch=0;
    
     ArrayList<GlitchClass> glitches;
    
     final int kGlitchState=0;
     final int kCamState=1;
    
     int state=kCamState;
    
    
    // ---------------------------------
    
     final int kGlitchTimeLapse=2000;  // 2 secs
     final int kCamTimeLapse=5000;    //10 secs
     int triggerTime;
    
    // ---------------------------------
    
    
    import processing.video.*; 
     Capture cam; 
    
    
    
    
          void setup() { 
          size(1024, 768); 
          imageMode(CENTER);
    
     glitches = new ArrayList <GlitchClass>();
     for (int i=0; i<kTotalGlitches; i++)
     glitches.add(new GlitchClass());
    
     cam = new Capture(this);
     cam.start();
     triggerTime=millis()+kCamTimeLapse;
    } 
    
     void draw() { 
    
          if(millis()>=triggerTime){
    
          if(state==kCamState){
          state=kGlitchState;
          triggerTime+=kGlitchTimeLapse;
          currGlitch=int(random(kTotalGlitches));  //Select new glitch
    }
    else{
      state=kCamState;
      triggerTime+=kCamTimeLapse;
    }
    
    }
    
     if (state==kCamState)
     image(cam, width/2, height/2);
     else {
     background(0);
     glitches.get(currGlitch).display();
    }
    } 
    
          void captureEvent(Capture c) {
      c.read();
    }
    
          class GlitchClass {
          int r(int a){
          return int(random(a));
    }
          void display() {
    
          for (int i=0; i<10; i++) {
          int x = r(width);
          int y = r(height);
          set(x+r(50)-1,y+r(3)-1,get(x,y,r(99),r(30)));
        }
    
    
          if (cam.available() == true) {
          if(frameCount==2||frameCount%150==0){
          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.
    }
    }
    
    }
    
    
    }
    

    The problem now is that I can't have the camera running for the second statement. Do you think you could help me?

  • Answer ✓

    No, you shouldn't access the cam as you do in line 83. Everything is being done by the callback function captureEvent(). Instead, just access the cam object directly. The capture class extends from PImage, so you manage your cam object as any other PImage object. Try this:

    Kf

    class GlitchClass {
      int r(int a) {
        return int(random(a));
      }
      void display() {
    
        image(cam, width/2,height/2);    
    
        for (int i=0; i<10; i++) {
          int x = width/2-r(cam.width)+cam.width/2;   
          int y = height/2-r(cam.height)+cam.height/2;
    
          //ENABLE next lines for alternative effect
          //fill(random(256));
          //ellipse(x,y,35,35);
    
          set(x+r(50)-1, y+r(3)-1, get(x, y, r(99), r(30)));  //Notice as it is, you are getting out of the actual cam image
        }   
    
      }
    }
    
Sign In or Register to comment.