Java 1.4+ DICOM ImageIO SPI

Update: 3-25-04 - update to handle sequences and performance improvements, changed license to MIT style
Update: 9-15-03 - fixed bug that prevented opening of non-DICOM images in most cases

Here is version 0.20 of a quick and dirty but easy to use DICOM image reader implemented in Java. This will allow you to open a DICOM image as BufferedImage using the ImageIO framework provided with Java 1.4+. It requires JDK 1.4 or greater to compile or run. This is released under the MIT license.

The source is here: dicom.tar.gz

The binary is here: dicom.jar

Here is some sample code:

       import javax.imageio.*;
       import java.awt.image.*;
       import java.awt.*;
       import java.io.*;

       public class test {
         public static void main(String args[]) {
           try {
             BufferedImage image = ImageIO.read(new File("/home/shah/DICOM/1ksmpte.dcm"));
             System.out.println(image.getWidth());
           } catch (Exception e) {
             e.printStackTrace();
           }
         }
       }

    
To compile:
    [shah@Von ~] javac test.java
    
To run and the resulting output:
    [shah@Von ~] java -cp dicom.jar:. test
    1024
    
Opening a DICOM image for as a BufferedImage is rather easy, however getting other information from the DICOM header is possible but still rather messy at the moment. You will need to code something like this:
    import java.awt.*;
    import java.awt.image.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    import javax.imageio.stream.*;
    import edu.ucla.rip.imageio.*;

    public class test {
      public static void main(String args[]) {
        try {
          File f = new File("image.dcm");

          Iterator readers = ImageIO.getImageReadersByFormatName("dcm");
          ImageReader reader = (ImageReader)readers.next();
          reader.setInput(new FileImageInputStream(f), true);

          // get the header as a hashtable

          Hashtable header = ((DICOMImageReader)reader).getDICOMHashtable();

          BufferedImage image = ((DICOMImageReader)reader).read(0, null);

          // let's retrieve the instance number dicom tag (0020,0013)
          int instance = Integer.parseInt((String)DICOMElement.getValueFromHash(header, 0x00200013));

          System.out.println(instance);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }      
    
To compile:
    [shah@Von ~] javac -classpath dicom.jar test.java
    
To run and the resulting output:
    [shah@Von ~] java -cp dicom.jar:. test
    1
    
Any questions, just mail me:

This code was inspired/adapted/stolen from NIH ImageJ and DCMTK.