dcm4che dcm和jpg互转,hl7和xml互转,解析hl7,解析dcm

工作中需要用到处理dicom和hl7两种医疗存储文件,简单记录下遇到的坑,希望能帮到有需要的朋友。

首先maven引入dcm4che相关的依赖包(文章末尾列出)

1:dicom和jpg互转的代码:

 public static boolean dcm2jpg(File dcmFile,String jpgFullFilePath)
    {

        try {
            File file=new File(jpgFullFilePath);
            if(!file.exists())
                file.mkdirs();
            Dcm2Jpg dcm2jpg = new Dcm2Jpg();
            dcm2jpg.initImageWriter("JPEG", null, null, null, 1l);
            dcm2jpg.convert(dcmFile, file);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

类似的,图片转dicom需要用到工具类Jpg2Dcm.java,不会调用的话可以查看这个类中的main方法,根据他的步骤调用就可以了。注意,这里不能转心电图的dicom,心电图的以后有空写。

2:hl7和xml互转:

这里给出一个hl7示例方便测试:

MSH|^~\&|HL7Soup|Instance1|HL7Soup|Instance2|20060307110114||ORM^O01|MSGID20060307110114|P|2.5.1
PID||81243|12001||Jones^John^^^Mr.||19670824|M|||123 West St.^^Denver^CO^80020^USA|||||||
PV1||O|OP^PAREG^||||2342^Jones^Bob|||OP|||||||||2|||||||||||||||||||||||||20060307110111|
ORC|NW|20060307110114

OBR|1|20060307110114||003038^Urinalysis^L|||20060307110114

private static void xml2hl7() throws Exception
    {
        SAXParserFactory f = SAXParserFactory.newInstance();
        SAXParser p = f.newSAXParser();

        PrintStream pw=new PrintStream(new FileOutputStream(new File("f:/hl7/要保存的文件.txt")));
        System.setOut(pw);
        HL7ContentHandler ch = new HL7ContentHandler(new OutputStreamWriter(System.out));
        p.parse(new File("f:/hl7/要转换的文件.xml"), ch);
    }
 private static void hl72xml() throws Exception
    {
     
        File f = new File("f:/hl7/保存的文件.xml");
        FileOutputStream fileOutputStream = new FileOutputStream(f,false);
        //true可以追加到文件output.txt的末尾,没有true 每次新建一个output.txt
        PrintStream printStream = new PrintStream(fileOutputStream,false);
        PrintStream ps=System.out;

        //此处要同步,其他地方不可同时使用System.out
        // 重新分配“标准”输出流
        System.setOut(printStream);
        HL72Xml hl72Xml=new HL72Xml();
         hl72Xml.setCharacterSet("UNICODE");
        InputStream dis = new FileInputStream("f:/hl7/要转换的文件.txt");
        try {
           hl72Xml.parse(dis);
           System.setOut(ps);
        } finally {
            dis.close();
        }


    }

       记住上面字符编码一定要设置成UNICODE,否则会出现中文乱码,底层会设置成UTF-8,不要直接设置成UTF-8,这样会设置失败,但并不报错。遇到的坑啊,具体原因是因为内部判定字符编码的时候根据传入的字符串的最后一个字符的ASCII进行一个switch判断,其中一个如果是E的话返回UTF-8.另外此处用到了标准输出流,记得同步下代码块,此处我还没加上。否则在转换期间其他地方调用了标准输出流可能会影响你保存文件内容。

3:解析hl7:

public static void main(String args[]) throws Exception
    {

        File hl7File=new File("f:/hl7.hl7");
        InputStream is=new FileInputStream(hl7File);
        byte[] bytes=new byte[is.available()];
        is.read(bytes);

        HL7Message message=HL7Message.parse(bytes,"UNICODE");
        ;
        if(message!=null && message.size()>0)
        {
            HL7Segment segment=message.get(0);

        }
        //打印出全部hl7格式信息
        System.out.println(message.toString('\n'));


    }


4:解析dcm:

                        // 打开dicom
			File fileObject = new File(path);
			DicomInputStream din = new DicomInputStream(fileObject);
			DicomObject dicomObject = din.readDicomObject();
			din.close();
                        String tmp = dicomObject.getString(Tag.PatientName);

Tag中预定义了所有可以获取的常量信息。


---------------------------------------------------------------------------------------------------------------------------------

相关依赖的jar包:

  <dependency>
        <groupId>org.dcm4che.tool</groupId>
        <artifactId>dcm4che-tool-dcm2jpg</artifactId>
        <version>dcm4chee-arc-light-5.10.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.dcm4che.tool/dcm4che-tool-xml2dcm -->
    <dependency>
        <groupId>org.dcm4che.tool</groupId>
        <artifactId>dcm4che-tool-xml2dcm</artifactId>
        <version>5.13.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
    <dependency>
        <groupId>commons-cli</groupId>
        <artifactId>commons-cli</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.dcm4che.tool/dcm4che-tool-dcm2xml -->
    <dependency>
        <groupId>org.dcm4che.tool</groupId>
        <artifactId>dcm4che-tool-dcm2xml</artifactId>
        <version>5.13.2</version>
    </dependency>
    <dependency>
        <groupId>org.dcm4che</groupId>
        <artifactId>dcm4che-core</artifactId>
        <version>5.13.2</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.dcm4che.tool/dcm4che-tool-xml2hl7 -->
    <dependency>
        <groupId>org.dcm4che.tool</groupId>
        <artifactId>dcm4che-tool-xml2hl7</artifactId>
        <version>5.13.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.dcm4che.tool/dcm4che-tool-hl72xml -->
    <dependency>
        <groupId>org.dcm4che.tool</groupId>
        <artifactId>dcm4che-tool-hl72xml</artifactId>
        <version>5.13.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/dom4j/dom4j -->
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.jaxen/com.springsource.org.jaxen -->
    <dependency>
        <groupId>org.jaxen</groupId>
        <artifactId>com.springsource.org.jaxen</artifactId>
        <version>1.1.1</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.dcm4che/dcm4che-hl7 -->
    <dependency>
        <groupId>org.dcm4che</groupId>
        <artifactId>dcm4che-hl7</artifactId>
        <version>5.13.2</version>
    </dependency>

源码下载链接:demo下载


ps:有问题可以留言,看到回答







展开阅读全文

没有更多推荐了,返回首页