如何使用dcm4che-3.2.1将JPG图像转换为DICOM文件?
我可以设置属性并创建dicom文件,但是不能将图像写入dicom文件吗?
回答:
我已经尝试过使用拥有的图像并且它可以工作,但是我希望它不适用于RGB图像。像这样的东西
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();
我假设您的图片在这里有一个无符号的短数据缓冲区。
DataBufferUShort buff = (DataBufferUShort) jpg.getData().getDataBuffer();
要处理其他数据缓冲区,应检查type,相应地进行强制转换,然后转换为字节数组。对于字节缓冲区,应该很容易
DataBufferByte buff = (DataBufferByte) jpg.getData().getDataBuffer();
然后
buff.getData(numOfBank)
numOfBank
您的图片在哪里0
应该返回一个字节数组
以上是 如何使用dcm4che-3.2.1将JPG图像转换为DICOM文件? 的全部内容, 来源链接: utcz.com/qa/424179.html