ImageView没有调整图像大小 [英] ImageView not resizing image

查看:130
本文介绍了ImageView没有调整图像大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use an ImageView to resize an image, however the image is not being resized:

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进行大小调整的原因是,我要调整Image中的大小RAM,无需再次从磁盘读取,请参阅这个问题了解更多详情。

However, the width should be 40. My ImageView is not attached to any scene and I also don't want to attach it, it shall only be used for image resizing. Is there any way to force the ImageView to resize its image, even though the ImageView is not attached to any scene? The reason I am using an ImageView for resizing is, that I want to resize an Image in RAM, without reading it again from the disk, please see this question for more details.

感谢任何提示!

推荐答案

使用ImageView进行大小调整似乎非常hacky。

Using an ImageView for resizing seems to be very hacky.

更好的方法是将Image转换为BufferedImage并调整旧方法的大小。 (JavaFx(尚未提供)调整内存映像大小的内部方法)

A better approach is to convert your Image into a BufferedImage and do the resizing the old way. (JavaFx does not (yet) provide an internal way to resize memory images)

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中调整图像大小。显然,您可以使用其他/更简单的重新缩放方法,但图像质量不会那么好。

Where as Scalr is a nice library for resizing images in native java. Obviously, you can use other/simpler methods of rescaling, but the image quality won't be that nice.

这篇关于ImageView没有调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆