java解析dcm文件

1,下载源码编译

下载dcm3che源码

mvn install

2,gradle/maven 引用

compile ('org.dcm4che:dcm4che-core:5.20.0'){
        exclude group: 'org.slf4j',module: 'slf4j-log4j12'
    }

3,编写工具类


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.io.DicomInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ThreadLocalRandom;

/**
 * dcm解析工具
 *
 * @author zhy
 */
@Slf4j
public class DcmUtil {

    private DcmUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 读取Dicom源数据
     *
     * @param inputStream dcm文件流
     * @return Attributes
     */
    private static Attributes loadDicomObject(InputStream inputStream) throws IOException {
        DicomInputStream dis = new DicomInputStream(inputStream);
        return dis.readDataset(-1, -1);
    }

    /**
     * 解析dcm数据到对象
     *
     * @param inputStream 输入流
     * @return Medical 自定义对象
     */
    public static Medical conversionMedical(InputStream inputStream) {
        Attributes attrs;
        try {
            attrs = loadDicomObject(inputStream);
            if (attrs == null) {
                throw new MedicalViewerException("解析dcm出错,没有获取到数据!");
            }
            String[] imagePositionPatients = attrs.getStrings(Tag.ImagePositionPatient);
byte[] bytename = attrs.getBytes(Tag.PatientName);
        System.out.println("姓名: " + new String(bytename,"gb18030"));
        byte[] bytesex = attrs.getBytes(Tag.PatientSex);
        System.out.println("性别: " + new String(bytesex,"gb18030"));          
                return Medical
                    .builder()
                    .modality(attrs.getString(Tag.Modality, ""))
                    .institutionName(attrs.getBytes(Tag.InstitutionName) == null ? "" :
                            new String(attrs.getBytes(Tag.InstitutionName), "gb18030"))
                    .stationName(attrs.getBytes(Tag.StationName) == null ? "" :
                            new String(attrs.getBytes(Tag.StationName), "gb18030"))
                    ))
                    .build();
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
        return Medical.builder().build();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值