Load newest image in folder

edited April 2017 in How To...

I was wondering if anyone knew a way to only load the newest image in the data folder when the code starts,or IDEALY using a keyCode to switch between images in the folder. I want to be able to change which image is being displayed without having to go into the code to change the file name in " image= loadImage("file name.JPG"); ".

Answers

  • edited April 2017

    On your post from : https://forum.processing.org/one/topic/listing-last-10-modified-files-in-directory

    It is close to what I need but I would like a user input to change the image not a continuous loop and it slows down my code significantly. Its perfect but unusable for its lag time. If you could help me fix both issues that'd be awesome!

  • edited April 2017 Answer ✓
    /**
     * FilenameFilter LastModified Sorting (v1.1.4)
     * GoToLoop (2017-Apr-15)
     *
     * https://forum.Processing.org/two/discussion/22004/
     * load-newest-image-in-folder#Item_3
     */
    
    import java.util.Arrays;
    import java.util.Comparator;
    
    import java.util.Map;
    import java.util.WeakHashMap;
    
    import java.io.FilenameFilter;
    
    static final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] EXTS = {
        "png", "jpg", "jpeg", "gif", "tif", "tiff", "tga"
      };
    
      @Override final boolean accept(final File paths, String name) {
        name = name.toLowerCase();
        for (final String ext : EXTS)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    
    static final File[] EMPTY_FILE_ARR = {};
    
    File[] getImagePaths() {
      File[] imgPaths = dataFile("").listFiles(PIC_FILTER);
      return imgPaths != null? imgPaths : EMPTY_FILE_ARR;
    }
    
    static final int MAX_IMAGES = 5;
    
    PImage pics[], pic;
    int idx;
    
    void setup() {
      noSmooth();
      noLoop();
      frameRate(2);
    
      String[] paths = new Recent().grabRecentPaths(MAX_IMAGES, getImagePaths());
      printArray(paths);
    
      if (paths.length == 0) {
        System.err.println("No files were requested or found in subfolder data!");
        exit();
      }
    
      pics = new PImage[paths.length];
      for (int i = 0; i < pics.length; pics[i] = loadImage(paths[i++]));
    
      pic = pics[0];
      getSurface().setSize(pic.width, pic.height);
    }
    
    void draw() {
      set(0, 0, pic);
      getSurface().setTitle("Pic: " + (idx+1) + '/' + pics.length);
    }
    
    void mousePressed() {
      keyCode = mouseButton;
      keyPressed();
    }
    
    void keyPressed() {
      final int k = keyCode;
      if (k != LEFT & k != RIGHT)  return;
    
      final int sig = k == RIGHT? 1 : -1;
      idx = (pics.length + idx + sig) % pics.length;
    
      pic = pics[idx];
      getSurface().setSize(pic.width, pic.height);
    
      clear();
      redraw();
    }
    
    static class Recent {
      final Map<File, Long> filecache = new WeakHashMap<File, Long>(0100);
    
      final Comparator<File> recentComparator = new Comparator<File>() {
        @Override final int compare(final File f1, final File f2) {
          return Long.signum(filecache.get(f2) - filecache.get(f1));
        }
      };
    
      static final String[] EMPTY_STR_ARR = {};
      String[] filenames = EMPTY_STR_ARR;
    
      @SafeVarargs final String[] grabRecentPaths(int qty, final File... paths) {
        if (qty == 0 || paths == null || paths.length == 0)
          return filenames = EMPTY_STR_ARR;
    
        if (qty < 0)  qty = paths.length;
        qty = min(qty, paths.length);
        filenames = new String[qty];
    
        if (paths.length == 1)  filenames[0] = paths[0].getPath();
        else {
          for (final File f : paths)  filecache.put(f, f.lastModified());
          Arrays.sort(paths, recentComparator);
          for (int i = 0; i < qty; filenames[i] = paths[i++].getPath());
        }
    
        return filenames;
      }
    
      @Override String toString() {
        return join(filenames, ENTER);
      }
    }
    
  • edited April 2017
    /**
     * FilenameFilter LastModified Sorting (v2.0)
     * GoToLoop (2017-Apr-15)
     *
     * https://forum.Processing.org/two/discussion/22004/
     * load-newest-image-in-folder#Item_4
     */
    
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.function.Supplier;
    
    import java.util.Map;
    import java.util.WeakHashMap;
    
    import java.io.FilenameFilter;
    
    static final FilenameFilter PIC_FILTER = new FilenameFilter() {
      final String[] EXTS = {
        "png", "jpg", "jpeg", "gif", "tif", "tiff", "tga"
      };
    
      @Override final boolean accept(final File paths, String name) {
        name = name.toLowerCase();
        for (final String ext : EXTS)  if (name.endsWith(ext))  return true;
        return false;
      }
    };
    
    static final File[] EMPTY_FILE_ARR = {};
    
    File[] getImagePaths() {
      File[] imgPaths = dataFile("").listFiles(PIC_FILTER);
      return imgPaths != null? imgPaths : EMPTY_FILE_ARR;
    }
    
    static final int MAX_IMAGES = 5;
    
    PImage pics[], pic;
    int idx;
    
    void setup() {
      noSmooth();
      noLoop();
      frameRate(2);
    
      final String[] paths = Recent.grabRecentPaths(MAX_IMAGES, getImagePaths());
      printArray(paths);
    
      if (paths.length == 0) {
        System.err.println("No files were requested or found in subfolder data!");
        exit();
      }
    
      pics = new PImage[paths.length];
      for (int i = 0; i < pics.length; pics[i] = loadImage(paths[i++]));
    
      pic = pics[0];
      getSurface().setSize(pic.width, pic.height);
    }
    
    void draw() {
      set(0, 0, pic);
      getSurface().setTitle("Pic: " + (idx+1) + '/' + pics.length);
    }
    
    void mousePressed() {
      keyCode = mouseButton;
      keyPressed();
    }
    
    void keyPressed() {
      final int k = keyCode;
      if (k != LEFT & k != RIGHT)  return;
    
      final int sig = k == RIGHT? 1 : -1;
      idx = (pics.length + idx + sig) % pics.length;
    
      pic = pics[idx];
      getSurface().setSize(pic.width, pic.height);
    
      clear();
      redraw();
    }
    
    static final class Recent {
      static final ThreadLocal<Map<File, Long>> filecache = ThreadLocal.withInitial(
        new Supplier<Map<File, Long>>() {
        @Override final Map<File, Long> get() {
          return new WeakHashMap<File, Long>(0150);
        }
      }
      );
    
      static final Comparator<File> RECENT_CMP = new Comparator<File>() {
        @Override final int compare(final File f1, final File f2) {
          final Map<File, Long> fc = filecache.get();
          return Long.signum(fc.get(f2) - fc.get(f1));
        }
      };
    
      static final String[] EMPTY_STR_ARR = {};
    
      @SafeVarargs static String[] grabRecentPaths(int qty, final File... paths) {
        if (qty == 0 || paths == null || paths.length == 0)  return EMPTY_STR_ARR;
    
        if (qty < 0)  qty = paths.length;
        qty = min(qty, paths.length);
    
        final String[] filenames = new String[qty];
    
        if (paths.length == 1)  filenames[0] = paths[0].getPath();
        else {
          final Map<File, Long> fc = filecache.get();
          for (final File f : paths)  fc.put(f, f.lastModified());
    
          Arrays.sort(paths, RECENT_CMP);
          for (int i = 0; i < qty; filenames[i] = paths[i++].getPath());
        }
    
        return filenames;
      }
    }
    
Sign In or Register to comment.