Jpg2Dcm.java 4.71 KB
package com.sw.laryngoscope.dicom;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;

import com.sw.laryngoscope.utils.Logger;

import org.dcm4che3.data.Attributes;
import org.dcm4che3.data.Tag;
import org.dcm4che3.data.UID;
import org.dcm4che3.data.VR;
import org.dcm4che3.io.DicomOutputStream;

import java.io.File;
import java.util.List;


public class Jpg2Dcm {

    public static void saveDicom(String filePath, List<String> picPath,
                                 String name, String sex, String id,
                                 String age, String studyDate) {
        try {
            Attributes attrs = new Attributes();

            String ts = UID.ExplicitVRLittleEndian;
            Attributes fmi =
                    Attributes.createFileMetaInformation("1.2.840.113619.2.55.3.2831180801.104.1384184038.748.1059",
                            "1.2.840.10008.5.1.4.1.1.2", ts);

            Bitmap bmpPic = BitmapFactory.decodeFile(picPath.get(0), null);

            //患者姓名
            attrs.setString(Tag.PatientName, VR.AE, name);
            //性别
            attrs.setString(Tag.PatientSex, VR.CS, sex);//"M"
            //患者ID
            attrs.setString(Tag.PatientID, VR.CS, id);
            //年龄
            attrs.setString(Tag.PatientAge, VR.AS, age + "Y");//99Y


            attrs.setString(Tag.SOPClassUID, VR.UI, "1.2.840.10008.5.1.4.1.1.2");
            attrs.setString(Tag.SOPInstanceUID, VR.UI, "1.2.840.113619.2.55.3.2831180801.104.1384184038.748.1059");
            //检查日期:检查开始的日期
            attrs.setString(Tag.StudyDate, VR.AS, studyDate);//"20140126"
            attrs.setString(Tag.SeriesDate, VR.AS, studyDate);


            attrs.setString(Tag.SpecificCharacterSet, VR.CS, "GB18030");

            //Modality 表示检查模态,有MRI,CT,CR,DR等
            attrs.setString(Tag.Modality, VR.CS, "ES");//ES DR
            //表示制造商
            attrs.setString(Tag.Manufacturer, VR.CS, "HugeMed");


            attrs.setInt(Tag.Columns, VR.US, bmpPic.getWidth());
            attrs.setInt(Tag.Rows, VR.US, bmpPic.getHeight());
            //每个像素占多少机器字,24位图samplesperpixel应该等于3
            attrs.setInt(Tag.SamplesPerPixel, VR.IS, 3);
            attrs.setString(Tag.PhotometricInterpretation, VR.CS, "RGB");
            attrs.setInt(Tag.BitsAllocated, VR.IS, 8);
            attrs.setInt(Tag.BitsStored, VR.IS, 8);
            attrs.setInt(Tag.NumberOfFrames, VR.IS, picPath.size());//2图片的个数

            attrs.setString(Tag.StudyInstanceUID, VR.UI, "1.2.840.113619.2.55.3.2831180801.104.1384184038.36");
            attrs.setString(Tag.SeriesInstanceUID, VR.UI, "1.2.840.113619.2.55.3.2831180801.104.1384184038.46");
            attrs.setInt(Tag.SeriesNumber, VR.IS, 2);
            //辨识图像的号码
            attrs.setInt(Tag.InstanceNumber, VR.US, 1);

            byte[] img_buf_0 = getPixels(bmpPic);

            byte[] buff = new byte[picPath.size() * img_buf_0.length];
            int i = 0;
            for ( String tmpPath:picPath ) {
                if ( i == 0 ) {
                    System.arraycopy(img_buf_0, 0, buff, i * img_buf_0.length, img_buf_0.length);
                } else {
                    Bitmap tmpPic = BitmapFactory.decodeFile(tmpPath, null);
                    byte[] buffImg = getPixels(tmpPic);
                    System.arraycopy(buffImg, 0, buff, i * buffImg.length, buffImg.length);
                }
                i++;
            }

            attrs.setBytes(Tag.PixelData, VR.OB, buff);
            attrs.trimToSize();

            File fi = new File(filePath);

            Logger.d(" getCanonicalPath " + fi.getCanonicalPath());

            DicomOutputStream dos = new DicomOutputStream(fi);
            dos.writeDataset(fmi, attrs);
            dos.finish();
            dos.close();

        } catch (Exception e) {
        }
    }

    public static byte[] getPixels(Bitmap bitmap) {
        byte[] bytes = new byte[bitmap.getWidth() * bitmap.getHeight() * 3];
        int wide = bitmap.getWidth();
        int i = 0;
        int height = bitmap.getHeight();
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < wide; x++) {
                int srcColor = bitmap.getPixel(x, y);
                bytes[i] = (byte) (Color.red(srcColor) & 0x000000ff);
                i++;
                bytes[i] = (byte) (Color.green(srcColor) & 0x000000ff);
                i++;
                bytes[i] = (byte) (Color.blue(srcColor) & 0x000000ff);
                i++;
            }
        }
        return bytes;
    }

}