Login  Register

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

Posted by John Hayes on Jun 01, 2016; 3:20pm
URL: http://imagej.273.s1.nabble.com/Read-open-image-from-resource-inside-a-JAR-file-tp5016554p5016572.html

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