how to do ArrayList of saved images + display and update them when a new image is saved?

Hi Good Folks;

I'm pretty principiant with processing and I need some hints for this project, please.

I'm using a PS3 Eye webcam and with a "key pressed" function it saves the frame/image in a folder;

I need to visualize the last 4 images saved which are located in that the folder: from the most recent to the oldest.

Each time a new capture has been taken it should be also displayed in the first image up left and the other one decrease position.

I hope is clear, Thank you for any supports

Here the code:

import java.util.ArrayList;

import com.thomasdiewald.ps3eye.PS3Eye;
import com.thomasdiewald.ps3eye.PS3EyeP5;

PS3EyeP5 ps3eye;

int startTime;
int counter = 0;
final int FLASH_DURATION = 70;

boolean Flash;

int imageIndex = 0; // Initial image to be displayed is the first
PImage[] microbes = new PImage[4]; // The image array


void setup() {
  size(640, 480);
  smooth(0);
  ps3eye = PS3EyeP5.getDevice(this);
  ps3eye.init(60, PS3Eye.Resolution.VGA);
  ps3eye.start();
  frameRate(1000);
}

void draw() { 

  if (ps3eye !=null) { 
    ps3eye.start();
    image(ps3eye.getFrame(), 0, 160, width, height);
  }


  for (int i = 0; i < 4; i++) {
   microbes[i] = loadImage("print/" + "microbe" +  nf(counter, 4) + ".jpg");
  } 

  image(microbes[3],   0, 0, 160, 160); 
  image(microbes[2], 160, 0, 160, 160); 
  image(microbes[1], 320, 0, 160, 160); 
  image(microbes[0], 480, 0, 160, 160);

  imageIndex = (imageIndex + 1) % 4; // ---> back to zero

  if (Flash) {
    image(loadImage("white.jpg"), -5, 0);
    ps3eye.stop();
  }
  if (millis() - startTime > FLASH_DURATION) {
    Flash = false;
  } else {
    ps3eye.start();
  }
}

void keyPressed() { 

  if ( key == 's' ) {
    PImage s = get(0, 160, 640, 320);
    s.save("print/" + "microbe" + nf(counter, 4) +".jpg");
    counter++;
    imageIndex = int(microbes.length); //--> I don't get what this line one exactly do
    Flash = true;
    startTime = millis();
  }
}
Tagged:

Answers

  • edited August 2017

    The easiest way is to keep a data structure of filenames -- an array, StringList, ArrayList, or Queue -- and update it every time you add a new image. Then you always have your last four.

    If you want to use a simple structure use String[4] and keep a manual ring buffer index -- int index=0,1,2,3. Each time you update, write your new file to the index%4, then increment the index. Each time you display, start at index then display the next for positions (%4).

    Java has custom data structures just for this, but in this case it might be simpler to make your own.

  • Answer ✓

    if this is the same program it should really be posted in the same thread. if you have two active threads then that'll just confuse things and waste people's time.

  • If you want to use a simple structure, use String[4] and keep a manual ring buffer index.

    An online ring buffer array container example: O:-)
    http://Studio.ProcessingTogether.com/sp/pad/export/ro.90vdKMfkiO$zf/latest

  • edited August 2017

    Thank you for your inputs, I will try tonight to make it work. I keep you updated.

    In case if some one has time a concrete code example on what talk above would help I think :D I'm very principiant...

    Thank you

  • edited August 2017

    Update:

    Each time you update, write your new file to the index%4, then increment the index. Each time you display, start at index then display the next for positions (%4).

    Hi again, I'm having a problem to make it run properly.

    to sum up what I would like to achieve:

    I need a program that shows me always the last 4 images saved from the most recent to the oldest.

    As the program is kind of photo booth style when I start and the image's folder is empty I get NPE error.

    How can I write the code so that:

    When I start the program if the folder is empty the 4 images shows are just black? When a new image is saved it is also displayed?

    Now the code I wrote shows 4 times the same image (microbe0000). Some how the loop I wrote is not loading the images from 0000 to 0003.

    the code:

    import java.util.ArrayList;
    
    import com.thomasdiewald.ps3eye.PS3Eye;
    import com.thomasdiewald.ps3eye.PS3EyeP5;
    
    PS3EyeP5 ps3eye;
    
    int startTime;
    int counter = 0;
    final int FLASH_DURATION = 70;
    
    boolean Flash;
    
    int imageIndex = 0; // Initial image to be displayed is the first
    PImage[] microbes = new PImage[4]; // The image array
    
    
    void setup() {
      size(640, 480);
      smooth(0);
      ps3eye = PS3EyeP5.getDevice(this);
      ps3eye.init(60, PS3Eye.Resolution.VGA);
      ps3eye.start();
      frameRate(1000);
    }
    
    void draw() { 
    
      if (ps3eye !=null) { 
        ps3eye.start();
        image(ps3eye.getFrame(), 0, 160, width, height);
      }
    
    
      for (int i = 0; i < 4; i++) {
       microbes[i] = loadImage("print/" + "microbe" +  nf(counter, 4) + ".jpg");
      } 
    
      image(microbes[3],   0, 0, 160, 160); 
      image(microbes[2], 160, 0, 160, 160); 
      image(microbes[1], 320, 0, 160, 160); 
      image(microbes[0], 480, 0, 160, 160);
    
      imageIndex = (imageIndex + 1) % 4; // ---> back to zero
    
      if (Flash) {
        image(loadImage("white.jpg"), -5, 0);
        ps3eye.stop();
      }
      if (millis() - startTime > FLASH_DURATION) {
        Flash = false;
      } else {
        ps3eye.start();
      }
    }
    
    void keyPressed() { 
    
      if ( key == 's' ) {
        PImage s = get(0, 160, 640, 320);
        s.save("print/" + "microbe" + nf(counter, 4) +".jpg");
        counter++;
        imageIndex = int(microbes.length); //--> I don't get what this line one exactly do
        Flash = true;
        startTime = millis();
      }
    }
    
  • What I also notice is that if the program starts and in the folder "print" (where the images are saved) there are 14 images, it overrides the one already present and after instead or create news one it gives me an error that he can not find the file.

    I don't know how to fix that

  • Hi there,

    here the final working code:

    import com.thomasdiewald.ps3eye.PS3Eye;
    import com.thomasdiewald.ps3eye.PS3EyeP5;
    
    PS3EyeP5 ps3eye;
    final int FLASH_DURATION = 70;
    
    int startTime;
    
    PImage[] microbes = new PImage[4];
    int lastImageIndex = 0;
    int microbesIndex = 0;
    
    boolean flashing = false;
    
    void setup() {
    
      size(640, 480);
      smooth(0);
      ps3eye = PS3EyeP5.getDevice(this);
      ps3eye.init(30, PS3Eye.Resolution.VGA);
      ps3eye.start();
      frameRate(30);
      initImages();
      startTime = millis();
    }
    
    
    void draw() { 
    
      if (ps3eye != null) { 
        ps3eye.start();
        image(ps3eye.getFrame(), 0, 160, width, height);
      }
    
      if (flashing) {
        image(loadImage("white.jpg"), -5, 0);
        ps3eye.stop();
      }
    
      if ( (millis() - startTime) >= FLASH_DURATION) {
        flashing = false;
        showImages();
      } else {
        ps3eye.start();
      }
    }
    
    void initImages() {
    
      PImage white= loadImage("white.jpg");
      microbes[0]= white; 
      microbes[1]= white; 
      microbes[2]= white;  
      microbes[3]= white;
      showImages();
    }
    
    void showImages() {
    
      image(microbes[0], 0, 0, 160, 160); 
      image(microbes[1], 160, 0, 160, 160); 
      image(microbes[2], 320, 0, 160, 160); 
      image(microbes[3], 480, 0, 160, 160);
    }
    
    void keyReleased() { 
      if ( key == 's' ) {
        PImage s = get(0, 160, 640, 320);
        println(nf(lastImageIndex, 4));
        s.save("print/microbe" + nf(lastImageIndex++, 4) +".jpg");
        flashing = true;
        startTime = millis();
    
        if (lastImageIndex > 999) {
          lastImageIndex = 0;
        }
        for (int i = 2; i >= 0; i--) {
          microbes[i+1] = microbes[i];
        }
        microbes[0]=s;
      }
    }
    

    Thank you

  • edited August 2017

    Glad it is working for you -- thank you for sharing your working solution with the forum!

  • Answer ✓

    @polispace Thxs for sharing your solution. I can see your final solution is to shuffle the images in your array to accommodate a new(most recent) image and you do that right at line 78. A suggestion is to do this small change:

    final int N_IMGS=4; PImage[] microbes = new PImage[N_IMGS];

    and then use N_IMGS in your code to work with the number of images in your container. In this way, if you ever want to change from 4 to any other number, you will just need to change one line in your code.

    @GoToLoop studio.processingtogether.com? Really? You change your web host for your code almost every time I turn my head away from the screen. How do you find out about these sites? How does it work for you and how do you like it compared to openprocessing?

    Kf

  • edited August 2017

    http://Studio.ProcessingTogether.com? Really?

    @kfrajer, my oldest sketch samples are still hosted there. :-\"
    Recent 1s generally either go to https://OpenProcessing.org/ or https://Gist.GitHub.com/. :)>-

Sign In or Register to comment.