给出java代码使用dcm4che库获取dcm4chee服务的dicom文件
时间: 2023-08-29 14:08:21 浏览: 272
dcm4che:在JAVA中实现DICOM
以下是使用dcm4che库获取dcm4chee服务的Dicom文件的Java代码示例:
```java
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.io.DicomInputStream;
import org.dcm4che3.net.ApplicationEntity;
import org.dcm4che3.net.Connection;
import org.dcm4che3.net.Device;
import org.dcm4che3.net.QueryOption;
import org.dcm4che3.net.service.DicomServiceException;
import org.dcm4che3.net.service.QueryRetrieveSCP;
import org.dcm4che3.net.service.QueryRetrieveService;
import org.dcm4che3.util.SafeClose;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.List;
public class DicomRetrieveExample {
public static void main(String[] args) throws Exception {
// 连接dcm4chee服务
Device device = new Device("retrieve");
Connection conn = new Connection();
conn.setHostname("dcm4chee-server");
conn.setPort(11112);
device.setConnection(conn);
ApplicationEntity ae = new ApplicationEntity("retrieve");
ae.addConnection(conn);
device.addApplicationEntity(ae);
device.setAssociationInitiator(true);
device.setPackPDV(true);
device.setTcpNoDelay(true);
device.setConnectTimeout(5000);
device.setRequestTimeout(5000);
device.setReceivePDULength(16384);
device.setSendPDULength(16384);
device.setRspTimeout(5000);
device.setAssociationAcceptor(true);
device.setAsyncOpsWindow(0, 1);
device.setMaxAsyncOpsInvoked(1);
device.setMaxAsyncOpsPerformed(1);
device.setStorageSCP(new QueryRetrieveSCP("*") {
@Override
protected void store(Socket socket, InputStream data, int pcid, String tsuid, Attributes attrs, Attributes rsp)
throws IOException, DicomServiceException {
// 不存储
}
});
device.startListening();
// 检索Dicom文件
QueryRetrieveService qrService = ae.getDevice().getQrService();
qrService.setQueryOptions(QueryOption.RELATIONAL);
qrService.setRetrieveTimeout(10000);
qrService.setCMoveDestination(conn.getCalledAET());
List<String> studies = qrService.queryStudies("*");
for (String study : studies) {
List<String> series = qrService.querySeries(study, "*");
for (String serie : series) {
List<String> instances = qrService.queryInstances(serie, "*");
for (String instance : instances) {
// 获取Dicom文件
DicomInputStream dis = null;
FileOutputStream fos = null;
try {
dis = new DicomInputStream(qrService.retrieveInstance(instance));
Attributes attrs = dis.readDataset(-1, -1);
// 保存Dicom文件
File outputFile = new File("path/to/output/file.dcm");
fos = new FileOutputStream(outputFile);
fos.write(dis.readBytes());
} finally {
SafeClose.close(fos);
SafeClose.close(dis);
}
}
}
}
// 断开连接
device.stopListening();
device.unbindConnections();
}
}
```
这个示例演示了如何使用dcm4che库从dcm4chee服务检索Dicom文件。首先,使用dcm4che库连接dcm4chee服务。然后,使用dcm4chee服务的QueryRetrieveService查询Dicom文件,获取Dicom文件并保存它们。最后,断开连接。
请将`dcm4chee-server`替换为dcm4chee服务的主机名或IP地址,并将`path/to/output/file.dcm`替换为要保存的文件路径。
阅读全文