Login  Register

Re: Read/open image from resource inside a JAR file?

Posted by ctrueden on Jun 03, 2016; 4:39pm
URL: http://imagej.273.s1.nabble.com/Read-open-image-from-resource-inside-a-JAR-file-tp5016554p5016588.html

Hi Wilhelm,

> does anyone know of a straightforward way to read/open an image that
> is contained as a resource inside a JAR file? That is, without perhaps
> copying an java.io.InputStream to a temporary file etc. ...

For performance, you should probably copy the InputStream to a temporary
file. Some formats including TIFF may require lots of random access seeks
to read the data, so limiting yourself to a stream makes life difficult.

That said, SCIFIO intends to support reading image data from streams (using
mark/reset internally). But depending on the size and structure of your
image file, you may not be happy with the performance.

Here is an example Groovy script:

--snip--
    // @DatasetIOService io
    // @UIService ui
    // @OUTPUT Dataset image

    imagePath = "about.jpg"

    // to search only the JAR containing the current class
    //url = getClass().getResource(imagePath)

    // to search all available JARs
    resources =
Thread.currentThread().getContextClassLoader().getResources(imagePath)
    url = resources.hasMoreElements() ? resources.nextElement() : null

    if (url == null) {
      ui.showDialog("No such image found: " + imagePath)
      return
    }

    // open the URL using SCIFIO
    image = io.open(url.toString())
--snap--

Unfortunately, it seems there is a bug right now. I filed an issue for it:
https://github.com/scifio/scifio/issues/304

I know this doesn't help you in the moment, but just wanted to mention that
this will become easier in the future.

Regards,
Curtis

--
Curtis Rueden
LOCI software architect - http://loci.wisc.edu/software
ImageJ2 lead, Fiji maintainer - http://imagej.net/User:Rueden
Did you know ImageJ has a forum? http://forum.imagej.net/


On Wed, Jun 1, 2016 at 10:43 AM, Burger Wilhelm <
[hidden email]> wrote:

> Another interesting idea! Will look into this ...
>
> Thanks much,
> Wilhelm
>
> -----Original Message-----
> From: John Hayes [mailto:[hidden email]]
> Sent: Mittwoch, 01. Juni 2016 17:36
> To: Burger Wilhelm <[hidden email]>
> Cc: John Hayes <[hidden email]>; [hidden email]
> Subject: Re: Read/open image from resource inside a JAR file?
>
> Hi Wilhelm,
>
> Do you know what file formats you need support for?
>
> Anyway, it's a bit more complicated, and I don't know what file formats it
> supports (looks like at least PNG), but you might can try the following
> (slightly modified from code in Opener::openPngUsingURL):
>
> >       Image img = null;
> >         try {
> >             img = ImageIO.read(jarInputStream);
> >          } catch (IOException e) {
> >                IJ.log(""+e);
> >          }
> >          if (img!=null) {
> >              ImagePlus imp = new ImagePlus(title, img);
> >              return imp;
> >           } else
> >                  return null;
>
> ImageIO is in the javax.imagio package. If the file format you are
> interested is not supported by ImageIO by default, you might be able to add
> support for additional formats as highlighted in this thread:
>
> http://stackoverflow.com/questions/17021391/how-to-make-imageio-support-more-formats
>
> HTH,
>
> John
>
> Le 1 juin 2016 à 10:57, Burger Wilhelm a écrit :
>
> > Hi John,
> >
> > excellent hint! -- works well, but unfortunately my images are not
> necessarily in TIF format.
> >
> > Thanks much,
> > Wilhelm
> >
> >
> > -----Original Message-----
> > From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> John Hayes
> > Sent: Dienstag, 31. Mai 2016 18:19
> > To: [hidden email]
> > Subject: Re: Read/open image from resource inside a JAR file?
> >
> > Hi Wilhelm,
> >
> > I believe the ij.io.Opener class has an "openTiff(java.io.InputStream
> in, java.lang.String name)" function that returns an ImagePlus object (if
> that's the type of images in your .jar file). You should be able to pass a
> JarInputStream to it to retrieve your images.
> >
> > HTH,
> >
> > John
> >
> > Le 31 mai 2016 à 10:22, Burger Wilhelm a écrit :
> >
> >> Hello group,
> >>
> >> does anyone know of a straightforward way to read/open an image that is
> contained as a resource inside a JAR file? That is, without perhaps copying
> an java.io.InputStream to a temporary file etc. ...
> >>
> >> Any hints highly appreciated
> >> -- Wilhelm
> >>
> >>
> >>
> >> --
> >> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html