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