小编典典

将DICOM图像转换为JPEG图像

jsp

我的代码是

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应用程序执行时,在此我从控制器页面调用它,例如

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)

如何解决此错误,请帮帮我....


阅读 548

收藏
2020-06-08

共1个答案

小编典典

ImageIO.getImageREadersByFormatName的Javadoc说:

返回一个Iterator,它包含所有当前已注册的ImageReader,这些ImageReader声称能够解码指定的格式。

如果在不检查迭代器是否包含元素的情况下访问迭代器,则会得到异常。

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

但是,我也想知道您如何称呼以上课程。是来自servlet吗?

2020-06-08