Jpg2Dcm.java
4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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;
}
}