JavaFX for server-side image generation

后端 未结 3 1753
野的像风
野的像风 2020-12-08 23:25

This could sound strange but I want to generate my chart images on server side using JavaFX. Because JavaFX has nice canvas API to perform image transformations joins and p

3条回答
  •  遥遥无期
    2020-12-09 00:08

    In case other people are looking for this, this is a much simpler way. Using JavaFX 2.2 i was able to perform the following operations.

        waitForInit = new Semaphore(0);
        root = new Group();
        root.getChildren().add(jfxnode);
        FxPlatformExecutor.runOnFxApplication(() -> {
            snapshot = jfxnode.snapshot(new SnapshotParameters(), null);
            waitForInit.release();
        });
    
        waitForInit.acquireUninterruptibly();
        BufferedImage bi = SwingFXUtils.fromFXImage(snapshot, null);
    

    There is no need to add the node to a group. From there you can do any operation you want with the image.

    The FxPlatformExecutor is from a JME3-JFX library I am using for my project. See: https://github.com/empirephoenix/JME3-JFX/blob/master/src/main/java/com/jme3x/jfx/FxPlatformExecutor.java

    You can easily create the runOnFxApplication() method or create the FxPlatformExecutor class.

    Here is the code.

    package com.jme3x.jfx;
    
    import javafx.application.Platform;
    
    /**
     * TODO This Class should be replaced by some Workmanager implemntation
     * in the future
     * @author Heist
     */
    public class FxPlatformExecutor {
    
        public static void runOnFxApplication(Runnable task) {
            if (Platform.isFxApplicationThread()) {
                task.run();
            } else {
                Platform.runLater(task);
            }
        }
    }
    

    I did not write this code, the github link is above.

提交回复
热议问题