在Java项目中处理DICOM图像,通常可以使用开源库,如dcm4che2或PixelMed
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-core</artifactId>
<version>5.23.0</version>
</dependency><dependency>
<groupId>org.dcm4che</groupId>
<artifactId>dcm4che-imageio</artifactId>
<version>5.23.0</version>
</dependency>
import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.io.DicomInputStream;
import org.dcm4che3.io.DicomOutputStream;
import java.io.File;
import java.io.IOException;
public class DicomReader {
public static void main(String[] args) {
File dicomFile = new File("path/to/your/dicom/file.dcm");
try (DicomInputStream dis = new DicomInputStream(dicomFile)) {
Attributes attributes = dis.readDataset(-1, -1);
String patientName = attributes.getString(Tag.PatientName, "Unknown");
System.out.println("Patient Name: " + patientName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import org.dcm4che3.imageio.plugins.dcm.DicomImageReadParam;
import org.dcm4che3.io.DicomInputStream;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class DicomToImage {
public static void main(String[] args) {
File dicomFile = new File("path/to/your/dicom/file.dcm");
try (ImageInputStream iis = ImageIO.createImageInputStream(dicomFile)) {
ImageReader reader = ImageIO.getImageReadersByFormatName("DICOM").next();
reader.setInput(iis, false);
ImageReadParam param = new DicomImageReadParam();
BufferedImage image = reader.read(0, param);
// 现在可以对image进行处理,例如缩放、旋转等
} catch (IOException e) {
e.printStackTrace();
}
}
}
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SaveImage {
public static void main(String[] args) {
BufferedImage image = ...; // 从上面的示例中获取处理后的图像
File outputFile = new File("path/to/output/image.png");
try {
ImageIO.write(image, "png", outputFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
这些示例展示了如何在Java项目中使用dcm4che2库处理DICOM图像。你可以根据自己的需求对这些示例进行修改和扩展。