Read/open image from resource inside a JAR file?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Read/open image from resource inside a JAR file?

Burger Wilhelm
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
Reply | Threaded
Open this post in threaded view
|

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

Michael Schmid
Hi Wilhelm,

a rough guess, stealing code from here and there...

If you have a plugin named MyPlugin, and a directory 'images' in its
.jar file, you can use
   URL imageURL = MyPlugin.class.getResource("images/myImage.png");

Then use the ImageJ URLOpener class or get a BufferedImage with
javax.imageio.ImageIO:
   BufferedImage bi = ImageIO.read(url);


Michael
________________________________________________________________
On 2016-05-31 16:22, Burger Wilhelm wrote:

> 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
Reply | Threaded
Open this post in threaded view
|

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

John Hayes
In reply to this post by Burger Wilhelm
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
Reply | Threaded
Open this post in threaded view
|

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

Rasband, Wayne (NIH/NIMH) [E]
In reply to this post by Burger Wilhelm
> On May 31, 2016, at 10:22 AM, Burger Wilhelm <[hidden email]> wrote:
>
> 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. …

The JAR Resources Demo plugin at

    https://imagej.nih.gov/ij/plugins/pcl-demo.html

opens a JPEG image, a TIFF image and a text file contained in a JAR file. It opens the JPEG using a URL and the TIFF using an InputStream.

-wayne


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

Burger Wilhelm
In reply to this post by John Hayes
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
Reply | Threaded
Open this post in threaded view
|

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

Burger Wilhelm
In reply to this post by Rasband, Wayne (NIH/NIMH) [E]
Thanks Wayne for this hint, I must have overlooked that plugin when I searched for it.
I am not completely happy with this solution though, since every file/image type is handled individually. Against my initial intentions (and lacking a better idea), I now consider extracting arbitrary resources to temporary files and subsequently load/process them.

--Wilhelm

-----Original Message-----
From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Rasband, Wayne (NIH/NIMH) [E]
Sent: Mittwoch, 01. Juni 2016 02:19
To: [hidden email]
Subject: Re: Read/open image from resource inside a JAR file?

> On May 31, 2016, at 10:22 AM, Burger Wilhelm <[hidden email]> wrote:
>
> 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. …

The JAR Resources Demo plugin at

    https://imagej.nih.gov/ij/plugins/pcl-demo.html

opens a JPEG image, a TIFF image and a text file contained in a JAR file. It opens the JPEG using a URL and the TIFF using an InputStream.

-wayne


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

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

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

John Hayes
In reply to this post by Burger Wilhelm
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
Reply | Threaded
Open this post in threaded view
|

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

Burger Wilhelm
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
Reply | Threaded
Open this post in threaded view
|

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

ctrueden
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