先说一下背景吧,有一台眼底照相机,拍摄患者的眼底信息后发送dicom文件到指定的服务器上面,然后再解析dicom文件,获取其中的图片和患者的其他信息,入库 给医生筛查。
在gitHub上面找了一个监听指定端口的服务,并把dicom文件保存在本地。代码如下:
protected void store(Association as, PresentationContext pc, Attributes rq,
PDVInputStream data, Attributes rsp) throws IOException {
String cuid = rq.getString(Tag.AffectedSOPClassUID);
String iuid = rq.getString(Tag.AffectedSOPInstanceUID);
System.out.println(rq.getString(Tag.PatientName));
String tsuid = pc.getTransferSyntax();
File storageDirectory = ((DicomServerApplicationEntity)
as.getApplicationEntity()).getStorageDirectory();
if(storageDirectory == null) {
logger.error("Unable to store instance with SOP Instance UID " + iuid +
": no storage directory configured");
return;
}
if(!storageDirectory.exists()){
storageDirectory.mkdirs();
}
File partFile = new File(storageDirectory, iuid + PART_EXT);
File instanceFile = new File(storageDirectory, iuid + FILE_EXT);
try {
storeTo(as, as.createFileMetaInformation(iuid, cuid, tsuid),
data, partFile);
renameTo(as, partFile, instanceFile);
} catch(Exception e) {
deleteFile(as, partFile);
throw new DicomServiceException(Status.ProcessingFailure, e);
}
for(StoreSCPListener storeSCPListener: storeSCPListeners){
storeSCPListener.onInstanceReceived(as, iuid, instanceFile);
}
sendPatientInfo(iuid);
}
存储到本地指定文件夹里面之后,就需要解析该文件了。可以看一下我写的测试类
File file = new File("/home/guoyh/yueke/gyh1.dcm");
DisplayTag d = new DisplayTag(file);
@SuppressWarnings("static-access")
Attributes attr1 = d.loadDicomObject(file);
byte[] bytename = attr1.getBytes(Tag.PatientName);
System.out.println(new String(bytename,"gb18030"));
byte[] bytesex = attr1.getBytes(Tag.PatientSex);
System.out.println(new String(bytesex,"gb18030"));
其中DisplayTag类是一个工具类,通过声明的file 对象,把dicom文件加载进来。其中 dicom文件的内容是由一个一个的tag组成,比如:0008,0005这个Tag是指定这个dicom文件的编码方式。获取到眼底相机传输过来的dicom文件之后,我试着去解析他,发现在0008,0005获取到的值是null,而且输入的中文全部都是乱码。打电话咨询了一下,得知编码方式是GB18030,然后就像上面的例子那样获取到指定Tag的byte[] ,再转化成String ,大功告成。
dcm4che开源框架还是很强大的。但是国内的资料比较少,刚开始做的时候也是挺费劲。而且maven好像国内的镜像没有jar包,我是导入到项目中,maven引用的本地jar。有知道的大神可以跟我说一下。如果有什么疑问,可以加我QQ guo_yuhao@foxmail.com,大家共同探讨进步!