Java:读取图像并显示为ImageIcon

前端之家收集整理的这篇文章主要介绍了Java:读取图像并显示为ImageIcon前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个应用程序,它以 ImageIcons(在JLabel中)读取和显示图像,应用程序需要能够支持jpeg和位图.

对于jpeg,我发现将文件名直接传递给ImageIcon构造函数工作正常(即使显示两个大型jpeg),但是如果我使用ImageIO.read获取图像然后将图像传递给ImageIcon构造函数,我会得到一个OutOfMemoryError( Java Heap Space)读取第二个图像时(使用与之前相同的图像).

对于位图,如果我尝试通过将文件名传递给ImageIcon来读取,则不显示任何内容,但是通过使用ImageIO.read读取图像然后在ImageIcon构造函数中使用此图像可以正常工作.

我从阅读其他论坛帖子中了解到,两种方法对于不同格式不起作用的原因归结为java与位图的兼容性问题,但是有一种方法可以绕过我的问题以便我可以使用相同的方法没有OutOfMemoryError的位图和jpegs?

(如果可能,我想避免增加堆大小!)

OutOfMemoryError由此行触发:

  1. img = getFileContentsAsImage(file);

方法定义是:

  1. public static BufferedImage getFileContentsAsImage(File file) throws FileNotFoundException {
  2. BufferedImage img = null;
  3. try {
  4. ImageIO.setUseCache(false);
  5. img = ImageIO.read(file);
  6. img.flush();
  7. } catch (IOException ex) {
  8. //log error
  9. }
  10. return img;
  11. }

堆栈跟踪是:

  1. Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
  2. at java.awt.image.DataBufferByte.<init>(DataBufferByte.java:58)
  3. at java.awt.image.ComponentSampleModel.createDataBuffer(ComponentSampleModel.java:397)
  4. at java.awt.image.Raster.createWritableRaster(Raster.java:938)
  5. at javax.imageio.ImageTypeSpecifier.createBufferedImage(ImageTypeSpecifier.java:1056)
  6. at javax.imageio.ImageReader.getDestination(ImageReader.java:2879)
  7. at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:925)
  8. at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:897)
  9. at javax.imageio.ImageIO.read(ImageIO.java:1422)
  10. at javax.imageio.ImageIO.read(ImageIO.java:1282)
  11. at framework.FileUtils.getFileContentsAsImage(FileUtils.java:33)

解决方法

内存不足因为ImageIO.read()返回一个非压缩的BufferedImage,它非常大并且保留在堆中,因为它被ImageIcon引用.但是,Toolkit.createImage返回的图像保持其压缩格式(使用私有的ByteArrayImageSource类.)

您无法使用Toolkit.createImage读取BMP(即使您可能仍然在内存中保持未压缩状态,并且您可能会再次耗尽堆空间)但您可以做的是读取未压缩的图像并将其保存在字节数组中以压缩形式,例如

  1. public static ImageIcon getPNGIconFromFile(File file) throws IOException {
  2. BufferedImage bitmap = ImageIO.read(file);
  3. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  4. ImageIO.write(bitmap,"PNG",bytes);
  5. return new ImageIcon(bytes.toByteArray());
  6. }

这样,无压缩位图必须保存在内存中的唯一时间是加载或渲染时.

猜你在找的Java相关文章