gpt4 book ai didi

java - 图像大小调整 Java

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:47:32 24 4
gpt4 key购买 nike

我在调整图像大小时遇到​​了一个奇怪的问题,无法弄清楚我做错了什么。我已经阅读了很多帖子,它们基本上具有与我相同的代码:

(我使用java库Scalr)

File image = new File("myimage.png");
File smallImage = new File("myimage_s");
try {
BufferedImage bufimage = ImageIO.read(image);

BufferedImage bISmallImage = Scalr.resize(bufimage, 30); // after this line my dimensions in bISmallImage are correct!
ImageIO.write(bISmallImage, "png", smallImage); // but my smallImage has the same dimension as the original foto
} catch (Exception e) {}

谁能告诉我我做错了什么?

最佳答案

我没有发现您的代码有任何问题。

我将其放入 Eclipse 中针对 Java SE 7 并使用 imgscalr 4.2 的快速测试项目中在 Windows 7 Pro 64 位上:

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;

public class ScalrTest {

public static void main(String[] args) {
File image = new File("myimage.png");
File smallImage = new File("myimage_s.png"); // FORNOW: added the file extension just to check the result a bit more easily
// FORNOW: added print statements just to be doubly sure where we're reading from and writing to
System.out.println(image.getAbsolutePath());
System.out.println(smallImage.getAbsolutePath());
try {
BufferedImage bufimage = ImageIO.read(image);

BufferedImage bISmallImage = Scalr.resize(bufimage, 30); // after this line my dimensions in bISmallImage are correct!
ImageIO.write(bISmallImage, "png", smallImage); // but my smallImage has the same dimension as the original foto
} catch (Exception e) {
System.out.println(e.getMessage()); // FORNOW: added just to be sure
}
}

}

使用以下 myimage.png...

myimage.png

...,它产生了以下 myimage_s.png:

myimage_s.png

也许存在阻碍您的代码的环境问题,但想到的可能性会伴随明显的错误。

关于java - 图像大小调整 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24689255/

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