How to output the content of scene graphics in JavaFX 2 to images
•
Java
How to output the content of scene graph in JavaFX 2 to image In fact, I'm developing an application that basically designs cards Therefore, users can customize the scene by clicking various options Finally, I want to export the scene content to the image file What do I do?
Solution
A new snapshot function appears in FX 2.2 You can say
WritableImage snapshot = scene.snapshot(null);
With the old FX, you can use AWT robot This is not a good method because it requires the entire 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()),h,true,true);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码