java thumbnails 内存,Java图像缩放,而无需将整个图像加载到内存中

该博客介绍了如何使用Java处理大型JPEG图片,避免内存溢出问题,通过ImageIO库实现从大图到缩略图的转换,关键在于利用ImageInputStream和setSourceSubsampling方法减少内存消耗。
摘要由CSDN通过智能技术生成

We have some very large jpgs which are used when print on A0 printers.

The problem is that we need to convert this large image down to a thumbnail for use in a few java UIs.

Is there any way to convert the image (with Java) without loading the whole image into memory? Currently we get out of memory exceptions when we try to load the images.

Is there anything in the standard code or is my best bet to use jmagick? A pure java implementation would be best for our deployment.

Thanks

解决方案

I managed to get it working and the full code is as follows.

The call to reader.setInput(iis, true, true); is the magic which I was missing from the previous post.

FileInputStream fin = new FileInputStream(source);

ImageInputStream iis = ImageIO.createImageInputStream(fin);

Iterator iter = ImageIO.getImageReaders(iis);

if (!iter.hasNext()) {

return;

}

ImageReader reader = (ImageReader) iter.next();

ImageReadParam params = reader.getDefaultReadParam();

reader.setInput(iis, true, true);

params.setSourceSubsampling(width, height, 0, 0);

BufferedImage img = reader.read(0, params);

ImageIO.write(img, "JPG", destination);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值