2012-09-08 82 views
6

我正在使用Java FX,我想將節點轉換爲圖像。我找到了這個資源,但它並不能解決我的問題,因爲我想將節點轉換爲圖像,而不是整個場景。如何將節點轉換爲javafx 2.1中的圖像?

How to output the content of a Scene graph in JavaFx 2.0 to an Image

+0

我不知道很多關於這一點,並沒有進行測試的時候,但不能你剛纔創建一個新的場景,並添加你想要轉換爲圖像,以它作爲唯一的節點孩子,然後使用鏈接中解釋的方法?我猜你永遠不必展示你創造的第二個場景。 –

回答

5

這是我的問題的解決方案。這個解決方案是Sergey和jewelsea的幫助。這個解決方案是在javafx 2.2中。感謝Sergey和jewelsea。

public class TrySnapshot extends Application { 

javafx.embed.swing.SwingFXUtils fXUtils; 
BufferedImage bufferedImage = new BufferedImage(550, 400, BufferedImage.TYPE_INT_ARGB); 
File file = new File("C:/Users/PC1/Desktop/Sample Images/test.jpg"); 
VBox vbox = null; 

@Override 
public void start(Stage primaryStage) { 
    vbox = new VBox(); 
    Button btn = new Button(); 
    Image i = new Image("file:C:\\Koala.jpg"); 
    ImageView imageView = new ImageView(); 
    imageView.setImage(i); 
    vbox.getChildren().add(imageView); 
    vbox.setSpacing(10); 
    btn.setText("Say 'Hello World'"); 
    btn.setOnAction(new EventHandler<ActionEvent>() { 
     @Override 
     public void handle(ActionEvent event) { 
     // here we make image from vbox and add it to scene, can be repeated :) 
     WritableImage snapshot = vbox.snapshot(new SnapshotParameters(), null); 
      vbox.getChildren().add(new ImageView(snapshot)); 
      saveImage(snapshot); 
      System.out.println(vbox.getChildren().size()); 
     } 
    }); 


    Scene scene = new Scene(new Group(btn), 500, 400); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

private void saveImage(WritableImage snapshot) { 
    BufferedImage image; 
    image = javafx.embed.swing.SwingFXUtils.fromFXImage(snapshot, bufferedImage); 
    try { 
     Graphics2D gd = (Graphics2D) image.getGraphics(); 
     gd.translate(vbox.getWidth(), vbox.getHeight()); 
     ImageIO.write(image, "png", file); 
    } catch (IOException ex) { 
     Logger.getLogger(TrySnapshot.class.getName()).log(Level.SEVERE, null, ex); 
    }; 
    } 
} 
+0

可以,請您將此答案標記爲正確並刪除「這是我的代碼」,以清除此主題。 –

+0

好的謝爾蓋。我刪除「這是我的代碼」。現在這是明確的話題。 –

+0

太好了。非常感謝! –

11

您可以使用新的FX 2.2快照功能:

public class TrySnapshot extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     final VBox vbox = new VBox(2); 
     final Button btn = new Button(); 
     vbox.getChildren().add(btn); 
     btn.setText("Say 'Hello World'"); 
     btn.setOnAction(new EventHandler<ActionEvent>() { 
      @Override 
      public void handle(ActionEvent event) { 
       // here we make image from vbox and add it to scene, can be repeated :) 
       WritableImage snapshot = vbox.snapshot(new SnapshotParameters(), null); 

       vbox.getChildren().add(new ImageView(snapshot)); 
       System.out.println(vbox.getChildren().size()); 
      } 
     }); 

     Scene scene = new Scene(new Group(vbox), 300, 250); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

如果必須使用較舊的FX由於某種原因,只是改變場景座標到您的節點座標使用在Node#getBoundsInParent電話您鏈接的代碼示例。

+0

謝謝謝爾蓋。但是,snaphot()方法不是我的問題的解決方案。我想bufferedimage一些事情。 –

+2

您可以使用Sergey的解決方案和[SwingFXUtils](http://docs.oracle.com/javafx/2/api/javafx/embed/swing/SwingFXUtils.html#fromFXImage(javafx.scene.image)獲得awt buffeted圖像。 Image,%20java.awt.image.BufferedImage)) – jewelsea