在 Java 中调整图像大小 [英] Resizing image in Java
问题描述
我有一个 PNG 图片,我想调整它的大小.我怎样才能做到这一点?虽然我已经经历了这个我看不懂片段.
I have a PNG image and I want to resize it. How can I do that? Though I have gone through this I can't understand the snippet.
推荐答案
如果你有一个 java.awt.Image
,重新调整它不需要任何额外的库.就这样做:
If you have an java.awt.Image
, rezising it doesn't require any additional libraries. Just do:
Image newImage = yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
显然,替换 newWidth
和 newHeight
为指定图像的尺寸.
请注意最后一个参数:它告诉运行时您要用于调整大小的算法.
Ovbiously, replace newWidth
and newHeight
with the dimensions of the specified image.
Notice the last parameter: it tells to the runtime the algorithm you want to use for resizing.
有些算法可以产生非常精确的结果,但是这些算法需要很长时间才能完成.
您可以使用以下任何一种算法:
There are algorithms that produce a very precise result, however these take a large time to complete.
You can use any of the following algorithms:
Image.SCALE_DEFAULT代码>
:使用默认的图像缩放算法.Image.SCALE_FAST代码>
:选择一种图像缩放算法,该算法优先考虑缩放速度而不是缩放图像的平滑度.Image.SCALE_SMOOTH代码>
:选择图像平滑度优先于缩放速度的图像缩放算法.Image.SCALE_AREA_AVERAGING代码>
:使用区域平均图像缩放算法.Image.SCALE_REPLICATE
:使用ReplicateScaleFilter
类中包含的图像缩放算法.
Image.SCALE_DEFAULT
: Use the default image-scaling algorithm.Image.SCALE_FAST
: Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image.Image.SCALE_SMOOTH
: Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.Image.SCALE_AREA_AVERAGING
: Use the Area Averaging image scaling algorithm.Image.SCALE_REPLICATE
: Use the image scaling algorithm embodied in theReplicateScaleFilter
class.
请参阅Javadoc 了解更多信息.
See the Javadoc for more info.
这篇关于在 Java 中调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!