문제

Please, let me know how to get processing to load the newest image from a directory.

img = loadImage("pic1.jpg"); is the basic example

도움이 되었습니까?

해결책

I tried the code from user2468700 and didn't worked so I jumped into the reference and ended up with something like this:

    void callImage () {
     File dir = new File(dataPath(""));
     String [] list = dir.list();
     img = loadImage (path+list[list.length-1]);
     image(img, 0, 0);
    }

It will load from the "data" directory in your sketch directory. Hope this helps someone :)

다른 팁

In the following code, whenever you press the mouse, the program looks into the data folder and creates an array of Strings that stores the file names. The last value stored in the array is the latest image you placed in the folder.

The String sketchPath stores the path of the Processing sketch you're working on.

If you need a separate folder for images, just make one and change the parameter inside the String path; silly example: String path = sketchPath+"/MyFolderFullOfImages/".

This method works only if you have numbered images, because the names are sorted by alphanumeric order. However, you can easily customize this program by adding a control array that stores file names you've already used and let the program load only images with new names.

PImage img;

void setup () {
  size (500, 500);
}

void draw () {
  if (mousePressed) {
    callImage();
  }
}

void callImage () {
  background(200);
  String path = sketchPath+"/data/";
  File data = new File (path);
  String [] list = data.list();
  img = loadImage (path+list[list.length-1]);
  image(img, 0, 0);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top