gpt4 book ai didi

java - 将 DICOM 图像转换为 jpeg 图像

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:44 27 4
gpt4 key购买 nike

我的代码是

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;
}
}
}

当我使用 eclipse 在 java 项目中执行时,它工作正常......但是当我使用 web 应用程序执行时,我从 Controller 页面调用它,比如

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

然后它会给出类似...的错误

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)

如何解决这个错误,请帮助我....

最佳答案

ImageIO.getImageREadersByFormatName 的 javadoc 说:

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

如果你访问迭代器而不检查它是否有一个元素,你会得到一个异常。

由于它在您的 IDE 中运行,而不是在服务器上运行,您可能会查看 DICOM 的图像读取器是否在服务器上应用程序的类路径中。

不过,我也想知道上面的类是怎么调用的。它来自 servlet 吗?

关于java - 将 DICOM 图像转换为 jpeg 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20094508/

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