[JAVA] Reading JPEG2000 Images

Hello!

I want to read JPEG2000 Images (.jp2) files with Java. I started my first tries with nativ JAI and JPEG2000ImageReader but failed … Stackoverflow Issue

So I tried to import the whole snap-engine into my intellij project as library.
If I try following code I receive a “NoSuchFileException” Error (but the file is there)

public class Main {

public static void main(String[] args) throws IOException {
    Image result = null;
    RenderedImage image0 = null;
    String path = "C:\\temp\\B2.jp2";
    File imageFile = new File(path);

    image0 = TiledFileOpImage.create(imageFile, new Properties());

}
}

The error is:

Exception in thread “main” java.nio.file.NoSuchFileException: C:\temp\B2.jp2\image.properties
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.spi.FileSystemProvider.newInputStream(FileSystemProvider.java:384)
at java.nio.file.Files.newInputStream(Files.java:152)
at java.nio.file.Files.newBufferedReader(Files.java:2784)
at java.nio.file.Files.newBufferedReader(Files.java:2816)
at org.esa.snap.core.image.ImageHeader.load(ImageHeader.java:56)
at org.esa.snap.core.image.TiledFileOpImage.create(TiledFileOpImage.java:71)
at org.esa.snap.core.image.TiledFileOpImage.create(TiledFileOpImage.java:63)
at Main.main(Main.java:23)

Someone a tip which maybe helps?

It is not so easy to read JPEG2000 files. In SNAP the native library OpenJPEG is used.
I haven’t tried it but maybe the plugins for JAI are helpful for you.

Hi! Thanks for the replay!

Is it possible to import SNAP into my project and open jp2 files through SNAP ?
I already tried jai-imageio … also no success

kind regards Lukas

Yes you can follow this guide to bundle the jars from SNAP and S2TBX. If you add these jars to your classpath then you can read the JPEG2000 files as SNAP products and retrieve the images from them
Some sample code:

Product jp2Product = ProductIO.readProduct("/path/to/my.jp2");
Band band1 = jp2Product.getBand("band_1");
PlanarImage jp2Image = band1.getSourceImage();