2014-02-27 60 views
1

我需要將某些畫布的一部分保存爲圖像,該圖像從x1 > 0y1 > 0x2 > x1y2 > y1。我從JavaFX的API明白了什麼,快照必須佔用節點的整個區域,像在javaFX中創建畫布的快照部分

wim = new WritableImage(((int) width), ((int) height)); 
bufferedImage = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB); 
parameter = new SnapshotParameters(); 
parameter.setTransform(new Translate(0, 200)); 

然後

node.snapshot(parameter, wim); 
image = SwingFXUtils.fromFXImage(wim, bufferedImage); 
Graphics2D gd = (Graphics2D) image.getGraphics(); 
gd.translate(0,200); 
ImageIO.write(image, "png", file); 

回答

1

HEJ穆罕默德,

我犯了一個小例子對你有硬編碼寬度和高度爲WritableImage,也許這會幫助你。我在舞臺上放置一個ChartBar,並點擊按鈕時拍下它的快照。

package de.professional_webworkx.blog.takesnapshoot; 

import java.io.File; 
import java.io.IOException; 
import java.util.Date; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.collections.ObservableList; 
import javafx.embed.swing.SwingFXUtils; 
import javafx.event.ActionEvent; 
import javafx.scene.Scene; 
import javafx.scene.SnapshotParameters; 
import javafx.scene.chart.BarChart; 
import javafx.scene.chart.CategoryAxis; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.XYChart; 
import javafx.scene.control.Button; 
import javafx.scene.image.WritableImage; 
import javafx.scene.layout.AnchorPane; 
import javafx.stage.Stage; 
import javax.imageio.ImageIO; 

/** 
* 
* @author Patrick Ott <[email protected]> 
*/ 
public class TakeSnapShoot extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     AnchorPane root = new AnchorPane(); 
     Scene scene = new Scene(root, 1024, 768); 
     ObservableList<String> observableArrayList = FXCollections.observableArrayList(); 
     observableArrayList.add("Kitchen"); 
     observableArrayList.add("Living Room"); 

     CategoryAxis xAxis = new CategoryAxis(); 
     NumberAxis yAxis = new NumberAxis(); 
     final BarChart barChart = new BarChart(xAxis, yAxis); 

     xAxis.setLabel("Room"); 

     XYChart.Series series = new XYChart.Series(); 
     series.getData().add(new XYChart.Data<String, Number>("Kitchen", 1245)); 
     series.getData().add(new XYChart.Data<String, Number>("Living Room", 245)); 
     series.getData().add(new XYChart.Data<String, Number>("Child 1", 3445)); 

     barChart.getData().add(series); 
     root.getChildren().add(barChart); 

     Button snapShotBtn = new Button("Take a Snapshot"); 
     root.getChildren().add(snapShotBtn); 
     snapShotBtn.setOnAction((ActionEvent t) -> { 
      try { 
       SnapshotParameters parameters = new SnapshotParameters(); 
       WritableImage wi = new WritableImage(100, 100); 
       WritableImage snapshot = barChart.snapshot(new SnapshotParameters(), wi); 

       File output = new File("snapshot" + new Date().getTime() + ".png"); 
       ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", output); 
      } catch (IOException ex) { 
       Logger.getLogger(TakeSnapShoot.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     }); 


     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 

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

} 

帕特里克

+0

這個偉大的工程,但創建一個非常像素化,模糊圖像。有沒有簡單的方法來避免這種情況? – Katie

+0

@Katie是的,增加WriteableImage的大小 WritableImage wi = new WritableImage(800,600); – Patrick