问题描述
我有一个奇怪的问题,调整图像大小,无法弄清楚我做错了什么。我已经阅读了很多帖子,基本上和我的代码基本相同:
I have a weird problem with resizing images and can't figure out what I'am doing wrong. I've read lots of posts wwhich basically have the same code as I:
(我使用的是java库Scalr)
(I use the java library 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) {}
有人可以告诉我我做错了吗?
Can someone tell me what I am doing wrong?
推荐答案
我认为你的代码没有任何问题。
I do not see anything wrong with your code.
我将它拉入Eclipse的快速测试项目,目标是Java SE 7并使用:
I pulled it into a quick test project in Eclipse targeting Java SE 7 and using imgscalr 4.2 on Windows 7 Pro 64-bit:
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_s.png
:
..., it produced the following myimage_s.png
:
也许有一个环境问题困扰着你的代码,但是想到的可能性会带来明显的错误。
Maybe there is an environmental issue that's hamstringing your code, but possibilities that come to mind would come with a clear error.
这篇关于图像调整Java大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!