Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get width/height of displayed image in javafx imageView?

Tags:

image

javafx-2

I need to get width/height of displayed image in imegView and compare it to orginal image size which is in imageView.getImage().getWidth()/getHeight() and listen changes if user resize it form application GUI.

I get this size from imageView.fitWidthProperty() and similar for height, but I get size of imageView not image within it: enter image description here

I get size of blue which is imageView but I need size of displayed image (green). How can I get it as property to listen when user resize window app (green also change size but it seems to have max and min size so it stop resize with imageView when it is max or min).

I use this property to compute % of size of orginal image in bottom right below blue rectangle.

Is it possible?

E: Below is fxml of this tab:

<BorderPane fx:id="root" fx:controller="..."
        xmlns:fx="..." xmlns="..." stylesheets="@...css">
<center>
    <StackPane>
        <ImageView fx:id="..."/>
        <fx:include source="...fxml" fx:id="..."/>
    </StackPane>
</center>
<bottom>
    <VBox fx:id="..." minHeight="110" styleClass="small-padding" spacing="5">
        <HBox alignment="CENTER_RIGHT" spacing="5">
            <fx:include source="...fxml" resources="..."
                        fx:id="..."/>
        </HBox>
        <TextArea fx:id="..." promptText="%..." styleClass="-fx-description-text-area"/>
    </VBox>
</bottom>

like image 846
xav9211 Avatar asked Sep 09 '16 09:09

xav9211


2 Answers

~>1)

The fact that the Image is not resizing to cover all the ImageView has to do with preserveRatio method.If you want to be covered setPreserveRatio(false); with a combination of setSmooth( true );

~>2)

The green border is the size of the original image.

~>3)

Before adding the Image to the ImageView you can do:

Image image = new Image("FILE:...");
image.getWidth( );
image.getHeight( );

And that is the original size of the image.

As for the size of the image when it is displayed inside the ImageView:

imageView.getFitWidth();
imageView.getFitHeight();

For the size of ImageView inside the Scene (the blue border in your question):

imageView.getWidth();
imageView.getHeight();

ImageView class has properties for the above.

like image 115
GOXR3PLUS Avatar answered Nov 15 '22 07:11

GOXR3PLUS


When the ImageView is set to preserve the aspect ratio, one has to calculate the real dimensions by hand. At least I've not found another way.

double aspectRatio = image.getWidth() / image.getHeight();
double realWidth = Math.min(imageView.getFitWidth(), imageView.getFitHeight() * aspectRatio);
double realHeight = Math.min(imageView.getFitHeight(), imageView.getFitWidth() / aspectRatio);

To explain this a bit: getFitHeight/Width is your blue rectangle. When preserving the aspect ratio of the source image, at least one side of the blue rectangle must have the same length as the corresponding side of the green rectangle. Also, the green rectangle will always be inside of the blue one, hence the call to Math.min().

like image 20
johnniegf Avatar answered Nov 15 '22 07:11

johnniegf