【问题标题】:ImageView not resizing imageImageView 不调整图像大小
【发布时间】:2013-02-02 05:11:22
【问题描述】:

我想使用 ImageView 来调整图像的大小,但是图像没有被调整大小:

ImageView imageView = new ImageView(image); 
imageView.setPreserveRatio(true);
imageView.setFitHeight(40);
System.out.println("imageview image width = " + imageView.getImage().getWidth());
System.out.println("imageview image height = " + imageView.getImage().getHeight());

输出是

imageview image width = 674.0
imageview image height = 888.0

但是,宽度应该是 40。我的 ImageView 没有附加到任何场景,我也不想附加它,它只能用于调整图像大小。 是否有任何方法可以强制 ImageView 调整其图像大小,即使 ImageView 未附加到任何场景? 我使用 ImageView 调整大小的原因是,我想在RAM,无需从磁盘再次读取,详情请见this question

感谢任何提示!

【问题讨论】:

  • 你在改变ImageView的大小,但是读取Image的大小,试试imageView.getFitHeight()

标签: imageview javafx-2


【解决方案1】:

使用 ImageView 来调整大小似乎很 hacky。

更好的方法是将您的 Image 转换为 BufferedImage 并以旧方式调整大小。 (JavaFx(还没有)提供调整内存图像大小的内部方法)

int width = 500; // desired size
int height = 400;
Image original = ...; // fx image

BufferedImage img = new BufferedImage(
        (int)original.getWidth(),
        (int)original.getHeight(),
        BufferedImage.TYPE_INT_ARGB);

SwingFXUtils.fromFXImage(original, img);
BufferedImage rescaled = Scalr.rescaleImage(img, width, heigth);  // the actual rescale

// convert back to FX image
WritableImage rescaledFX = new WritableImage(width, heigth);
SwingFXUtils.toFXImage(rescaled, rescaledFX);

Scalr 是一个很好的库,用于在本机 java 中调整图像大小。显然,您可以使用其他/更简单的重新缩放方法,但图像质量不会那么好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-24
    • 1970-01-01
    • 2018-02-17
    相关资源
    最近更新 更多