我目前正在开展一个项目,我需要加载一个包含图像的文件夹。
下面的代码段在Java模式下运行完美,但无法在JS模式下呈现图像。我已经尝试了很长时间才能使这个工作,我想知道为什么这个剪下来不起作用,如果有任何其他方法加载一个完整的图像文件夹,而不知道文件名。
void setup()
{
for(int i = 0; i < myPNGs.length; i++)
{
myClasses.add(new Class(myPNGs[i]));
}
}
PImage[] myPNGs()
{
ArrayList<PImage> myImages = new ArrayList<PImage>();
File picturesPath = sketchFile("images"); // Supposes you have a "images" folder in your sketch folder
File[] files = picturesPath.listFiles(); // Get a list of files
PImage[] images = new PImage[files.length];
int imageCount = 0;
for (int i = 0; i < files.length; i++)
{
String fileName = files[i].getName().toLowerCase();
if (fileName.endsWith(".png") || fileName.endsWith(".PNG"))
{
images[imageCount++] = loadImage(files[i].getAbsolutePath());
}
}
return images;
}
我的原帖:http://forum.processing.org/two/discussion/6324/problem-loading-a-folder-full-of-images#Item_1
感谢。
答案 0 :(得分:0)
图像需要@preload指令(http://processingjs.org/reference/preload),并且受到互联网工作方式的限制,而不是文件系统:你不能只是获取一个dirlisting并加载文件夹中的每个文件,因为那里除非你将这些功能写入你的网络服务器,并且有一些客户端javascript来解释结果,否则互联网上没有任何信息。
您必须构建要加载的图像列表,并在使用它们之前预先加载每个图像,或者编写一些可以向服务器请求dir内容的客户端 - 服务器代码,构建预加载列表,然后使用预加载到草图源的预加载指令加载草图。
或者,你可以使用requestImage概念,在那里你请求图像,然后你必须在某个时候检查img.width
是否大于0;一旦它,图像可供使用,但您仍然需要服务器/客户端代码来获取目录列表。