Java: read the image and display it as imageicon

I'm writing an application that reads and displays images with imageicons (in jlabel). The application needs to be able to support JPEG and bitmap

For JPEG, I find that passing the file name directly to the imageicon constructor works well (even if two large JPEG are displayed), but if I use imageio Read gets the image and passes it to the imageicon constructor. I will get an outofmemoryerror (Java heap space) when reading the second image (using the same image as before)

For bitmaps, if I try to read by passing the file name to imageicon, nothing is displayed, but by using imageio Read reads the image and then uses it in the imageicon constructor to work properly

I learned from reading other forum posts that the reason why the two methods do not work for different formats is due to the compatibility between Java and bitmap, but there is a way to bypass my problem so that I can use the same method without outofmemoryerror bitmap and JPEG?

(if possible, I want to avoid increasing the heap size!)

Outofmemoryerror is triggered by this line:

img = getFileContentsAsImage(file);

The method definition is:

public static BufferedImage getFileContentsAsImage(File file) throws FileNotFoundException { 
  BufferedImage img = null; 
  try { 
    ImageIO.setUseCache(false); 
    img = ImageIO.read(file); 
    img.flush(); 
  } catch (IOException ex) { 
    //log error 
  } 
return img; 
}

Stack traces are:

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

Solution

Out of memory because imageio Read () returns an uncompressed bufferedimage, which is very large and remains in the heap because it is referenced by imageicon However, toolkit The image returned by createimage maintains its compressed format (using the private bytearrayimagesource class.)

You cannot use toolkit Createimage reads the BMP (even if you may still remain uncompressed in memory and you may run out of heap space again), but what you can do is read the uncompressed image and save it in a byte array in compressed form, such as

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

In this way, the only time an uncompressed bitmap must be saved in memory is when it is loaded or rendered

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>