将图像另存为PNG

时间:2016-04-19 07:12:24

标签: java image

我想从我的阵列中保存一个点图表作为图像(PNG),这个Java程序我可以显示我的Schatter Diagram但我不知道如何保存它。也许用ImageIO.write,怎么样? 任何人都可以给我一些建议来解决这个问题。谢谢

public class Graph extends Application {

public void start(Stage primaryStage) {
Pane root = new Pane();
int[] mydata = {
        12, 9, 0, 1, 38, 19, 21, 72, 33, 83, 14, 10, 65, 46, 10, 17, 27, 38, 65, 98, 8, 58, 38, 79, 37, 69, 26, 15};

        NumberAxis xAxis = new NumberAxis();
NumberAxis yAxis = new NumberAxis();
ScatterChart scatterChart=new ScatterChart(xAxis,yAxis);


XYChart.Series data=new XYChart.Series();
for (int i=0; i< mydata.length; i++) {
    data.getData().add(new XYChart.Data(i,mydata[i]));
}       
 scatterChart.getData().addAll(data);


root.getChildren().add(scatterChart);
Scene scene = new Scene(root, 600, 400);

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

File file = new File("c:\\Images\\Image.png");

// and now ?????

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

2 个答案:

答案 0 :(得分:0)

正如here所述,您需要代码:

...
//and now
WritableImage image = scatterChart.snapshot(new SnapshotParameters(), null);
try {
  ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file);
} catch (IOException e) {
  // TODO: handle exception here
}

答案 1 :(得分:0)

您可以将绘图或图表绘制到像BufferedImage这样的图像上。之后将BufferedImage保存为文件。你可以试试这样的东西:

resultBook.lowestOnlinePricePromise.resolve is not a function

将图表保存为图像文件(例如class MyScatterPlot { private BufferedImage buf; //Constructors, initializations not shown. public void saveBufferAsImage(String pathname){ String fmt = ""; if(!(pathname == null || pathname.equals(""))){ fmt = pathname.substring(pathname.lastIndexOf(".")+1); File outputfile = new File(pathname); try{ ImageIO.write(buf, fmt, outputfile); }catch(IOException ioe){System.out.println("Unable to save file");} } } public void drawImage(){ buf = new BufferedImage (200, 200, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = buf.createGraphics(); g2d.fillRect(10, 10, 150, 150); //draw your image here (example only) g2d.dispose(); } } ):

.png