Java图像缩放,而无需将整个图像加载到内存中 [英] Java image scaling without loading the whole image into memory

查看:89
本文介绍了Java图像缩放,而无需将整个图像加载到内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在A0打印机上打印时,我们会使用一些非常大的jpg.

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

问题是我们需要将大图像转换为缩略图,以便在几个Java UI中使用.

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

是否有任何方法可以在不将整个图像加载到内存的情况下转换图像(使用Java)?当前,当我们尝试加载图像时会遇到内存不足的异常.

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.

标准代码中是否包含任何内容,或者最好的选择是使用jmagick?纯Java实现最适合我们的部署.

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.

谢谢

推荐答案

我设法使其正常运行,完整代码如下.

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

对reader.setInput(iis,true,true)的调用;是上一篇文章中我所缺少的魔术.

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);

这篇关于Java图像缩放,而无需将整个图像加载到内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆