Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting DICOM image to jpeg image

Tags:

java

jsp

My code is

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;    
import java.io.File;    
import java.io.FileOutputStream;   
import java.io.IOException;  
import java.io.OutputStream;    
import java.util.Iterator;    
import javax.imageio.ImageIO;    
import javax.imageio.ImageReader;    
import javax.imageio.stream.ImageInputStream;    
import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;    
import com.sun.image.codec.jpeg.JPEGCodec;    
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class DicomToJpeg {    
    public static void main(String args[]) throws IOException, Exception
    {
        dicomToJpeg("d:/F74AFBC7");
    }

    public static void dicomToJpeg(String args) throws IOException, Exception {
        // TODO Auto-generated method stub      
        try 
        {               
            File myDicomFile = new File(args);
            BufferedImage myJpegImage = null;
            Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
            ImageReader reader = (ImageReader) iter.next();
            DicomImageReadParam param = null;
            try{                    
                param = (DicomImageReadParam) reader.getDefaultReadParam();
            }
            catch (Exception e) {                   
                e.printStackTrace();
            }
         ImageInputStream iis=ImageIO.createImageInputStream(myDicomFile);
                   reader.setInput(iis, false);   
                   myJpegImage = reader.read(0, param);   
                   iis.close();
                   if (myJpegImage == null) {
                          System.out.println("\nError: couldn't read dicom image!");
                          return;
                       }

                   File myJpegFile = new File("d:/demo.jpg");   
                   OutputStream output = new BufferedOutputStream(new FileOutputStream(myJpegFile));
                   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
                   encoder.encode(myJpegImage);
                   System.out.println("Image Create successufully");
                   output.close();

            } 
            catch(IOException e){
               System.out.println("\nError: couldn't read dicom image!"+ e.getMessage());
               return;
            }
    }
}

When i execute in java project using eclipse it work fine... But when i execute using web application and in this i call it from controller page like

DicomToJpeg.dicomToJpeg("d:/F74AFBC7");

then it gives error like...

java.util.NoSuchElementException
    at javax.imageio.spi.FilterIterator.next(Unknown Source)
    at javax.imageio.ImageIO$ImageReaderIterator.next(Unknown Source)
    at javax.imageio.ImageIO$ImageReaderIterator.next(Unknown Source)
    at com.lifecare.controller.DicomToJpeg.dicomToJpeg(DicomToJpeg.java:32)

How to solve this error please help me....

like image 616
Panchotiya Vipul Avatar asked Nov 20 '13 11:11

Panchotiya Vipul


2 Answers

The javadoc to ImageIO.getImageREadersByFormatName says:

Returns an Iterator containing all currently registered ImageReaders that claim to be able to decode the named format.

If you access the iterator without checking if it has an element, you will get an exception.

Since it runs in you IDE and not on the server, you may have a look if the image readers for the DICOM is in the application's classpath on the server.

However, I would also like to know how do you call the above class. Is it from a servlet?

like image 155
Spindizzy Avatar answered Oct 26 '22 06:10

Spindizzy


I solved it by calling ImageIO.scanForPlugins() before ImageIO.getImageReadersByFormatName()

ImageIO.scanForPlugins()            
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
ImageReader reader = (ImageReader) iter.next();

This works perfectly on servlets

like image 45
Santino 'Sonny' Corleone Avatar answered Oct 26 '22 06:10

Santino 'Sonny' Corleone