如何使用Dcm4che3将多个jpg图像写入Dicom文件?

时间:2014-01-21 10:43:33

标签: java dicom

在之前的问题中我曾经问过一段时间,我可以给Dicom写一个图像,但是现在我需要在Dicon文件中编写两个或更多jpg图像作为序列。使用dcm4che3.Please任何人都可以帮忙吗?

public static void main(String[] args) throws Exception {
    Attributes attrs = new Attributes();
    String ts = UID.JPEG2000;
    Attributes fmi = Attributes.createFileMetaInformation("1.2.3", UID.MRImageStorage , ts);
    File f = new File("/tmp/taylor.jpg");
    BufferedImage vf = ImageIO.read(f);
    attrs.setString(Tag.PatientName, VR.AE, "Test");
    attrs.setString(Tag.PatientSex, VR.CS, "M");
    attrs.setString(Tag.PatientID, VR.CS, "10");
    attrs.setString(Tag.PatientBirthDate, VR.AS, "19861010");
    attrs.setString(Tag.StudyDate, VR.AS, "20140126");
    attrs.setString(Tag.SeriesDate, VR.AS, "20140126");
    attrs.setString(Tag.StudyTime, VR.AS, "101010");
    attrs.setString(Tag.SeriesTime, VR.AS, "101010");
    attrs.setString(Tag.StudyDescription, VR.AS, "Test #1");
    attrs.setString(Tag.SeriesDescription, VR.AS, "Test #2");
    attrs.setString(Tag.Modality, VR.CS, "MR");
    attrs.setInt(Tag.Columns, VR.US, vf.getWidth());
    attrs.setInt(Tag.Rows, VR.US, vf.getHeight());
    attrs.setInt(Tag.InstanceNumber, VR.US, 1);
    attrs.setInt(Tag.SamplesPerPixel, VR.IS, 3);
    attrs.setString(Tag.PhotometricInterpretation, VR.CS, "MONOCHROME2");
    attrs.setInt(Tag.BitsAllocated, VR.IS, 8);
    attrs.setInt(Tag.BitsStored, VR.IS, 8);
    attrs.setInt(Tag.NumberOfFrames, VR.IS, 5);
    attrs.setInt(Tag.SeriesNumber, VR.IS, 2);


    DataBufferByte buff = (DataBufferByte) vf.getData().getDataBuffer();
    Fragments fr = attrs.newFragments(Tag.PixelData, VR.OW, 1);
    for (int i = 0; i < 5; i++) {
        fr.add(buff.getData(0));
    }
    attrs.trimToSize();
    File fi = new File("/tmp/test.dcm");
    System.out.println(fi.getCanonicalPath());
    System.out.println(fmi);
    System.out.println(attrs);
    DicomOutputStream dos = new DicomOutputStream(fi);
    dos.writeDataset(fmi, attrs);
    dos.finish();
    dos.close();
}

此代码是创建dcm文件但我无法使用Weasis查看器打开它。

1 个答案:

答案 0 :(得分:0)

使用我给你的内容previously,你可以遍历所有jpg图像并将字节连接在一起。将连接的字节设置为像素数据。

然后添加

 attribs.setInt(Tag.NumberOFFrames, VR.**, numberOfImages);

到dicom标题。我不记得这个标签的VR应该是什么,所以检查DICOM标准。

修改1

我会将像素数据设置为

    byte[] buffbytes = buff.getData(0);
    byte[] b = new byte[5*buff.getData(0).length];
    for (int i = 0; i < 5; i++) {
        System.arraycopy(buffbytes, 0, b, i*buffbytes.length, buffbytes.length);
    }

    attrs.setBytes(Tag.PixelData, VR.OW, b);