trying to save gif from webcam and display the result - video capture, gifAnimation, gifExport

edited April 2015 in Library Questions

Hello ! I'm trying to code a sort of photobooth - gif recorder. Almost everything is done but I'm stucked on two things…

I'm recording frames then saving them as a gif file with GifMaker, incrementing the file name for each new recording.

1- when the frames are being added to the file, I can't get my screen to display anything, nor a background nor my webcam… is it normal ?

    // TODO - CHECK NUMBER OF FILES IN 'IMG' DIRECTORY & INCREMENT FILENAME
    gifExport = new GifMaker(this, "img/file"+(nbGif+1)+".gif");
    gifExport.setRepeat(0); // make it an "endless" animation

    // RECORD FRAMES UNTIL FRAMELIMIT
    for (frames=0; frames<frameLimit; frames++) {
      gifExport.setDelay(FRAMES_DURATION);
      gifExport.addFrame();
      println("adding 1 frame");
    } // end loop

    // STOP RECORDING AND SAVE FILE
    if (frames==frameLimit) {
      gifExport.finish();
      frames=0;

2 - Then I want to get this last file and display it after the recording has ended. All my attempts have been fails and I'm a bit lost. limits of my syntax knowledge… I look at the structure on http://extrapixel.github.io/gif-animation/ and it seems Gif object is not going to work if not instantiated before the draw() ? How can I change the filenames / paths that are instantiated in the setup ? I've tried to change it in the draw but the gif stops to loop, so I think I'm doing something wrong with the library

import gifAnimation.*;
Gif gifLoop;
//...
void setup() {
//…
gifLoop = new Gif(this, "img/file1.gif"); // temporary instanciation
 }

void draw() {
     image(gifLoop, width/2, height/2, width, height);
     gifLoop.play();
     }

I can send my (long) sketch if someone want to help / if I'm not being clear. Thanks a lot in advance !

Answers

  • Ok so a bit more of the sketch (I've made it lighter) Would you know why does the webcam freeze when gif is recording ?

    import processing.video.*;
    Capture cam;
    import gifAnimation.*;
    GifMaker gifExport;
    import java.util.Date;
    
    int FRAME_RATE=30;
    boolean record=false;
    int frames;
    int frameLimit = 30;
    int FRAMES_DURATION = 120;
    int nbGif;
    
    ////////////////////////////////////////// SETUP
    void setup() {
      size(320, 240);
    
      // camera
      String[] cameras = Capture.list();
      cam = new Capture(this, cameras[4]);
      cam.start();
    }
    
    ////////////////////////////////////////// DRAW
    void draw() {
      nbGif = howManyGif(); // get number of gifs in 'img' directory
    
      if (record) {
        recordGif();
      } else {
        displayCam();
      }
    }
    ////////////////////////////////////////// DISPLAY CAM
    void displayCam() {
      if (cam.available()) { 
        cam.read();
      }
      // mirror
      pushMatrix();
      scale(-1, 1);
      imageMode(CORNER);
      image(cam, -width, 0, width, height);
      popMatrix();
      imageMode(CENTER);
    }
    
    ////////////////////////////////////////// RECORD GIF
      void recordGif() {
      if (record == true) {
        // CHECK NUMBER OF FILES IN 'IMG' DIRECTORY & CREATE NEW FILE
        gifExport = new GifMaker(this, "img/file"+(nbGif+1)+".gif");
        gifExport.setRepeat(0); // make it an "endless" animation
    
        // RECORD FRAMES UNTIL FRAMELIMIT
        for (frames=0; frames<frameLimit; frames++) {
          displayCam();
          gifExport.setDelay(FRAMES_DURATION);
          gifExport.addFrame();
          println("saving frame");
        } // end loop
    
        // STOP RECORDING AND SAVE FILE
        if (frames==frameLimit) {
          gifExport.finish();
          println("file"+(nbGif+1)+".gif WAS SAVED - RE INITIALIZING");
          noLoop();
        } // end if frameLimit
      } // end if launchRecording
    
      // RE INIT
      frames=0;
      record = false;
      loop();
      println("end record");
    } // END RECORD GIF
    
    ////////////////////////////////////////// RETURN ALL FILES AS STRING ARRAY  
    String[] listFileNames(String dir) {
      File file = new File(dir);
      if (file.isDirectory()) {
        String names[] = file.list();
        return names;
      } else {
        // If it's not a directory
        return null;
      }
    }
    
    //////////////////////////////////////////////////////// HOW MANY GIFS
    int howManyGif() {
      String path = sketchPath+"/img";
      File dataFolder = new File(path);
      String[] theList = dataFolder.list();
      int fileCount = theList.length;
      return(fileCount);
    }
    
    ////////////////////////////////////////// MOUSE RELEASED
    void mouseReleased() {
        record = true;
    }
    
  • Hello ! I've managed to solve the problems : applet is working ! BUT I always get an OutOfMemoryError any time after a few minutes I've already increased the RAM preferences but it doesn't solve the pb… Can you help me to debug this ? Or is there a way to relaunch automatically so I can make it work for the party tonight ? : )

    Thanks a lot

    download here

Sign In or Register to comment.