如何调整JavaFX图像的大小? [英] How can I resize a JavaFX image?

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

问题描述

我有 javafx.scene.image.Image ,我想调整大小,例如按给定因子缩放它。怎么做(没有转换为 BufferedImage ),有关质量和性能的选项有哪些(例如插值类型)?

I have a javafx.scene.image.Image, and I would like to resize it, e.g. scaling it by a given factor. How to do that (without converting to BufferedImage), and what are the options regarding quality and performance (such as interpolation types)?

有几个问题看起来很相似,但我找不到任何提出同样问题的人。关键点是输入是一个 Image 对象(和缩放系数),所需的输出是另一个 Image 对象。

There are several questions that look similar, but I couldn't find anybody who asked the same thing. The key point is that the input is an Image object (and scaling factor) and the desired output is another Image object.

推荐答案

最简单的方法是:

public Image scale(Image source, int targetWidth, int targetHeight, boolean preserveRatio) {
    ImageView imageView = new ImageView(source);
    imageView.setPreserveRatio(preserveRatio);
    imageView.setFitWidth(targetWidth);
    imageView.setFitHeight(targetHeight);
    return imageView.snapshot(null, null);
}

对缩放算法的速度/平滑度进行最小程度的控制可以通过调用 imageView.setSmooth(...); 使用更快的算法更好地使用值 true 平滑和 false 使用更快的算法,平滑度更低。

A minimal amount of control over the speed/smoothness of the scaling algorithm is made available via a call to imageView.setSmooth(...); with the value true using a slower algorithm with better smoothing and false using a faster algorithm with less smoothing.

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

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