I can set the attributes and create the dicom file, but I can not write the image to the dicom file?
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