JAVAFX imageview 允许用户添加标签

JAVAFX imageview allow user to add tag

我正在使用 javafx 制作图片库,我希望有这样的功能,当用户在图像视图上单击时,允许用户向图像视图添加一些标签(用于人脸),它应该抓住位置鼠标指向并提示一个对话框,让用户输入面部名称并将其保存到数据库中。

这可以通过使用 javafx 实现吗?

如果是,我应该怎么做?

感谢您的帮助。

下面是我的应用程序的示例输出:

当然有可能。我为单个 Image 创建了这个简单的示例,它向场景添加了一个按钮,该按钮在单击图像的位置添加了一个圆圈。这应该演示该方法。

@Override
public void start(Stage primaryStage) {
    ImageView imageView = new ImageView(...);
    imageView.setPreserveRatio(true);
    imageView.setFitHeight(200);
    StackPane pane = new StackPane();
    StackPane.setAlignment(imageView, Pos.TOP_LEFT);
    pane.getChildren().add(imageView);
    VBox box = new VBox(pane);

    pane.setOnMouseClicked(evt -> {
        if (evt.getButton() == MouseButton.PRIMARY) {
            // create node(s) that uses the click data
            Button button = new Button("Show point");
            button.setOnAction(e -> {
                // do something with the data on user interaction
                box.getChildren().remove(button);
                Circle circle = new Circle(evt.getX(), evt.getY(), 5, Color.RED);
                Pane circlePane = new Pane(circle);
                StackPane.setAlignment(circlePane, Pos.TOP_LEFT);
                pane.getChildren().add(circlePane);
            });
            box.getChildren().add(button);
        }
    });

    Scene scene = new Scene(box, 200, 300);

    primaryStage.setScene(scene);
    primaryStage.show();
}

对于您的申请,您将:

  • 创建一个不同的事件处理程序来创建 and/or 根据您的需要初始化 UI 或显示一个对话框。您还可以直接将侦听器注册到 ImageView(除非您需要在其上添加一些元素,就像我一样)
  • 使用evt.getX() * imageView.getImage().getWidth() / imageView.getLayoutBounds().getWidth()(绝对位置)或evt.getX() / imageView.getLayoutBounds().getWidth()(相对位置)计算图像坐标中的真实点击位置;相应地进行 y 位置。直接在点击处理程序中执行此计算,而不是在用户提交输入的信息时执行,因为您可能希望用户能够调整图像大小。此外,如果您使用 viewport 属性,则需要相应地修改计算。
  • 创建一个不同的线程,在用户提交 his/her 编辑时将信息发送到数据库。当然你还需要在某处存储ImageView中当前显示的图像的一些信息。