i want create snapshot/screenshot/image webview in javafx(8).
this webview not need visible (in case).
my question:
possible (in way), create screenshot/image webview,
when webview not visible (or not added visible container)?
see example code, when webview (or it's parent scrollpane) visible=false,
screenshot won't work (respectively is emtpy/blank).
example code:
package test; import javafx.animation.keyframe; import javafx.animation.timeline; import javafx.application.application; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.concurrent.worker; import javafx.geometry.pos; import javafx.scene.scene; import javafx.scene.snapshotresult; import javafx.scene.control.label; import javafx.scene.control.scrollpane; import javafx.scene.image.imageview; import javafx.scene.layout.vbox; import javafx.scene.web.webview; import javafx.stage.stage; import javafx.util.duration; public class javafxapplication extends application { @override public void start(stage primarystage) { imageview webviewpreviewimage = new imageview(); label waitlabel = new label("please wait..."); webview webview = new webview(); webview.setmaxheight(480d); webview.setminheight(480d); webview.setmaxwidth(640d); webview.setminwidth(640d); webview.setzoom(0.4); scrollpane scrollpane = new scrollpane(webview); scrollpane.sethbarpolicy(scrollpane.scrollbarpolicy.never); scrollpane.setvbarpolicy(scrollpane.scrollbarpolicy.never); scrollpane.setmaxwidth(0); //workaround: hide webview/scrollpane scrollpane.setmaxheight(0); //workaround: hide webview/scrollpane scrollpane.setminwidth(0); //workaround: hide webview/scrollpane scrollpane.setminheight(0); //workaround: hide webview/scrollpane //scrollpane.setvisible(false); //when webview invisible, snapshot doesn't work! webview.getengine().getloadworker().stateproperty().addlistener(new changelistener<worker.state>() { @override public void changed(observablevalue ov, worker.state oldstate, worker.state newstate) { if (newstate == worker.state.succeeded) { //when succeeded called, webpage may not has finished rendering! //so, wait few seceonds before making screenshot... timeline timeline = new timeline(new keyframe( duration.millis(1500), ae -> takesnapshot())); timeline.play(); } } private keyframe takesnapshot() { webview.snapshot((snapshotresult param) -> { webviewpreviewimage.setimage(param.getimage()); webviewpreviewimage.setfitheight(240d); webviewpreviewimage.setfitwidth(320d); webviewpreviewimage.setpreserveratio(true); waitlabel.setvisible(false); return null; }, null, null); return null; } }); webview.getengine().load("http://www.bing.com"); vbox root = new vbox(); root.setalignment(pos.center); root.setspacing(10d); root.getchildren().add(waitlabel); root.getchildren().add(scrollpane); root.getchildren().add(webviewpreviewimage); scene scene = new scene(root, 800, 600); primarystage.setscene(scene); primarystage.show(); } public static void main(string[] args) { launch(args); } }
use robot class simulate key presses (fn , printscreen), load , crop it.
Comments
Post a Comment