简体   繁体   English

javafx-调整图像大小以保留纵横比后,访问图像的高度和宽度值

[英]javafx - access height and width values of an image after it is resized to preserve aspect ratio

Hello all and thanks for reading. 大家好,谢谢阅读。 I am currently building an image annotation tool. 我目前正在构建图像注释工具。 Images are loaded into and bound to the parent container(borderpane). 图像被加载并绑定到父容器(边框)。 Aspect ratio is also preserved. 长宽比也被保留。 The code snippet attached illustrates these points. 随附的代码段说明了这些要点。

     imageView.setPreserveRatio(true);
     imageView.setSmooth(true);
     imageView.fitHeightProperty().bind(heightProperty());
     imageView.fitWidthProperty().bind(widthProperty());

This works fine: images fit the window and are resized when the window is resized. 效果很好:图像适合窗口并在调整窗口大小时进行调整。

On top of this I have placed a canvas on which the user can draw rectangles. 在此之上,我放置了一个画布,用户可以在其上绘制矩形。 After they have drawn a rectangle, a pop up is launched in which they can write an annotation. 在绘制矩形之后,将弹出一个窗口,在其中可以编写注释。 After it is saved the user can hover over the drawn rectangle and the annotation will appear. 保存后,用户可以将鼠标悬停在绘制的矩形上,然后将显示注释。

I want to know if it's possible to access the height and width values of an image after it has been resized to fit the window and while it is being resized when the window is being resized. 我想知道是否有可能在调整图像大小以适合窗口之后以及在调整窗口大小的同时调整图像的大小时访问图像的高度和宽度值。

My reason for needing this is that I want the canvas to mirror the dimensions of the image so that the annotations are within its bounds. 我需要这样做的原因是,我希望画布能够镜像图像的尺寸,以便注释位于其范围之内。 Currently when the window is resized the annotations do not remain 'fixed' to their area as the canvas is set to the dimensions of the window and not the image. 当前,当调整窗口大小时,由于画布设置为窗口的尺寸而非图像的尺寸,因此注释不会保持“固定”到其区域。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I've hit a bit of a wall. 我碰壁了。 And am not even sure if what I'm asking is possible in JavaFX. 而且甚至不确定我要问的内容是否可以在JavaFX中使用。 Thanks again for reading. 再次感谢您的阅读。

Evening all. 晚上都。 Here's how I managed to solve my problem for anyone who might need help with a similar issue in the future. 这是我设法为将来可能需要解决类似问题的任何人解决问题的方法。

First, create a 'Resizable Canvas' using this code: 首先,使用以下代码创建“可调整大小的画布”:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;  

class ResizableCanvas extends Canvas {

        public ResizableCanvas() {

            // Redraw canvas when size changes.

            widthProperty().addListener(evt -> draw());

            heightProperty().addListener(evt -> draw());

        }

        private void draw() {

            double width = getWidth();

            double height = getHeight();

            GraphicsContext gc = getGraphicsContext2D();

            gc.clearRect(0, 0, width, height);

            gc.setStroke(Color.RED);

            gc.strokeLine(0, 0, width, height);

            gc.strokeLine(0, height, width, 0);
        }
    @Override

        public boolean isResizable() {

            return true;
        }


    @Override

        public double prefWidth(double height) {

            return getWidth();

        }

        @Override

        public double prefHeight(double width) {

            return getHeight();

        }

    }

This done, set up your Stage as follows: 完成后,按以下步骤设置舞台:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;


public class ImageViewTest extends Application   {

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

@Override
public void start(Stage stage) throws Exception {

    ResizableCanvas rc = new ResizableCanvas();
    rc.setOnMouseEntered(e -> {
        System.out.println("entered ");
    });
    Image image = new Image("rushmore.png");

    ImageView imageView = new ImageView();
    imageView.setImage(image);
    imageView.setSmooth(true);
    imageView.setCache(true);


    AnchorPane rootAnchorPane = new AnchorPane();
    AnchorPane resizableCanvasAnchorPane = new AnchorPane();


   rc.widthProperty().bind(resizableCanvasAnchorPane.widthProperty());
 rc.heightProperty().bind(resizableCanvasAnchorPane.heightProperty());


    imageView.fitWidthProperty().bind(stage.widthProperty());
    imageView.fitHeightProperty().bind(stage.heightProperty());
    imageView.setPreserveRatio(true);

    //here's where the 'magic happens'
    resizableCanvasAnchorPane.getChildren().addAll(imageView, rc);
    rootAnchorPane.getChildren().addAll(resizableCanvasAnchorPane,rc);

    Scene scene = new Scene(rootAnchorPane);
    scene.setFill(Color.BLACK);
    stage.setTitle("ImageView Test");
    stage.setWidth(415);
    stage.setHeight(200);
    stage.setScene(scene);
    stage.show(); 
   }

}

Big thanks also goes to Dirk Lemmermann and this tutorial: 非常感谢Dirk Lemmermann和本教程:

https://www.javacodegeeks.com/2014/04/javafx-tip-1-resizable-canvas.html https://www.javacodegeeks.com/2014/04/javafx-tip-1-resizable-canvas.html

Hopefully you can see that the canvas resizes with the image. 希望您能看到画布随图像调整大小。 There are red guidelines over the image and a mouse event to illustrate this. 图像上方有红色准则,并且有一个鼠标事件来说明这一点。 Check the console of your IDE and you can see an event being fired when the mouse is within the bounds of the canvas. 检查IDE的控制台,当鼠标在画布的边界内时,您会看到一个事件被触发。

Any questions, let me know. 有任何问题,请告诉我。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 保持 Gridpane 相同的高度和宽度(纵横比) - Maintain Gridpane the same height and width (Aspect ratio) 如何在不影响Java中的图像实际纵横比的情况下,为图像计算新的高度和宽度以适合预定义的高度和宽度? - How to calculate new height and width for an image to fit with in predefined height and width, without affecting the image real aspect ratio in java? MigLayout:如何与宽度成比例地调整高度(保持长宽比)? - MigLayout: how to resize the height proportionally to the width (keeping the aspect ratio)? 如何获得新的调整尺寸图像的高度和宽度? - How to obtain new height and width of resized image? Android:如何在保持纵横比的同时将图像拉伸到屏幕宽度? - Android: How to stretch an image to the screen width while maintaining aspect ratio? 图像缩放的最佳解决方案,我需要保持纵横比并保持良好的质量 - Optimal solution for Image scaling where i need to preserve aspect ratio and retain good quality javaFx的图像高度和宽度倒退了吗? - javaFx Image Height and Width got backwards? 调用sizeToScene之后的JavaFX舞台宽度/高度 - JavaFX Stage width/height after sizeToScene call JavaFX:如何使用设置的宽高比调整矩形的大小 - JavaFX: How to resize a rectangle with set aspect ratio JavaFX如何保持一定的纵横比 - JavaFX how to keep a certain aspect ratio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM