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) {
;
}
});
}
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;
}
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);
}
}
}
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);
}
}
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();
}
Aggregations