User Tools

Site Tools


lire:imageread

Reading and Decoding images in Java

Java is unfortunately limited in it's ability to handle images. But there are two basic workarounds.

  1. You can convert all images to a format that Java can handle. Just use ImageMagick or some other great tool to batch process yout images and convert them all to RGB color JPEGs. This is a non Java approach and definitely the faster one.
  2. You can circumvent the ImageIO.read(..) metzhod by using ImageJ. In ImageJ you've got the ImagePlus class, which supprts loading and decoding of various formats and is much more error resilient than the pure Java SDK method. Speed, however, is not increased by this approach. It's more the other way round.

Using ImageJ to load and decode images

public class Utils {
    // Loading an image with ImageJ
    public static BufferedImage openImage(String path) {
        ImagePlus imgPlus = new ImagePlus(path);
        // converting the image to RGB
        ImageConverter imageConverter = new ImageConverter(imgPlus);
        imageConverter.convertToRGB();
        // returning the BufferedImage instance
        return imgPlus.getBufferedImage();
    }
}
lire/imageread.txt · Last modified: 2021/03/08 03:43 by mlux