Question

I try to implement sample piece of code to get image from DicomObject:

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.dcm4che2.data.DicomObject;
import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;
import org.dcm4che2.io.DicomOutputStream;

....

public static BufferedImage getPixelDataAsBufferedImage(byte[] dicomData)
        throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(dicomData);
    BufferedImage buff = null;
    Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
    ImageReader reader = (ImageReader) iter.next();
    DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    reader.setInput(iis, false);
    buff = reader.read(0, param);
    iis.close();
    if (buff == null) {
        throw new IOException("Could not read Dicom file. Maybe pixel data is invalid.");
    }
    return buff;
}

When ImageReader reader = (ImageReader) iter.next(); I got:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/media/imageio/stream/StreamSegmentMapper
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi.createReaderInstance(DicomImageReaderSpi.java:146)
at javax.imageio.spi.ImageReaderSpi.createReaderInstance(ImageReaderSpi.java:320)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:529)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:513)
at dcm4.dcmTools.loadImage(dcmTools.java:61)
at dcm4.Dcm4.main(Dcm4.java:87)
Caused by: java.lang.ClassNotFoundException: com.sun.media.imageio.stream.StreamSegmentMapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 6 more

Does anyone knows What is wrong??? I use JDK7 64 on Windows7 64

Was it helpful?

Solution

The given exception indicates that you have a runtime dependency on Java Advanced Imaging (JAI). You need additional JARs not provided with the standard JDK.

Here are the JAI and JAI ImageIO Tools installation instructions:

http://download.java.net/media/jai/builds/release/1_1_3/INSTALL.html#Windows
http://download.java.net/media/jai-imageio/builds/release/1.1/INSTALL-jai_imageio.html#Windows

You can download the installers here:

http://download.java.net/media/jai/builds/release/1_1_3/
http://download.java.net/media/jai-imageio/builds/release/1.1/

Once you install JAI, you will need to include the extracted JARs in your project classpath.

Maven

If using Maven, JAI and JAI ImageIO Tools can be used for your project without installers:

<dependency>
    <groupId>com.sun.media</groupId>
    <artifactId>jai_imageio</artifactId>
    <version>1.1</version>
</dependency>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top