在Processing中保存文件的无限循环

时间:2013-10-12 17:13:58

标签: java processing

我制作了一个将PImage保存到文件(.png.jpg等等...)的方法,但是如果文件超过一定大小[不是磁盘上的大小] ,我的意思是维度,程序会一遍又一遍地重复。

我发布了方法和草图的较轻版本。

草图:

void setup(){
  noLoop();
  size(300, 300);
}

void draw(){
  background(0);
  text("Hello world!", 15, 15);
  PImage toEdit = loadImage("C:\\Users\\ingrossod\\Desktop\\toEdit.png");
  PImage toSave = change(toEdit, color(86, 177, 222), color(239, 254, 255));
  save(toSave, "C:\\Users\\ingrossod\\Desktop\\edited"); // The file extension is missing
}

现在,PImage change(PImage, color, color)需要PImage并将颜色替换为另一种颜色。

以下是void save(PImage, String)

void save(PImage toSave, String fileName){
  fileName += ".png"; // Adding the extension

  // Saving the current frame
  int oldWidth = width;
  int oldHeight = height;
  saveFrame(fileName);
  PImage oldFrame = loadImage(fileName);

  // Modifyng the current frame with the image I want to save
  size(toSave.width, toSave.height);
  background(toSave);
  saveFrame(fileName);

  // Restoring the frame with old parameters
  size(oldWidth, oldHeight);
  background(oldFrame);
}

我已经尝试修改frameRate值,将所有内容放在一个方法中调用它... 什么都没有。

该程序使用小图像工作。关于如何解决它的任何想法?

2 个答案:

答案 0 :(得分:1)

你确定无限循环不是来自change函数吗?您没有向我们提供其实施。

我的建议:

  1. 使用image(toSave, 0, 0)代替background()
  2. 使用save(fileName)代替saveFrame()

答案 1 :(得分:0)

您已将noLoop放入void setup,这会阻止void draw在第1帧之后继续。因此frameRate将完全没有效果。