Example usage for javafx.embed.swing SwingFXUtils fromFXImage

List of usage examples for javafx.embed.swing SwingFXUtils fromFXImage

Introduction

In this page you can find the example usage for javafx.embed.swing SwingFXUtils fromFXImage.

Prototype

public static BufferedImage fromFXImage(Image img, BufferedImage bimg) 

Source Link

Document

Snapshots the specified JavaFX Image object and stores a copy of its pixels into a BufferedImage object, creating a new object if needed.

Usage

From source file:Main.java

private void saveLayoutAsImage() {
    FileChooser fc = new FileChooser();
    fc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Portable Network Graphics(*.png)",
            Collections.singletonList("*.png")));
    File output = fc.showSaveDialog(boundsLayoutNode.getScene().getWindow());
    if (output == null) {
        return;/*from ww w. jav a 2  s .com*/
    } else {
        String fn = output.getName();
        if (!fn.toLowerCase().endsWith(".png")) {
            output = new File(output.getParent(), fn + ".png");
        }
    }

    try {
        WritableImage wImg = boundsLayoutNode.snapshot(null, null);
        //WritableImage wImg = mainRect.snapshot(null, null);
        BufferedImage bImg = SwingFXUtils.fromFXImage(wImg, null);
        ImageIO.write(bImg, "png", output);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.rptools.tokentool.controller.TokenTool_Controller.java

private boolean writeTokenImage(File tokenFile) {
    try {//from   w  ww  .ja v  a  2 s  . com
        Image tokenImage;
        if (clipPortraitCheckbox.isSelected())
            tokenImage = ImageUtil.resizeCanvas(tokenImageView.getImage(), getOverlayWidth(),
                    getOverlayHeight());
        else
            tokenImage = tokenImageView.getImage();

        return ImageIO.write(SwingFXUtils.fromFXImage(tokenImage, null), "png", tokenFile);
    } catch (IOException e) {
        log.error("Unable to write token to file: " + tokenFile.getAbsolutePath(), e);
    } catch (IndexOutOfBoundsException e) {
        log.error("Image width/height out of bounds: " + getOverlayWidth() + " x " + getOverlayHeight(), e);
    }

    return false;
}

From source file:org.wandora.application.gui.topicpanels.webview.WebViewPanel.java

public Image getSnapshot() {
    WritableImage image = webView.snapshot(null, null);
    BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);
    return bufferedImage;
}

From source file:uk.bl.dpt.qa.gui.DissimilarGUIThread.java

/**
 * overlay heatmap on image// ww  w .ja  va  2  s .  c o m
 */
private void internalOverlayHeatmap() {

    gRightImageSave = imageRight.getImage();

    if (!gHeatmapGenerated) {

        internalBeforeGUIThread();

        //threaded load so GUI doesn't hang
        Task<Integer> task = new Task<Integer>() {

            @Override
            protected Integer call() throws Exception {
                BufferedImage image = SwingFXUtils.fromFXImage(imageRight.getImage(), null);

                if (gHeatmap == null) {
                    //re-generate heatmap (must be on a re-load)
                    try {
                        gHeatmap = Imaging.getBufferedImage(gResults.get(gCurrentRecord).getHeatmapTemp());
                    } catch (IOException | ImageReadException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                for (int y = 0; y < image.getHeight(); y++) {
                    for (int x = 0; x < image.getWidth(); x++) {
                        int rgb = image.getRGB(x, y);

                        Color heatmapColor = new Color(gHeatmap.getRGB((x / DissimilarV2.SSIMWINDOWSIZE),
                                (y / DissimilarV2.SSIMWINDOWSIZE)));
                        int heatmapPixel = heatmapColor.getGreen();//&maxPixelValue;
                        if (heatmapColor.getGreen() != heatmapColor.getBlue()
                                && heatmapColor.getBlue() != heatmapColor.getRed()) {
                            gLogger.error("Heatmap error (should not happen)");
                        }

                        double factor = 1 - (((double) heatmapPixel / 255));

                        Color col = new Color(rgb);
                        int red = (int) (factor * col.getRed());
                        int green = (int) (factor * col.getGreen());
                        int blue = (int) (factor * col.getBlue());

                        if (red < 0)
                            red = 0;
                        if (green < 0)
                            green = 0;
                        if (blue < 0)
                            blue = 0;
                        col = new Color(red, green, blue);

                        image.setRGB(x, y, col.getRGB());
                    }
                }

                gHeatmapImage = SwingFXUtils.toFXImage(image, null);
                gHeatmapGenerated = true;

                Platform.runLater(new Runnable() {
                    //@Override
                    public void run() {
                        imageRight.setImage(gHeatmapImage);
                    }
                });

                internalAfterGUIThread();

                return 1;
            }

        };

        progressIndicator.progressProperty().bind(task.progressProperty());
        Thread loader = new Thread(task);
        loader.setDaemon(true);
        loader.start();

    } else {
        imageRight.setImage(gHeatmapImage);
    }

}

From source file:org.beryx.viewreka.fxapp.Viewreka.java

public void exportChart() {
    ViewPane<?> viewPane = getCurrentViewPane();
    if (viewPane == null)
        return;//from w w  w  .  ja v  a 2s . c  o m

    FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle("Export chart");

    List<String> acceptedFormats = Arrays.asList("png", "gif");
    ObservableList<ExtensionFilter> extensionFilters = fileChooser.getExtensionFilters();
    acceptedFormats
            .forEach(ext -> extensionFilters.add(new FileChooser.ExtensionFilter("PNG files", "*." + ext)));
    GuiSettings guiSettings = guiSettingsManager.getSettings();
    String initialDirPath = guiSettings.getProperty(PROP_CHART_EXPORT_DIR,
            guiSettings.getMostRecentProjectDir().getAbsolutePath(), false);
    File initialDir = new File(initialDirPath);

    fileChooser.setInitialDirectory(initialDir);
    File file = fileChooser.showSaveDialog(getScene().getWindow());
    if (file == null)
        return;

    if (file.getParentFile() != null) {
        guiSettings.setProperty(PROP_CHART_EXPORT_DIR, file.getParent());
    }

    String extension = FilenameUtils.getExtension(file.getName()).toLowerCase();
    if (!acceptedFormats.contains(extension)) {
        extension = acceptedFormats.get(0);
    }

    WritableImage chartImage = viewPane.getChartImage();

    try {
        ImageIO.write(SwingFXUtils.fromFXImage(chartImage, null), extension, file);
    } catch (IOException e) {
        Dialogs.error("Chart export failed", "Cannot export the chart image", e);
    }
}

From source file:snpviewer.SnpViewer.java

public void drawPaneToPng() {
    if (chromosomeSelector.getSelectionModel().isEmpty()) {
        return;//from ww  w  .  j av  a  2 s  .c  om
    }
    FileChooser fileChooser = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("PNG Image Files (*.png)", "*.png");
    fileChooser.getExtensionFilters().add(extFilter);
    fileChooser.setTitle("Save View as Image (.png) File...");
    File pngFile = fileChooser.showSaveDialog(mainWindow);
    if (pngFile == null) {
        return;
    } else if (!pngFile.getName().endsWith(".png")) {
        pngFile = new File(pngFile.getAbsolutePath() + ".png");
    }
    WritableImage image = chromSplitPane.snapshot(null, null);
    if (image == null) {
        Dialogs.showErrorDialog(null, "Error attempting to convert" + " dynamic view to image file",
                "PNG conversion failed", "SNP Viewer");
        return;
    }
    try {
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", pngFile);
        Dialogs.showInformationDialog(null, "Sucessfully saved current view " + "to " + pngFile.getName(),
                "Image Saved", "SNP Viewer");
    } catch (IOException ex) {
        Dialogs.showErrorDialog(null, "Error attempting to convert" + " dynamic view to image file",
                "PNG conversion failed", "SNP Viewer", ex);
    }
}