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

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

问题描述

我们有一些非常大的 jpg,用于在 A0 打印机上打印.

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天全站免登陆