JavaFX – how to create screenshots of snapshot / (invisible) WebView
•
Java
I want to create a snapshot / screenshot / image in WebView in JavaFX (8) This WebView does not need to be visible (in my case)
Seeing my sample code, when WebView (or its parent scrollpane) is visible = false, the screenshot will not work (emtpy / blank respectively)
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.V@R_765_2419@; 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 the WebView/ScrollPane scrollpane.setMaxHeight(0); //WORKAROUND: hide the WebView/ScrollPane scrollpane.setMinWidth(0); //WORKAROUND: hide the WebView/ScrollPane scrollpane.setMinHeight(0); //WORKAROUND: hide the WebView/ScrollPane //scrollpane.setVisible(false); //when WebView is 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 is called,the WebPage may not has fully finished rendering! //so,wait for few seceonds before making the 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"); V@R_765_2419@ root = new V@R_765_2419@(); 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); } }
Solution
Use robot class to simulate keys (FN and PrintScreen), load and crop
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码