Search in sources :

Example 6 with WritableImage

use of javafx.scene.image.WritableImage in project jphp by jphp-compiler.

the class UXCanvas method writeImageAsync.

@Signature
public void writeImageAsync(final String format, final Memory outputStream, @Nullable final javafx.scene.paint.Color transparentColor, @Nullable final Invoker callback, final Environment env) throws IOException {
    Platform.runLater(() -> {
        SnapshotParameters param = new SnapshotParameters();
        param.setDepthBuffer(true);
        param.setFill(javafx.scene.paint.Color.TRANSPARENT);
        try {
            final WritableImage image = getWrappedObject().snapshot(param, null);
            new Thread(() -> {
                BufferedImage bImage = SwingFXUtils.fromFXImage(image, null);
                if (transparentColor != null) {
                    final int markerRGB = new Color((int) (transparentColor.getRed() * 255), (int) (transparentColor.getGreen() * 255), (int) (transparentColor.getBlue() * 255)).getRGB();
                    ImageFilter filter = new RGBImageFilter() {

                        public final int filterRGB(int x, int y, int rgb) {
                            if (markerRGB == rgb) {
                                return 0x00FFFFFF & rgb;
                            } else {
                                return rgb;
                            }
                        }
                    };
                    ImageProducer ip = new FilteredImageSource(bImage.getSource(), filter);
                    Image image1 = Toolkit.getDefaultToolkit().createImage(ip);
                    bImage = imageToBufferedImage(image1);
                }
                try {
                    OutputStream out = Stream.getOutputStream(env, outputStream);
                    if (out == null) {
                        throw new IOException();
                    }
                    ImageIO.write(bImage, format, out);
                    if (callback != null) {
                        Platform.runLater(() -> callback.callAny(true));
                    }
                    Stream.closeStream(env, out);
                } catch (IOException e) {
                    if (callback != null) {
                        Platform.runLater(() -> callback.callAny(false));
                    }
                    env.wrapThrow(e);
                }
            }).start();
        } catch (IllegalArgumentException e) {
            ;
        }
    });
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) WritableImage(javafx.scene.image.WritableImage) WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) Signature(php.runtime.annotation.Reflection.Signature)

Example 7 with WritableImage

use of javafx.scene.image.WritableImage in project jphp by jphp-compiler.

the class UXImage method ofNative.

@Signature
public static UXImage ofNative(Environment env, Memory memory) throws Throwable {
    MemoryOperation operation = MemoryOperation.get(BufferedImage.class, null);
    if (operation != null) {
        BufferedImage convert = (BufferedImage) operation.convert(env, env.trace(), memory);
        WritableImage image = SwingFXUtils.toFXImage(convert, null);
        return new UXImage(env, image);
    }
    return null;
}
Also used : WritableImage(javafx.scene.image.WritableImage) MemoryOperation(php.runtime.memory.support.MemoryOperation) BufferedImage(java.awt.image.BufferedImage) Signature(php.runtime.annotation.Reflection.Signature)

Example 8 with WritableImage

use of javafx.scene.image.WritableImage in project jgnash by ccavanaugh.

the class ChartUtilities method saveChart.

static void saveChart(final Pane pane) {
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle(ResourceUtils.getString("Title.SaveFile"));
    fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("PNG", "*.png"));
    final File file = fileChooser.showSaveDialog(MainView.getPrimaryStage());
    if (file != null) {
        final WritableImage image = takeSnapshot(pane);
        try {
            final String type = FileUtils.getFileExtension(file.toString().toLowerCase(Locale.ROOT));
            ImageIO.write(SwingFXUtils.fromFXImage(image, null), type, file);
        } catch (final IOException e) {
            StaticUIMethods.displayException(e);
        }
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) FileChooser(javafx.stage.FileChooser) IOException(java.io.IOException) File(java.io.File)

Example 9 with WritableImage

use of javafx.scene.image.WritableImage in project Gargoyle by callakrsos.

the class FxUtil method snapShot.

public static void snapShot(Node target, OutputStream out, int requestWidth, int requestHeight, Consumer<Exception> errorCallback) {
    if (target == null)
        throw new NullPointerException("target Node is empty.");
    if (out == null)
        throw new NullPointerException("target Stream is empty.");
    SnapshotParameters params = new SnapshotParameters();
    params.setDepthBuffer(true);
    // params.setFill(Color.TRANSPARENT);
    WritableImage wi = null;
    if (requestWidth >= 0 || requestHeight >= 0) {
        wi = new WritableImage(requestWidth, requestHeight);
    }
    WritableImage snapshot = target.snapshot(params, wi);
    try {
        boolean isSuccess = snapShot(out, snapshot);
        LOGGER.debug("Write Image result {}", isSuccess);
    } catch (IOException e) {
        errorCallback.accept(e);
    }
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) IOException(java.io.IOException)

Example 10 with WritableImage

use of javafx.scene.image.WritableImage in project tray by qzind.

the class WebApp method capture.

/**
 * Sets up capture to run on JavaFX thread and returns snapshot of rendered page
 *
 * @param model Data about the html to be rendered for capture
 * @return BufferedImage of the rendered html
 */
public static synchronized BufferedImage capture(final WebAppModel model) throws Throwable {
    final AtomicReference<BufferedImage> capture = new AtomicReference<>();
    complete.set(false);
    thrown.set(null);
    // ensure JavaFX has started before we run
    if (webView == null) {
        throw new IOException("JavaFX has not been started");
    }
    // run these actions on the JavaFX thread
    Platform.runLater(new Thread() {

        public void run() {
            try {
                pageWidth = model.getWebWidth();
                pageHeight = model.getWebHeight();
                pageZoom = model.getZoom();
                webView.setMinSize(100, 100);
                webView.setPrefSize(100, 100);
                webView.autosize();
                // FIXME - will not capture without showing stage
                stage.show();
                stage.toBack();
                // ran when engine reaches SUCCEEDED state, takes snapshot of loaded html
                snap = new PauseTransition(Duration.millis(100));
                snap.setOnFinished(new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent actionEvent) {
                        try {
                            log.debug("Attempting image capture");
                            WritableImage snapshot = webView.snapshot(new SnapshotParameters(), null);
                            capture.set(SwingFXUtils.fromFXImage(snapshot, null));
                            complete.set(true);
                        } catch (Throwable t) {
                            thrown.set(t);
                        } finally {
                            // hide stage so users won't have to manually close it
                            stage.hide();
                        }
                    }
                });
                // actually begin loading the html
                if (model.isPlainText()) {
                    webView.getEngine().loadContent(model.getSource(), "text/html");
                } else {
                    webView.getEngine().load(model.getSource());
                }
            } catch (Throwable t) {
                thrown.set(t);
            }
        }
    });
    Throwable t = null;
    while (!complete.get() && (t = thrown.get()) == null) {
        log.trace("Waiting on capture..");
        try {
            Thread.sleep(1000);
        } catch (Exception ignore) {
        }
    }
    if (t != null) {
        throw t;
    }
    return capture.get();
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) ActionEvent(javafx.event.ActionEvent) EventHandler(javafx.event.EventHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) PauseTransition(javafx.animation.PauseTransition) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ReflectException(org.joor.ReflectException) IOException(java.io.IOException)

Aggregations

WritableImage (javafx.scene.image.WritableImage)70 SnapshotParameters (javafx.scene.SnapshotParameters)24 PixelWriter (javafx.scene.image.PixelWriter)21 PixelReader (javafx.scene.image.PixelReader)13 IOException (java.io.IOException)12 Color (javafx.scene.paint.Color)11 BufferedImage (java.awt.image.BufferedImage)9 File (java.io.File)8 Image (javafx.scene.image.Image)6 ImageView (javafx.scene.image.ImageView)6 Group (javafx.scene.Group)5 Random (java.util.Random)4 Canvas (javafx.scene.canvas.Canvas)4 GraphicsContext (javafx.scene.canvas.GraphicsContext)4 Texture (com.almasb.fxgl.texture.Texture)3 OutputStream (java.io.OutputStream)3 Point2D (javafx.geometry.Point2D)3 Scene (javafx.scene.Scene)3 Background (javafx.scene.layout.Background)3 Scale (javafx.scene.transform.Scale)3