The ultimate goal is to be able to record the output of a WebView at 30fps or better, perhaps by setting up an FBO for javafx? I could then pull out frames at whatever framerate I wanted.
I've poked around some and I came across UploadingPainter in ViewScene, which makes me think that this is possible. The struggle is that this is seemingly under the hood and somewhat new to me.
Anyone know of a way to make something like this work?
This is the code that I came across during debugging:
@Override
public void setStage(GlassStage stage) {
super.setStage(stage);
if (stage != null) {
WindowStage wstage = (WindowStage)stage;
if (wstage.needsUpdateWindow() || GraphicsPipeline.getPipeline().isUploading()) {
if (Pixels.getNativeFormat() != Pixels.Format.BYTE_BGRA_PRE ||
ByteOrder.nativeOrder() != ByteOrder.LITTLE_ENDIAN) {
throw new UnsupportedOperationException(UNSUPPORTED_FORMAT);
}
painter = new UploadingPainter(this);
} else {
painter = new PresentingPainter(this);
}
painter.setRoot(getRoot());
paintRenderJob = new PaintRenderJob(this, PaintCollector.getInstance().getRendered(), painter);
}
}
Here is an example of capturing an animation in a WebView.
The images captured from the web view are placed in a Paginator for viewing purposes just so that it is easy to review them. You could use
SwingFXUtils
andImageIO
to write them out to files instead if you wish. If you want to get the resultant images into a buffer, you could use theirPixelReader
.It doesn't quite work the way I wanted it to. I wanted to snapshot the WebView without placing it in a visible stage. Taking snapshots of nodes that are not in a Stage works fine for every other node type in JavaFX (as far as I know), however, for some weird reason, it does not work for WebView. So the sample actually creates a new Stage behind the display window that displays the image sequence for the animation capture result. I'm aware that not exactly what you want, but it is what it is...
To fine-tune the animation sequence capture, you could review this info on AnimationTimers in JavaFX.
If you need to make the thing "headless", so that a visible stage is not required, you could try this gist by danialfarid which performs "Java Image Capture, HTML Snapshot, HTML to image" (though I did not write the linked gist and have not tried it).
The gist is based upon the Monocle glass rendering toolkit for JavaFX systems. This toolkit supports software based headless rendering on any system.
From the Monocle Documentation:
If the JavaFX Monocle based approach ends up not working out for you, you could consider another (not JavaFX related) headless HTML rendering kit, such as PhantomJS.