在Java中没有内存问题的缩略图加载大图像? [英] Loading large images as thumbnails without memory issues in Java?

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

问题描述

我试图让用户从他们的硬盘加载图像,并在GUI中以视觉形式呈现这些缩略图列表(带有添加到JList的图标的JPanel)。我目前正在使用ImageIO.read()来获取BufferedImage并为每个图像使用getScaledInstance(听说你不应该使用它)。

I'm trying to let the user load images from their harddrive, and present these visually in the GUI as a list of thumbnails (JPanels with icons added to a JList). I'm currently using ImageIO.read() to get a BufferedImage and using getScaledInstance for each image (heard that you weren't supposed use that though).

它适用于小图片,但加载了超过四张照片(5000x3000或其他一些),我得到java.lang.OutOfMemoryError:Java堆空间。不保存对全尺寸BufferedImage的引用,所以我认为垃圾收集器会处理它并且只保留缩放图像(不应该占用太多内存),但它看起来不像。我也试着getRuntime()。gc()和System.gc(),但没有效果。

It works reasonably well with small images, but loading more than four photographs (5000x3000 or some such) and I get "java.lang.OutOfMemoryError: Java heap space". The reference to the full size BufferedImage isn't saved, so I figured that the garbage collector would dispose of it and only keep the scaled image (which oughtn't take much memory), but it doesn't seem like it. I tread getRuntime().gc() and System.gc() as well, to no effect.

从文件中加载缩放图像的任何好方法,不会遇到记忆错误?显然很多软件都设法做到这一点,但也许不是用Java做的。外部库是可以的。

Any good way of loading scaled images from file, without running into memory errors? Obviously a lot of softwares manage to do this very thing, maybe not in Java though. External libraries are okay.

当前代码:

BufferedImage unscaledImage = ImageIO.read(imageFile);

int unscaledHeight = unscaledImage.getHeight();
int unscaledWidth = unscaledImage.getWidth();

int imageRatio = unscaledHeight/unscaledWidth;

if (imageRatio >= 1) {
    return new ImageIcon(unscaledImage.getScaledInstance(width,-1,Image.SCALE_FAST));
} else {
    return new ImageIcon(unscaledImage.getScaledInstance(-1,height,Image.SCALE_FAST));
}


推荐答案

问题在于使用BufferedImage本身。当文件被读入内存时,你将没有堆空间。根据具体情况,您可以使用图像阅读器,也可以增加堆的大小。

The problem is in the use of BufferedImage itself. By the time the file is read into memory you'll be out of heap space. Depending on what this is for you can either use an image reader or you can increase the size of the heap.

我建议您使用图像阅读器。例如,要获得图像阅读器,您的代码将是这样的:

I'd recommend that you use an image reader. For example to get an image reader you code would be something like this:

  // Create an image input stream on the image
    ImageInputStream iis = ImageIO.createImageInputStream(o);

    // Find all image readers that recognize the image format
    Iterator iter = ImageIO.getImageReaders(iis);
    if (!iter.hasNext()) {
        // No readers found
        return null;
    }

    // Use the first reader
    ImageReader reader = (ImageReader)iter.next();

来自: http://www.exampledepot.com/egs/javax.imageio/DiscType.html

一个你有ImageReader你可以通过调用 reader.getAspectRatio()

One you have the ImageReader you can get the aspect ration by calling reader.getAspectRatio()

我可以获得方面比例我不是确定你如何从ImageReader转到缩略图。

I'm not sure how you'd go from an ImageReader to a thumbnail though.

这篇关于在Java中没有内存问题的缩略图加载大图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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