Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

java - How to convert JPG image to DICOM file using dcm4che-3.2.1?

I can set the attributes and create the dicom file, but I can not write the image to the dicom file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've tried it with an image I have and it works but I expect it won't work for RGB images. Something like this though

    BufferedImage jpg = ImageIO.read(new File("myjpg.jpg"));

    //Convert the image to a byte array 
    DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();
    short[] data = buff.getData();
    ByteBuffer byteBuf = ByteBuffer.allocate(2*data.length);
    int i = 0;
    while (data.length > i) {
        byteBuf.putShort(data[i]);
        i++;
    }

    //Copy a header 
    DicomInputStream dis = new DicomInputStream(new File("fileToCopyheaderFrom.dcm"));
    Attributes meta = dis.readFileMetaInformation();
    Attributes attribs = dis.readDataset(-1, Tag.PixelData);
    dis.close();

    //Change the rows and columns
    attribs.setInt(Tag.Rows, VR.US, jpg.getHeight());
    attribs.setInt(Tag.Columns, VR.US, jpg.getWidth());
    System.out.println(byteBuf.array().length);
    //Attributes attribs = new Attributes();

    //Write the file
    attribs.setBytes(Tag.PixelData, VR.OW, byteBuf.array());
    DicomOutputStream dcmo = new DicomOutputStream(new File("myDicom.dcm"));
    dcmo.writeFileMetaInformation(meta);
    attribs.writeTo(dcmo);
    dcmo.close();

Edit 1

I've assumed your image has an Unsigned short Data Buffer here.

DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();

To handle other data buffers you should check the type , cast acordingly and then convert to a byte array. For a byte Buffer it should be easy

DataBufferByte buff = (DataBufferByte) jpg.getData().getDataBuffer();

Then

buff.getData(numOfBank)

where numOfBank is 0 for you image

should return a byte array


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...