2014-07-11 289 views
3

我有一个奇怪的问题,调整图像大小,无法弄清楚我做错了什么。我读过很多职位wwhich基本上有相同的代码,因为我的:图像调整大小Java

(我用的是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) {} 

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

+0

有什么迹象表明有什么不对?如果你的代码抛出一个异常'catch(Exception e){}'将会让你在黑暗中发生什么。您是否使用调试器完成了代码? – J0e3gan

+0

http://www.mkyong.com/java/how-to-resize-an-image-in-java/ –

+2

我刚刚离开了不必要的代码,我在我的代码中有错误处理等。我没有得到任何例外或其他任何东西。 “调整大小”图像的大小与原始大小相同 – Markus

回答

3

我没有看到你的代码有什么问题。

我把它开进在Eclipse中快速测试项目针对Java SE 7中,并使用imgscalr 4.2在Windows 7专业版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

也许有一个环境问题妨碍了你的代码,但想到的可能性会带来明显的错误。

+1

是的,你是对的。我还在一个额外的应用程序中测试了它。我认为我的问题不是Java相关的,而是别的。感谢您考虑测试我的问题,并将我带入正确的方向! – Markus