多幅BMP图像数据存入DCM文件 java 實現

背景:

_**仔細研究了老大zssure关于C++的代码及其实现,zssure的博客,前面做过关于一副bmp图像转换成dcm文件,现阶段只能实现长宽大小相同的bmp图像转换。

1.多幅与一副的区别

关于Tag=(0028,0008)   NumberOfFrames  bmp文件的个数。

2.两幅转换

  • ** 先贴代码
public class TwoBmp2Dcm {
    private static byte[] buffer = new byte[54];
    private static byte[] buff = new byte[8192];
    private static int imageSize;
    public static void main(String[] args) throws IOException {
        // TODO 自动生成的方法存根
        Bmp2Dcm2 bmp2Dcm2=new Bmp2Dcm2();
        File file=new File("E://bmp//bmp1.bmp");
        File file2=new File("E://bmp//bmp2.bmp");
        File dcm=new File("E://bmp//first2.dcm");
        DataInputStream dis=new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        DataInputStream dis2=new DataInputStream(new BufferedInputStream(new FileInputStream(file2)));
        DicomOutputStream out =new DicomOutputStream(dcm);
        Attributes attrs=new Attributes();
        Attributes data=new Attributes();
        attrs=bmp2Dcm2.addMetaInformation(attrs);
        data=bmp2Dcm2.addData(data);
        data=bmp2Dcm2.readHeader(data, dis);
        out.writeDataset(attrs, data);
        out.writeHeader(Tag.PixelData, VR.OW, imageSize*2);
         int r;
         while ((r = dis.read(buff)) > 0) {
             out.write(buff, 0, r);
         }
         dis2.read(buffer,0,54); 
         while ((r = dis2.read(buff)) > 0) {
             out.write(buff, 0, r);
         }
       out.close();
       dis.close();
       dis2.close();
    }   
     public  Attributes readHeader(Attributes attrs ,DataInputStream dis) throws IOException{
         dis.read(buffer,0,54); 
         if((buffer[0]&0xff)!=0x42&&(buffer[1]&0xff)!=0x4d){
              throw new IOException("Missing BM segment in Bmp stream");
          }     
          int imageRows=byteToInt(buffer,21);
          int imageColumns=byteToInt(buffer,25);
          imageSize=byteToInt(buffer,5)-54;
          int biBitCount=byteTo2Int(buffer,29); 
          attrs.setInt (Tag.SamplesPerPixel, VR.US,3);
          attrs.setInt(Tag.Rows, VR.US, imageColumns<0?-imageColumns:imageColumns);
          attrs.setInt(Tag.Columns, VR.US, imageRows);
          attrs.setInt(Tag.BitsAllocated, VR.US,biBitCount );//biBitCount
          attrs.setInt(Tag.BitsStored, VR.US,32);
          attrs.setInt(Tag.HighBit, VR.US, 32);
          attrs.setInt(Tag.PixelRepresentation, VR.US, 0); 
          attrs.setString(Tag.PhotometricInterpretation,VR.CS, "MONOCHROME2"); //PALETTE_COLOR MONOCHROME2
          attrs.setString(Tag.RescaleSlope, VR.DS, "1.0");
          attrs.setString(Tag.RescaleIntercept, VR.DS, "0.0");
          attrs.setString(Tag.PixelSpacing,VR.DS, "0.25/0.25");
          attrs.setInt(Tag.NumberOfFrames, VR.IS, 2);
          return attrs;
     }
     public Attributes addData(Attributes attrs){
         attrs.setString(Tag.SpecificCharacterSet, VR.CS, "GB18030");
         attrs.setString(Tag.ImageType, VR.CS, "ORIGINAL/PRIMARY/AXIAL");
         attrs.setString(Tag.InstanceCreationDate, VR.DA, "20130013");
         attrs.setString(Tag.InstanceCreationTime, VR.TM, "175827");
         attrs.setString(Tag.SOPClassUID, VR.UI, "1.2.840.10008.5.1.4.1.1.2");
         attrs.setString(Tag.SOPInstanceUID, VR.UI, "1.3.6.1.4.1.30071.6.22744165.4288391077912169.1.3");
         attrs.setString(Tag.StudyDate, VR.DA, "20130803");
         attrs.setString(Tag.AcquisitionDateTime, VR.DT, "20130803000000");
         attrs.setString(Tag.StudyTime, VR.TM, "175532");
         attrs.setString(Tag.AccessionNumber, VR.SH, "0");
         attrs.setString(Tag.Modality, VR.CS, "CT");
         attrs.setString(Tag.Manufacturer, VR.LO, "LargeV");
         attrs.setString(Tag.StudyDescription, VR.LO, "融合扫描图像");
         attrs.setString(Tag.SeriesDescription, VR.LO, "融合扫描图像");
         attrs.setString(Tag.ManufacturerModelName, VR.LO, "HighRes3D");

         attrs.setString(Tag.PatientName, VR.PN, "振振");
         attrs.setString(Tag.PatientBirthDate,VR.DA,"19920720");
         attrs.setString(Tag.PatientSex, VR.CS, "F");
         return attrs;
     }
     public Attributes addMetaInformation( Attributes attrs){
         attrs.setString(Tag.FileMetaInformationVersion, VR.OB, "");
         attrs.setString(Tag.MediaStorageSOPClassUID, VR.UI, "1.2.840.10008.5.1.4.1.1.2");
         attrs.setString(Tag.MediaStorageSOPInstanceUID, VR.UI, "1.3.6.1.4.1.30071.6.22744165.4288391077912169.1.18");
         attrs.setString(Tag.TransferSyntaxUID, VR.UI, "1.2.840.10008.1.2.1");
         attrs.setString(Tag.ImplementationVersionName, VR.SH, "SmartVDicom1.0");
         attrs.setString(Tag.ImplementationClassUID, VR.UI, "1.3.6.1.4.1.30071.6");
         attrs.setString(Tag.SourceApplicationEntityTitle, VR.AE, "");
         return attrs;
     }
     public int byteToInt(byte[] bi,int end){  
            int a=(((int)bi[end]&0xff)<<24)  ;
            int b=(((int)bi[end-1]&0xff)<<16)  ;
            int c=(((int)bi[end-2]&0xff)<<8)  ;
            int d=(int)bi[end-3]&0xff; 
            return a+b+c+d;
        }  
     public int byteTo2Int(byte[] bi,int end){  

            int c=(((int)bi[end]&0xff)<<8)  ;
            int d=(int)bi[end-1]&0xff; 
            return c+d;
        }  
}

根据单幅转换修改的,自己找差别吧。贴一下效果。

多幅BMP图像数据存入DCM文件 java 實現_第1张图片

你可能感兴趣的:(Dcm,JAVA,bmp)