图像调整Java大小

我有一个奇怪的问题,调整图像大小,无法弄清楚我做错了什么。 我看过很多post,基本上和我的代码基本相同:

(我使用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的快速测试项目,并在Windows 7 Pro 64位上使用imgscalr 4.2 :

 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

也许有一个环境问题困扰着你的代码,但是脑海中出现的可能性会带来明显的错误。