将DICOM图像转换为jpeg图像 [英] Converting DICOM image to jpeg image

查看:133
本文介绍了将DICOM图像转换为jpeg图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

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

当我在java项目中执行时使用eclipse它工作正常......
但是当我使用Web应用程序执行时,我在控制器页面中调用它,如

When i execute in java project using eclipse it work fine... But when i execute using web application and in this i call it from controller page like


DicomToJpeg.dicomToJpeg(d:/ F74AFBC7);

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

然后它给出错误,如...

then it gives error like...

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)

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

How to solve this error please help me....

推荐答案

到ImageIO.getImageREadersByFormatName的javadoc说:

The javadoc to ImageIO.getImageREadersByFormatName says:


返回包含当前所有内容的迭代器雷吉斯声称能够解码指定格式的ImageReaders

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

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

If you access the iterator without checking if it has an element, you will get an exception.

因为它在你的IDE中而不是在服务器上运行,所以你可以查看DICOM的图像阅读器是否在应用程序在服务器上的类路径。

Since it runs in you IDE and not on the server, you may have a look if the image readers for the DICOM is in the application's classpath on the server.

但是,我还想知道你如何调用上面的类。它来自servlet吗?

However, I would also like to know how do you call the above class. Is it from a servlet?

这篇关于将DICOM图像转换为jpeg图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆