BioFormat reading as ImagePlus in Java

My question is about what dependencies I should add in maven to read BioFormats. I created a sample project just to know the minimum dependencies I would need to use ImageJ.

In the following example I am trying to read a czi file where I include the following dependencies,
to have ImagePlus

<dependency>
     <groupId>net.imagej</groupId>
     <artifactId>ij</artifactId>
     <version>1.52q.1</version>
</dependency>

To add bioformats as per their website,
Just add formats-gpl.jar to your CLASSPATH or build path. You will also need ome-common.jar for common I/O functions, ome-xml.jar for metadata standardization, and SLF4J for Logging.

<dependency>
     <groupId>ome</groupId>
     <artifactId>formats-gpl</artifactId>
     <version>6.2.1</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>2.0.0-alpha1</version>
</dependency>

Then I include only the required dependencies as per need in a module file,

    requires ij;
    requires formats.gpl;
    requires loci.plugins;

Below is the code where I still need to import loci.formats.FormatException that I got from an example on bioformats website

import ij.ImagePlus;
import loci.plugins.BF;
import loci.formats.FormatException;

try {
         ImagePlus [] imps = BF.openImagePlus(selectedFile.getPath());
        } catch (FormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

ImagePlus imp = imps[0];

and so I needed also loci dependence,

<dependency>
     <groupId>loci</groupId>
     <artifactId>loci_plugins</artifactId>
     <version>4.4.12</version>
</dependency>

and to add it in the module,

    requires ij;
    requires formats.gpl;
    requires loci.plugins;
    requires formats.api;

which made a conflict as it reads package ‘loci.formats’ from both ‘formats.gpl’ and ‘formats.api’

Any suggestions or something to read would be appreciated?

Why not use bioformats_package first, to try to get something working?

1 Like

I assume you’re using the Java Module System. You may find it easier to avoid this at first, since if Bio-Formats or one of its dependencies aren’t compatible with it then this might also be causing problems. I still find JMS support quite limited among the tools I use.

1 Like

I think the confusion here is that you shouldn’t need loci_plugins for the FormatException, a lot of the package names start with loci. but that does not mean they are part of the loci_plugins jar. As Pete suggested, if in doubt using the bioformats_package jar may be the easiest way to ensure everything is included to begin with.

2 Likes

I didn’t know that to say the truth as I am only following suggestion from the internet and I intentionally used Java module system in order to clear things out for me which confused me more.

I will remove all and give this a try.

You are absolutely right, it just confused me more.

I noticed that I only need formats-gpl and loci-plugins to use the BF reading method and its FormatException but if I used SCIFIO dataset or ImageJ instance to access SCIFIO I only need IJ, ImageJ and Imglib2 yet I am having a problem error with the BF and problem with the jar file in Scifio or imageJ instances. So even with no conflict I still have the same problem. The conflict problem only showed by using JMS.

It does worked with using the following dependencies only,

        <dependency>
            <groupId>net.imglib2</groupId>
            <artifactId>imglib2-ij</artifactId>
            <version>2.0.0-beta-45</version>
        </dependency>
        <dependency>
            <groupId>ome</groupId>
            <artifactId>bio-formats_plugins</artifactId>
            <version>6.1.1</version>
        </dependency>

Using the BF Method now it runs normally using the jar,

        ImagePlus imp = null;
        try {
            ImagePlus[] imps = BF.openImagePlus(selectedFile.getPath());
        } catch (FormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

net.imglib2 provided the ImagePlus
bio-formats_plugins provided BF and FormatException

when I searched for bioformats_package I didn’t find one library to call in maven. I tried formats-common and the three formats (formats-gpl, formats-api, formats-bsd) separately and kept on until It worked with bio-formats-plugins.