gpt4 book ai didi

java - 在不影响纵横比的情况下调整图像大小

转载 作者:行者123 更新时间:2023-11-30 04:23:00 25 4
gpt4 key购买 nike

我使用以下代码来调整图像大小,但问题是它们不保持原始宽高比,有什么方法可以在不影响宽高比的情况下调整图像大小。

BufferedImage img1 = new BufferedImage(800, 600,
BufferedImage.TYPE_INT_RGB);
img1.createGraphics()
.drawImage(
ImageIO.read(
new File("/home/rvkydmpo/webapps/ROOT/images/profilePicture/"
+ fileName)).getScaledInstance(800,
600, Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(img1, "jpg", new File("/home/rvkydmpo/webapps/ROOT/images/profilePicture/" +fileName));

最佳答案

您可能想阅读the docs of getScaledInstance() :

Image java.awt.Image.getScaledInstance(int width, int height, int hints)

Creates a scaled version of this image. A new Image object is returned which will render the image at the specified width and height by default. The new Image object may be loaded asynchronously even if the original source image has already been loaded completely.

If either width or height is a negative number then a value is substituted to maintain the aspect ratio of the original image dimensions. If both width and height are negative, then the original image dimensions are used.

(强调我的)

关于java - 在不影响纵横比的情况下调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16546415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com