Read images in plugins

Hi guys

I bump into a weird problem and actually I am not surely if it is pure java problem or it is the issue of ImageJ.

For a specific purpose, now I have an ImagePlus instance and I have to get the same file again by myself to acquire the ImageReader instance (Java.awt.ImageReader). The process is:

final FileInfo fileInfo = imagePlus.getFileInfo();
final String dir = imagePlus.getOriginalFileInfo().directory;
final String fileName = imagePlus.getOriginalFileInfo().fileName;

File file = new File(dir+fileName);

if (!file.exists())
      IJ.error("File not exist");
      return null;

FileReader fileReader = new FileReader(file);
ImageReader imgReader = fileReader.**getImageReader**();   // Target

The getImageReader method is as follows.

private ImageReader **getImageReader**(File file) throws FileNotFoundException, IOException
    Iterator iter = null;
    ImageReader reader = null;


      // Get an Image Input Stream to the File
      Object input = new FileImageInputStream(file);
      Object inputStream = input;

      // Get an Image Reader for the source data from the source
      // reader bytes
      iter = ImageIO.getImageReaders(input);

      // No Image Reader is found
      if (!iter.hasNext())

        // Get an Image Reader from the file
        input = file;
        iter = ImageIO.getImageReaders(input);

        // No Image Reader is found
        if (!iter.hasNext())
          input = inputStream;

          // Get an Image Reader from the file name suffix
          String temp = file.toString();
          String[] strings = temp.split("\\.");
          if (strings.length > 1)
            iter = ImageIO.getImageReadersBySuffix(strings[strings.length - 1]);

          // No Image Reader found
          if (!iter.hasNext())
            // return (null);
            String msg = "Cannot find an Image Reader for the source. ";
            throw new IOException(msg);

      // Set the Input Stream
      reader = (ImageReader);
      ImageReaderSpi spi = reader.getOriginatingProvider();

      if (spi.canDecodeInput(input))
        _IJ.log("Can be decoded");_
        return reader;
    } catch (Exception e)

    _return null;_

Thanks a lot if you reach here.

The problem is:

Case 1: I run the getImageReader() by unit test and the log message “Can be decoded” is displayed.

Case 2: I package the whole project into .jar and execute by ImageJ UI, and it runs to the end of the method and return null.

I have check that the input file paths are the same which are absolute paths started by “/home/…” in Ubuntu. Then, why results in Case1 and Case2 varies ???

Thanks for your patience and glad to hear from you, thanks.

Read the image into File instance seems fine, while it results in null after ImageReader.

Is it possible that the conflicts with the one in ij.ImageReader ??

Anyone has similar experience about the library conflict ?

It’s not clear to me what you want to achieve. Why do you need to have an ImageReader and what do you plan to do with it?

In the javadoc, I can’t find java.awt.ImageReader, but only and javax.imageio.ImageReader. Which one do you need? You should import only the classes that you actually need to avoid any conflict.

Also, I can’t see any method getImageReader() in the class.

1 Like

Thanks for your attention,

What I want is the javax.imageio.ImageReader, and FileReader is written by one of my colleague.

I wonder why the result is different between local running and packaging into jar file.

As it stands, this question is not very specific to ImageJ and might be a better fit for In any case, it might help if you include the code of the custom-written FileReader class, or post an MCVE.

On the other hand, if you explain us what you want to achieve finally, someone here on the forum might be able to help you finding a simpler solution (since re-coding file readers is certainly not necessary for most cases, there are plenty of tools available out there…).

1 Like

Just like the program provided in this topic. Actually the FileReader use standard procedure to read in an image from File.class to ImageReader.class while the two ways varies in the


I think this is the reason why using the javax.imageIo.ImageReader. Are there any similar method in ImageJ ? It looks like a regular check to see if the read in image can be decoded. If I use ImagePlus and can successfully read the information such as the pixel value, then can I assume that the image is correctly decoded so that I don’t need the check anymore ???

one results in true and another results in false. And I haven’t found any different before that.
Any ideas? Thanks.

The preferred way to read in any kind of (biological data, microscopy,…) file in ImageJ is by using Bio-Formats. Have a look at the following resources: