gpt4 book ai didi

java - 将 DICOM 转换为 JPEG 时出错

转载 作者:行者123 更新时间:2023-12-02 05:09:13 57 4
gpt4 key购买 nike

我正在编写将 DICOM Iamge (.dcm) 转换为 JPEG 格式的代码 - 这是我使用的代码,它给了我一个:javax.imageio.spi.ImageReaderSpi:Provider com.sun.media.imageioimpl.plugins.pcx.PCXImageReaderSpi 未找到错误...

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)

最佳答案

在类路径的某个地方,有一个条目(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。

关于java - 将 DICOM 转换为 JPEG 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27473895/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com