如何使用dcm4che3获取dicom的缩略图
时间: 2023-04-01 09:02:47 浏览: 360
您可以使用dcm4che3提供的ThumbnailExtractor类来获取DICOM图像的缩略图。您可以使用以下代码:
```java
import org.dcm4che3.image.ThumbnailExtractor;
import org.dcm4che3.io.DicomInputStream;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
public class DicomThumbnail {
public static void main(String[] args) throws IOException {
File dicomFile = new File("path/to/dicom/file.dcm");
DicomInputStream dis = new DicomInputStream(dicomFile);
ThumbnailExtractor te = new ThumbnailExtractor(dis);
BufferedImage thumbnail = te.getThumbnail();
// do something with the thumbnail image
}
}
```
请注意,您需要在类路径中包含dcm4che3库以使用此代码。
相关问题
dcm4che3生成dicom缩略图的代码
你可以使用以下代码生成 DICOM 缩略图:
```java
import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
public class DicomThumbnailGenerator {
public static void main(String[] args) throws IOException {
File dicomFile = new File("path/to/dicom/file.dcm");
File thumbnailFile = new File("path/to/thumbnail/file.jpg");
ImageInputStream iis = ImageIO.createImageInputStream(dicomFile);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (!readers.hasNext()) {
throw new RuntimeException("No DICOM ImageReader found!");
}
ImageReader reader = readers.next();
reader.setInput(iis);
DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();
BufferedImage image = reader.read(, param);
ImageIO.write(image, "jpg", thumbnailFile);
}
}
```
这段代码使用 dcm4che3 库中的 `DicomImageReadParam` 类来读取 DICOM 文件,并将其转换为缩略图。注意,需要将 `path/to/dicom/file.dcm` 和 `path/to/thumbnail/file.jpg` 替换为实际的文件路径。
给出java代码使用dcm4che库获取dcm4chee服务的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`替换为要保存的文件路径。
阅读全文