Dexter
Dexter

Reputation: 35

Error while converting DICOM to JPEG

i was working on the code to convert a DICOM Iamge (.dcm) to JPEG format - here is the code i used, it is giving me a: javax.imageio.spi.ImageReaderSpi:Provider com.sun.media.imageioimpl.plugins.pcx.PCXImageReaderSpi not found ERROR ...

import java.awt.image.BufferedImage;
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.dcm4che.imageio.plugins.DcmImageReadParam;
import org.dcm4che.util.BufferedOutputStream;
import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;
public class DicomToJpeg
{
    public static void main(String[] args)
    {
        //our DICOM file
        File my`enter code here`DicomFile = new File("IM-0001-0001.dcm");

    //declaring our JPEG image
    BufferedImage myJpegImage = null;

    //iterator containing all registered ImageReades that claim to decome the name format
    Iterator<ImageReader> iter= ImageIO.getImageReadersByFormatName("DICOM");

    //geting our ImageReader object
    ImageReader reader = (ImageReader) iter.next();

    //getting the parameters to read the DICOM image 
    DicomImageReadParam param = (DicomImageReadParam)reader.getDefaultReadParam();

    try
    {
        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 teh dicom image!");
            return;
        }

        File myJpegFile = new File("OutputJpegImage.jpg");
        OutputStream output = new BufferedOutputStream( new FileOutputStream(myJpegFile));
        ImageIO.write(myJpegImage, "jpeg", output);
        output.close();
    }
    catch (IOException e) 
    {
           System.out.println("\nError: couldn't read dicom image!"+ e.getMessage());
           return;
    }
}

}

This is the error i am receiving ? any idea what am i missing

Exception in thread "main" java.util.ServiceConfigurationError: javax.imageio.spi.ImageReaderSpi:   Provider com.sun.media.imageioimpl.plugins.pcx.PCXImageReaderSpi not found
    at java.util.ServiceLoader.fail(Unknown Source)
    at java.util.ServiceLoader.access$300(Unknown Source)
    at java.util.ServiceLoader$LazyIterator.next(Unknown Source)
    at java.util.ServiceLoader$1.next(Unknown Source)
    at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(Unknown Source)
    at javax.imageio.spi.IIORegistry.<init>(Unknown Source)
    at javax.imageio.spi.IIORegistry.getDefaultInstance(Unknown Source)
    at javax.imageio.ImageIO.<clinit>(Unknown Source)
    at dcm4chee.DicomToJpeg.main(DicomToJpeg.java:29)

Upvotes: 1

Views: 1870

Answers (2)

user579013
user579013

Reputation: 197

static
{
    for (Iterator<urclass> im = ServiceLoader.load(urclass.class).iterator(); im.hasNext();)
        try
        {
            urclass tclass = im.next();
        }
        catch (ServiceConfigurationError e)
        {
        }
}

This is a sample where the lookup is not done in the JRE and allows classes that can't be instantiated.

Upvotes: -1

Harald K
Harald K

Reputation: 27084

Somewhere in your classpath, you haven an entry (a file or a resource inside a JAR), named:

/META-INF/services/javax.imageio.spi.ImageReaderSpi

...containing (at least) the line:

com.sun.media.imageioimpl.plugins.pcx.PCXImageReaderSpi

Whenever ImageIO is initialized, or an invocation of ImageIO.scanForPlugins() is made, the IIORegistry/ServiceLoader will scan the class path for resources matching the name above. For each line in this file/resource, it will try to instantiate the class with the given name (typically a service provider). If the class can't be found, or can't be instantiated (access protection, missing constructor etc.), it will fail with a ServiceConfigurationError.

See for example this link for more information.

To fix, you either need to remove the file or the JAR containing the resource (or edit it out of the JAR), or remove the line containing the bad service provider. Alternatively, add the service provider to your class path (I think it's normally in a JAR called jai_imageio.jar). But I don't think you'll need PCX (ZSoft Paintbrush format) support to decode DICOM.

Upvotes: 4

Related Questions