Processing saveframe()屏幕截图已写入文件,但在处理过程中无法读取图像文件

Processing saveframe()屏幕截图已写入文件,但在处理过程中无法读取图像文件,processing,Processing,我想使用处理来拍摄截图,并使用截图来生成动画。我尝试的方法是将帧保存到目录中,然后将图像文件重新加载到图像字符串中。不确定这样做是否正确,但我在加载图像时出错: String[]列表文件名(String dir){ 文件=新文件(目录); if(file.isDirectory()){ 字符串名称[]=file.list(); 返回姓名; }否则{ //如果它不是一个目录 返回null; } } 作废提款(){ 如果(帧数%60==0)屏幕(); 如果(屏幕!=null)图像(屏幕,0,0,宽

我想使用处理来拍摄截图,并使用截图来生成动画。我尝试的方法是将帧保存到目录中,然后将图像文件重新加载到图像字符串中。不确定这样做是否正确,但我在加载图像时出错:

String[]列表文件名(String dir){
文件=新文件(目录);
if(file.isDirectory()){
字符串名称[]=file.list();
返回姓名;
}否则{
//如果它不是一个目录
返回null;
}
}
作废提款(){
如果(帧数%60==0)屏幕();
如果(屏幕!=null)图像(屏幕,0,0,宽度,高度);
如果(录音){
保存框(“输出//屏幕截图######.png”);
}
名称=列表文件名(“/Users/lj/Desktop/JingLang\u vis145A\u middterm/screenshot/output/”;
印刷(姓名);
如果(names.length!=0){
for(int i=0;i

我想知道是否有人知道这个问题,是否有其他方法来制作截图动画。谢谢

我认为问题在于
listFileNames()
只是列出文件名,而不是文件的绝对路径。除非这些图像位于草图目录(或草图的数据目录)中,否则需要使用图像的完整路径来加载它们

一个选项是将绝对目录路径与文件名连接起来:

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;
  }
}


void draw(){
  if (frameCount % 60 == 0) screen();
  if (screen != null) image(screen, 0, 0, width, height);
  if(recording){
    saveFrame("output//screenshot_####.png");
  }
  String dir = "/Users/lj/Desktop/JingLang_vis145A_Midterm/screenshot/output/"
  names = listFileNames(dir);
  print(names);
  if(names.length != 0){
    for(int i = 0; i < names.length; i = i+1){
      // concatenate the directory with each fine name
      // dir inclues / at the end (otherwise you can use File.separator (e.g. dir/image.png = dir + File.separator + "image.png"))
      img[i] = loadImage(dir + names[i]);
      for (int j = 0; j < particles.length; j++) {
         particles[j].display();
         particles[j].move();
      }
    }
  }
}
您可以通过处理>示例>主题>文件IO在Daniel Shiffman的目录列表示例中找到此函数和其他一些函数

此外,/已经存在于处理中,只是稍微隐藏了一点。这些变体的好处在于,您可以将结果筛选为仅具有png扩展名的文件(例如,排除文件夹、隐藏文件等)

以下是上述代码段的修改版本:

void draw(){
  if (frameCount % 60 == 0) screen();
  if (screen != null) image(screen, 0, 0, width, height);
  if(recording){
    saveFrame("output//screenshot_####.png");
  }
  String dir = "/Users/lj/Desktop/JingLang_vis145A_Midterm/screenshot/output/"
  File[] files = listFiles(dir,"files","extension=png")

  if(files == null) {
    println(dir + "is not a directory");
    return;
  }

  for(int i = 0; i < files.length; i = i+1){
    // concatenate the directory with each fine name
    // dir inclues / at the end (otherwise you can use File.separator (e.g. dir/image.png = dir + File.separator + "image.png"))
    img[i] = loadImage(files[i].getAbsolutePath());
    for (int j = 0; j < particles.length; j++) {
       particles[j].display();
       particles[j].move();
    }
  }
}
void draw(){
如果(帧数%60==0)屏幕();
如果(屏幕!=null)图像(屏幕,0,0,宽度,高度);
如果(录音){
保存框(“输出//屏幕截图######.png”);
}
String dir=“/Users/lj/Desktop/JingLang\u vis145A\u期中/screenshot/output/”
File[]files=listFiles(dir,“files”,“extension=png”)
if(files==null){
println(dir+“不是目录”);
回来
}
对于(int i=0;i
你的问题有点离题,我注意到一些令人困惑的事情:

  • 您在
    draw()
    中每帧多次列出文件和加载图像。您可能希望在
    setup()
    中加载图像一次,将其存储在
    PImage[]
    中,然后只需在
    draw()中使用即可
  • 似乎您仅在加载图像时更新粒子,并且加载的图像和共享代码中的粒子之间似乎没有连接。您可能希望将粒子代码与图像加载代码分开
  • File[] listFiles(String dir) {
      File file = new File(dir);
      if (file.isDirectory()) {
        File[] files = file.listFiles();
        return files;
      } else {
        // If it's not a directory
        return null;
      }
    }
    
    File[] files;
    
    void draw(){
      if (frameCount % 60 == 0) screen();
      if (screen != null) image(screen, 0, 0, width, height);
      if(recording){
        saveFrame("output//screenshot_####.png");
      }
      String dir = "/Users/lj/Desktop/JingLang_vis145A_Midterm/screenshot/output/"
      files = listFiles(dir);
    
      if(files == null) {
        println(dir + "is not a directory");
        return;
      }
    
      for(int i = 0; i < files.length; i = i+1){
        // concatenate the directory with each fine name
        // dir inclues / at the end (otherwise you can use File.separator (e.g. dir/image.png = dir + File.separator + "image.png"))
        img[i] = loadImage(files[i].getAbsolutePath());
        for (int j = 0; j < particles.length; j++) {
           particles[j].display();
           particles[j].move();
        }
      }
    }
    
    void draw(){
      if (frameCount % 60 == 0) screen();
      if (screen != null) image(screen, 0, 0, width, height);
      if(recording){
        saveFrame("output//screenshot_####.png");
      }
      String dir = "/Users/lj/Desktop/JingLang_vis145A_Midterm/screenshot/output/"
      File[] files = listFiles(dir,"files","extension=png")
    
      if(files == null) {
        println(dir + "is not a directory");
        return;
      }
    
      for(int i = 0; i < files.length; i = i+1){
        // concatenate the directory with each fine name
        // dir inclues / at the end (otherwise you can use File.separator (e.g. dir/image.png = dir + File.separator + "image.png"))
        img[i] = loadImage(files[i].getAbsolutePath());
        for (int j = 0; j < particles.length; j++) {
           particles[j].display();
           particles[j].move();
        }
      }
    }