如何将JavaFX 2中的场景图形的内容输出到图像 [英] How to output the content of a Scenegraph in JavaFX 2 to an Image
本文介绍了如何将JavaFX 2中的场景图形的内容输出到图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将JavaFX 2中 Scene
图表的内容输出到图像
。实际上,我正在开发一款基本上设计卡片的应用程序。因此,用户只需单击各种选项即可自定义场景。最后,我想将场景内容导出到Image文件。我该怎么做?
How to output the content of a Scene
graph in JavaFX 2 to an Image
. Actually, I am working on an app, which basically designs cards. So, the user just clicks to the various options to customize the scene. Finally I would like to export the scene content to an Image file. How do I do that ?
推荐答案
在FX 2.2中出现了新的快照功能。你可以说
In FX 2.2 new snapshot feature appeared for that matter. You can just say
WritableImage snapshot = scene.snapshot(null);
使用旧版FX,您可以使用AWT Robot。这不是一个很好的方法,因为它需要整个AWT堆栈才能启动。
With older FX you can use AWT Robot. This is not very good approach as it requires whole AWT stack to start.
// getting screen coordinates of a node (or whole scene)
Bounds b = node.getBoundsInParent();
int x = (int)Math.round(primaryStage.getX() + scene.getX() + b.getMinX());
int y = (int)Math.round(primaryStage.getY() + scene.getY() + b.getMinY());
int w = (int)Math.round(b.getWidth());
int h = (int)Math.round(b.getHeight());
// using ATW robot to get image
java.awt.Robot robot = new java.awt.Robot();
java.awt.image.BufferedImage bi = robot.createScreenCapture(new java.awt.Rectangle(x, y, w, h));
// convert BufferedImage to javafx.scene.image.Image
java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
// or you can write directly to file instead
ImageIO.write(bi, "png", stream);
Image image = new Image(new java.io.ByteArrayInputStream(stream.toByteArray()), w, h, true, true);
这篇关于如何将JavaFX 2中的场景图形的内容输出到图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文