在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圖像。你可以根據自己的需求對這些示例進行修改和擴展。