package com.itdominator.webcapimgfx; import javafx.fxml.FXML; import javafx.stage.Stage; import javafx.stage.FileChooser; import javafx.scene.web.WebView; import javafx.scene.web.WebEngine; import javafx.scene.control.ScrollPane; import javafx.scene.image.ImageView; import javafx.scene.control.TextField; import javafx.scene.image.Image; import javafx.scene.control.Button; import javafx.scene.input.MouseEvent; import javafx.scene.input.KeyEvent; import javafx.scene.input.KeyCode; import javafx.scene.input.ScrollEvent; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.beans.Observable; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.InvalidationListener; import javax.imageio.ImageIO; import java.io.IOException; import java.io.File; import javafx.embed.swing.SwingFXUtils; import javafx.scene.image.WritableImage; import javafx.scene.SnapshotParameters; import javafx.concurrent.Worker; public class Controller { // Classes // FXML Stuff @FXML private WebView webView = new WebView(); @FXML private ScrollPane scrollWindow; @FXML private ImageView imageView; @FXML private TextField urlOfPhoto; @FXML private Button saveBttn; // Generics private final DoubleProperty zoomProperty = new SimpleDoubleProperty(200); private FileChooser fileChooser = new FileChooser(); private Stage fileChooserStage = new Stage(); private WritableImage image; private WebEngine webEngine; @FXML void initialize() { assert webView != null : "fx:id=\"webView\" was not injected: check your FXML file 'Window.fxml'."; assert scrollWindow != null : "fx:id=\"scrollWindow\" was not injected: check your FXML file 'Window.fxml'."; assert imageView != null : "fx:id=\"imageView\" was not injected: check your FXML file 'Window.fxml'."; assert urlOfPhoto != null : "fx:id=\"urlOfPhoto\" was not injected: check your FXML file 'Window.fxml'."; zoomProperty.addListener(new InvalidationListener() { @Override public void invalidated(Observable arg0) { imageView.setFitWidth(zoomProperty.get() * 4); imageView.setFitHeight(zoomProperty.get() * 3); } }); scrollWindow.addEventFilter(ScrollEvent.SCROLL,new EventHandler() { @Override public void handle(ScrollEvent event) { if (event.isControlDown()) { if (event.getDeltaY() > 0) { plus(); } else if (event.getDeltaY() < 0) { minus(); } } } }); fileChooser.setTitle("Save Image"); fileChooser.getExtensionFilters().addAll( new FileChooser.ExtensionFilter("PNG", "*.png") ); // Need an initial load to then set proper deminsions for other page loads webEngine = webView.getEngine(); webEngine.load("https://www.google.com/"); manageWebView(); } @FXML void zoomIn(MouseEvent event) { plus(); } @FXML void zoomOut(MouseEvent event) { minus(); } @FXML void setScrllFocus(MouseEvent event) { scrollWindow.requestFocus(); } @FXML void takeWebsitePhoto(ActionEvent event) throws Exception { startRun(); } @FXML void clearData(ActionEvent event) { urlOfPhoto.setText(""); imageView.setImage(new Image("file://")); saveBttn.setDisable(true); } @FXML void onEnter(KeyEvent event) throws Exception { if (event.getCode() == KeyCode.ENTER) { startRun(); } } @FXML void save(ActionEvent event) { try { String path = fileChooser.showSaveDialog(fileChooserStage).toString(); if (!path.contains(".png")) { path += ".png"; } File file = new File(path); ImageIO.write(SwingFXUtils.fromFXImage(image, null), "png", file); } catch (IOException e) { } } private void startRun() throws Exception { webEngine.load(urlOfPhoto.getText()); webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> { if (newState == Worker.State.SUCCEEDED) { manageWebView(); image = webView.snapshot(new SnapshotParameters(), null); imageView.setImage(image); saveBttn.setDisable(false); } }); } private void manageWebView() { // Manage webview size to get full size of page Double width = Double.parseDouble(webView.getEngine().executeScript("document.width").toString()), height = Double.parseDouble(webView.getEngine().executeScript("document.height").toString()); if (width<100 || width>1080) { width = 1080.0; } if (height<100 || height>2048) { height = 1920.0; } webView.setMinSize(width, height); } private void plus() { zoomProperty.set(zoomProperty.get() * 1.1); } private void minus() { zoomProperty.set(zoomProperty.get() / 1.1); } }