JavaFX批量编辑图像

JavaFX批量编辑图像,java,image,javafx,Java,Image,Javafx,我试图一次编辑文件夹中的五幅图像(使用javafx运动模糊),而不是一个接一个地选择它们。这是我的代码,我不确定我做错了什么,但当我运行它时,只有文件夹中的最后一个图像被编辑。其他人保持原样 public class IterateThrough extends Application { private Desktop desktop = Desktop.getDesktop(); @Override public void start(Stage primaryStage) t

我试图一次编辑文件夹中的五幅图像(使用javafx运动模糊),而不是一个接一个地选择它们。这是我的代码,我不确定我做错了什么,但当我运行它时,只有文件夹中的最后一个图像被编辑。其他人保持原样

public class IterateThrough extends Application {

    private Desktop desktop = Desktop.getDesktop();


@Override
public void start(Stage primaryStage) throws IOException {

    File dir = new File("filepath");
    File [] directoryListing = dir.listFiles();


    if (directoryListing != null) {

        for (File file : directoryListing) {
            if(file.getName().toLowerCase().endsWith(".jpeg")){

                //desktop.open(file);

                Image image = new Image(new FileInputStream(file));


                 //Setting the image view 
                 ImageView imageView = new ImageView(image); 

                 //setting the fit height and width of the image view 
                 imageView.setFitHeight(600); 
                 imageView.setFitWidth(500); 

                 //Setting the preserve ratio of the image view 
                 imageView.setPreserveRatio(true);  

                 // instantiate Motion Blur class
                 MotionBlur motionBlur = new MotionBlur();

                 // set blur radius and blur angle
                 motionBlur.setRadius(15.0);
                 motionBlur.setAngle(110.0);

                 //set imageView effect
                 imageView.setEffect(motionBlur);

                 //Creating a Group object  
                 Group root = new Group(imageView);  

                 //Creating a scene object 
                 Scene scene = new Scene(root, 600, 500);  

                 //Setting title to the Stage 
                 primaryStage.setTitle("Loading an image");  

                 //Adding scene to the stage 
                 primaryStage.setScene(scene);

                 //Displaying the contents of the stage 
                 primaryStage.show(); 
            } 
        }
     }
}


public static void main(String[] args) {
    launch(args); 
}

}

首先,您不应该只让primaryStage在for each循环中显示(),因为当primaryStage first show()时,fx线程暂停在该指令上,剩余的图片将不会被读取,并且当您关闭窗口时,循环将不会继续进行,因为剩余的图片表示应用程序的结束

因此,在读取所有图像后,最好让primaryStage show()位于每个循环之外

其次,您不应该使用“组”容器来包含所有图像,最好使用VBox/HBox/FlowPane,确保上面的图像不会覆盖下面的图像

下面是修改后的代码供参考

public class IterateThrough2 extends Application{
  private Desktop desktop = Desktop.getDesktop();
  @Override
  public void start(Stage primaryStage) throws IOException{
    File dir = new File("filepath");
    File[] directoryListing = dir.listFiles();

    FlowPane root = new FlowPane();

    if(directoryListing != null){
      for(File file : directoryListing){
        if(file.getName().toLowerCase().endsWith(".jpeg")){
          Image image = new Image(new FileInputStream(file));
          ImageView imageView = new ImageView(image);
          imageView.setFitHeight(600);
          imageView.setFitWidth(500);
          imageView.setPreserveRatio(true);
          MotionBlur motionBlur = new MotionBlur();
          motionBlur.setRadius(15.0);
          motionBlur.setAngle(110.0);
          imageView.setEffect(motionBlur);
          root.getChildren().add(imageView);
        }
      }
    }

    Scene scene = new Scene(root, 600, 500);
    primaryStage.setTitle("Loading an image");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  public static void main(String[] args){
    launch(args);
  }
}

我想你用“编辑”这个词来表示“显示”,但我还是不明白你想做什么。是否要同时显示所有图像,并且彼此重叠?当使用模糊作为过渡效果时,您是否尝试一次显示一个?@VGR我尝试在使用模糊作为过渡效果时一次显示一个。那么,您可能需要使用模糊。但首先,您需要更好地定义您希望过渡的外观。仅仅模糊图像并不构成过渡效果。你想让一张图像变得越来越模糊,然后被下一张图像的模糊版本所取代,然后逐渐消除模糊吗?@diamondgirl你应该放大你的窗口,看看我上传的图像。