Java 如何在webview中通过鼠标事件选择图像的特定区域,并通过按键显示白色屏幕

Java 如何在webview中通过鼠标事件选择图像的特定区域,并通过按键显示白色屏幕,java,javafx,Java,Javafx,我必须选择WebView中的图像的特定部分,裁剪图像并将裁剪后的图像保存到另一个位置,同时也保存主图像,我还希望在图像的web视图中以特定角度旋转图像 我已经试过这个代码给贝娄 import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.logging.Level; import java

我必须选择WebView中的图像的特定部分,裁剪图像并将裁剪后的图像保存到另一个位置,同时也保存主图像,我还希望在图像的web视图中以特定角度旋转图像

我已经试过这个代码给贝娄

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.sun.javafx.application.PlatformImpl;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.SplitPane;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class MediaMonitoring extends Application {

public static void main(String[] args) {
    Application.launch(args);
}

private static WebView mainWebView;
private static WebEngine webEngine;
private static WebView imageWebView;
private static WebView detailWebView;
private static Scene mainScene;
private static Scene scene;

@Override
public void start(Stage primaryStage) throws Exception {
    PlatformImpl.startup(() -> {
        mainWebView = new WebView();
        SplitPane mainSplitPane = new SplitPane();
        SplitPane imageSplitPane = new SplitPane();
        SplitPane detailSplitPane = new SplitPane();
        detailSplitPane.setOrientation(Orientation.VERTICAL);
        imageSplitPane.getItems().add(0, mainWebView);
        scene = new Scene(imageSplitPane);
        webEngine = mainWebView.getEngine();
        webEngine.setJavaScriptEnabled(true);
        imageWebView = new WebView();
        detailWebView = new WebView();
        mainScene = new Scene(mainSplitPane);
        imageWebView.addEventFilter(KeyEvent.KEY_RELEASED, (KeyEvent e) -> {
            if (e.getCode() == KeyCode.ADD || e.getCode() == KeyCode.EQUALS || e.getCode() == KeyCode.PLUS) {
                imageWebView.setZoom(imageWebView.getZoom() * 1.1);
            } else if (e.getCode() == KeyCode.SUBTRACT || e.getCode() == KeyCode.MINUS) {
                imageWebView.setZoom(imageWebView.getZoom() / 1.1);
            }
        });

        Rectangle rectangle = new Rectangle(0, 0, 0, 0);
        rectangle.setVisible(true);

        imageWebView.addEventFilter(MouseEvent.ANY, (MouseEvent mouseEvent) -> {
                System.out.println(mouseEvent.getEventType() + "CLicking    >>>>>>>>>>>>");
                if (mouseEvent.getEventType() == MouseEvent.MOUSE_CLICKED) {
                    rectangle.setVisible(true);
                    rectangle.setTranslateX(mouseEvent.getX());
                    rectangle.setTranslateY(mouseEvent.getY());
                }
                if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED && rectangle.isVisible()) {
                    rectangle.setWidth(mouseEvent.getX() - rectangle.getTranslateX());
                    rectangle.setHeight(mouseEvent.getY() - rectangle.getTranslateY());
                }

                if (mouseEvent.getEventType() == MouseEvent.MOUSE_RELEASED)
                    rectangle.setVisible(true);

            /*
             * Pane pane = new Pane(imageWebView, rectangle); final Scene scene1 = new
             * Scene(pane, 400, 300);
             * 
             * 
             * if (!imageSplitPane.getItems().isEmpty())
             * imageSplitPane.getItems().remove(0);
             * 
             * 
             * imageSplitPane.getItems().add(0,scene1.getFocusOwner());
             */
            //primaryStage.setScene(scene1);
            //primaryStage.show();

                //mainScene
                System.err.printf("X: %.2f, Y: %.2f, Width: %.2f, Height: %.2f%n", rectangle.getX(), rectangle.getY(),
                        rectangle.getWidth(), rectangle.getHeight());
        });

        webEngine.getLoadWorker().stateProperty()
                .addListener((ObservableValue<? extends State> observable, State oldValue, State newValue) -> {
                    primaryStage.setTitle(webEngine.getLocation());
                    if (newValue == Worker.State.SUCCEEDED
                            && mainWebView.getEngine().getLoadWorker().getState() == Worker.State.SUCCEEDED) {
                        storeDetail(webEngine);
                    }
                    if (newValue == Worker.State.SCHEDULED) {
                        Platform.runLater(() -> showMainScreen(webEngine.getLocation(), imageSplitPane,
                                mainSplitPane, detailSplitPane, mainScene, primaryStage));
                    }
                });

        webEngine.load("http://pmmstest.ankiti.net/");
        primaryStage.setScene(scene);
    });
    primaryStage.show();
}

static void showMainScreen(String location, SplitPane imageSplitPane, SplitPane mainSplitPane,
        SplitPane detailSplitPane, Scene mainScene, Stage primaryStage) {
    if (location.contains("img")) {
        webEngine.getLoadWorker().cancel();
        if (location.matches("(.*)img=(.*)")) {
            int n = location.indexOf("img=");
            int len = location.length();
            String imageUrl = "file:" + location.substring(n + 4, len).split("&")[0];
            imageWebView.getEngine().load(imageUrl);
            if (location.matches("(.*)target=wb2(.*)")) {
                int n1 = location.indexOf("target=wb2");
                String detailUrl = "" + location.substring(0, n1 + 10);
                detailWebView.getEngine().load(detailUrl);
            }
            if (!imageSplitPane.getItems().isEmpty())
                imageSplitPane.getItems().remove(0);
            if (mainSplitPane.getItems().size() >= 2)
                mainSplitPane.getItems().remove(0, 2);
            if (detailSplitPane.getItems().size() >= 2)
                detailSplitPane.getItems().remove(0, 2);
            imageSplitPane.getItems().add(0, imageWebView);
            imageWebView.setZoom(1.5);
            detailSplitPane.getItems().add(0, mainWebView);
            detailSplitPane.getItems().add(1, detailWebView);
            mainSplitPane.getItems().add(0, imageSplitPane);
            mainSplitPane.getItems().add(1, detailSplitPane);
            primaryStage.setScene(mainScene);
        }
    }
}

static void storeDetail(WebEngine webEngine) {
    if (webEngine.getLocation().contains("noob=1&noshell=1&ini=")) {
        int n4 = webEngine.getLocation().indexOf("ini=");
        int n5 = webEngine.getLocation().indexOf("&output_format=html");
        String find4 = webEngine.getLocation().substring(n4 + 4, n5);
        String html = (String) webEngine.executeScript("document.documentElement.innerText");

        try {
            System.out.write(html.getBytes(StandardCharsets.UTF_8));
        } catch (IOException ex) {
            Logger.getLogger(SwingFXWebView.class.getName()).log(Level.SEVERE, null, ex);
        }

        String html1 = html.replace('|', '\n');
        String h1 = html1.replaceAll("\n", "\r\n");
        String path = "C:/temp";
        path = path + "/";
        File newFile = new File(path + find4 + ".ini");

        try {
            newFile.createNewFile();
            if (newFile.exists()) {
                newFile.delete();
            }
            try (FileWriter writer = new FileWriter(newFile, true)) {
                writer.write(h1);
                writer.flush();
            } catch (IOException ex) {
                Logger.getLogger(SwingFXWebView.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (IOException ex) {
            Logger.getLogger(SwingFXWebView.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (html.contains("date_data_path=")) {
            storeDateData(html);
        }
    }
}

static void storeDateData(String html) {
    int n6 = html.indexOf("date_data_path=");
    int len1 = html.length();
    String find2 = html.substring(n6 + 15, len1 - 1);
    String pathname = "C:";
    String cuts = "cuts";
    pathname += find2 + "/" + cuts;
    File directory = new File(pathname);
    if (!directory.exists()) {
        directory.mkdirs();
    }
    String pathname1 = "C:";
    String db = "db";
    pathname1 += find2 + "/" + db;
    File directory1 = new File(pathname1);
    if (!directory1.exists()) {
        directory1.mkdirs();
    }
    String pathname2 = "C:";
    String ocr = "ocr";
    pathname2 += find2 + "/" + ocr;
    File directory2 = new File(pathname2);
    if (!directory2.exists()) {
        directory2.mkdirs();
    }
    String pathname3 = "C:";
    String output = "output";
    pathname3 += find2 + "/" + output;
    File directory3 = new File(pathname3);
    if (!directory3.exists()) {
        directory3.mkdirs();
    }
    String pathname4 = "C:";
    String scans = "scans";
    pathname4 += find2 + "/" + scans;
    File directory4 = new File(pathname4);
    if (!directory4.exists()) {
        directory4.mkdirs();
    }
}
}
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.nio.charset.StandardCharset;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入com.sun.javafx.application.PlatformImpl;
导入javafx.application.application;
导入javafx.application.Platform;
导入javafx.beans.value.observeValue;
导入javafx.concurrent.Worker;
导入javafx.concurrent.Worker.State;
导入javafx.geometry.Orientation;
导入javafx.scene.scene;
导入javafx.scene.control.SplitPane;
导入javafx.scene.input.KeyCode;
导入javafx.scene.input.KeyEvent;
导入javafx.scene.input.MouseEvent;
导入javafx.scene.layout.Pane;
导入javafx.scene.shape.Rectangle;
导入javafx.scene.web.WebEngine;
导入javafx.scene.web.WebView;
导入javafx.stage.stage;
公共类MediaMonitoring扩展了应用程序{
公共静态void main(字符串[]args){
应用程序启动(args);
}
私有静态WebView mainWebView;
私有静态WebEngine;
私有静态WebView imageWebView;
私有静态WebView detailWebView;
私密静态场景;
私人静态场景;
@凌驾
public void start(Stage primaryStage)引发异常{
平台模板启动(()->{
mainWebView=新建WebView();
SplitPane mainSplitPane=新建SplitPane();
SplitPane imageSplitPane=新建SplitPane();
SplitPane detailSplitPane=新的SplitPane();
detailSplitPane.setOrientation(方向.垂直);
imageSplitPane.getItems().add(0,mainWebView);
场景=新场景(imageSplitPane);
webEngine=mainWebView.getEngine();
setJavaScriptEnabled(true);
imageWebView=新建WebView();
detailWebView=新建WebView();
mainScene=新场景(mainSplitPane);
imageWebView.addEventFilter(KeyEvent.KEY_已发布,(KeyEvent e)->{
如果(e.getCode()==KeyCode.ADD | | e.getCode()==KeyCode.EQUALS | | e.getCode()==KeyCode.PLUS){
setZoom(imageWebView.getZoom()*1.1);
}else if(e.getCode()==KeyCode.SUBTRACT | | e.getCode()==KeyCode.减号){
setZoom(imageWebView.getZoom()/1.1);
}
});
矩形矩形=新矩形(0,0,0,0);
矩形。setVisible(true);
imageWebView.addEventFilter(MouseEvent.ANY,(MouseEvent MouseEvent)->{
System.out.println(mouseEvent.getEventType()+“单击>>>>>”;
if(mouseEvent.getEventType()==mouseEvent.MOUSE_单击){
矩形。setVisible(true);
setTranslateX(mouseEvent.getX());
rectangle.setTranslateY(mouseEvent.getY());
}
if(mouseEvent.getEventType()==mouseEvent.MOUSE\u移动和矩形.isVisible()){
setWidth(mouseEvent.getX()-rectangle.getTranslateX());
setHeight(mouseEvent.getY()-rectangle.getTranslateY());
}
if(mouseEvent.getEventType()==mouseEvent.MOUSE\u已释放)
矩形。setVisible(true);
/*
*窗格窗格=新建窗格(imageWebView,矩形);最终场景场景1=新建
*场景(窗格,400300);
* 
* 
*如果(!imageSplitPane.getItems().isEmpty())
*imageSplitPane.getItems().remove(0);
* 
* 
*imageSplitPane.getItems().add(0,scene1.getFocusOwner());
*/
//初生阶段:场景1;
//primaryStage.show();
//主景
System.err.printf(“X:%.2f,Y:%.2f,宽度:%.2f,高度:%.2f%n”),rectangle.getX(),rectangle.getY(),
rectangle.getWidth(),rectangle.getHeight();
});
webEngine.getLoadWorker().stateProperty()

.addListener((ObservalEvalue)如果有付费图书馆,请推荐我如果有付费图书馆,请推荐我