将DICOM转换为JPEG时出错

时间:2014-12-14 20:45:10

标签: java image jpeg dicom

我正在处理将DICOM Iamge(.dcm)转换为JPEG格式的代码 - 这是我使用的代码,它给了我一个: javax.imageio.spi.ImageReaderSpi:提供者com.sun.media.imageioimpl.plugins.pcx.PCXImageReaderSpi未找到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;
    }
}

}

这是我收到的错误?任何想法我缺少什么

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)

2 个答案:

答案 0 :(得分:3)

在您的类路径中

某处,您没有条目(JAR中的文件或资源),名为:

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

...包含(至少)该行:

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

每当初始化ImageIO或调用ImageIO.scanForPlugins()时,IIORegistry/ServiceLoader将扫描类路径以查找与上述名称匹配的资源。对于此文件/资源​​中的每一行,它将尝试使用给定名称(通常是服务提供者)实例化该类。如果找不到该类,或者无法实例化(访问保护,缺少构造函数等),则它将失败并显示ServiceConfigurationError

有关详细信息,请参阅示例this link

要修复,您需要删除文件或包含资源的JAR(或从JAR中编辑它),或删除包含错误服务提供程序的行。或者,将服务提供者添加到您的类路径中(我认为它通常位于名为jai_imageio.jar的JAR中)。但我认为您不需要PCX(ZSoft Paintbrush格式)支持来解码DICOM。

答案 1 :(得分:-1)

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

这是一个示例,其中在JRE中未完成查找,并允许无法实例化的类。