java 图像分小块,图像调整Java大小

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:

(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.

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

}

}

}

With the following myimage.png...

ueTBj.png

..., it produced the following myimage_s.png:

fMwfT.png

Maybe there is an environmental issue that's hamstringing your code, but possibilities that come to mind would come with a clear error.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值