วิธีการแปลงไฟล์ DICOM ไปยัง JSON โดยใช้ DicomJsonSerializer
การสอนนี้แสดงให้เห็นว่าวิธีการแปลงไฟล์ DICOM ไปยังรูปแบบ JSON โดยใช้ C# ผลลัพธ์ JSon เป็นสิ่งสําคัญสําหรับการรวมข้อมูลโลหะ DIKOM กับบริการเว็บที่ทันสมัย REST API และระบบข้อมูลการดูแลสุขภาพ
ประโยชน์ของการแปลง DICOM ไปยัง JSON
การบูรณาการเว็บ:- JSON เป็นรูปแบบมาตรฐานสําหรับ REST API และบริการเว็บ
การวิเคราะห์ข้อมูล:- ง่ายต่อการประมวลผล DICOM metadata ในท่อวิเคราะห์ข้อมูล
**การทํางานร่วมกัน *:- แบ่งปันข้อมูล metadata กับระบบที่ไม่สนับสนุนรูปแบบ DICOM ที่เกิด
ข้อกําหนด: การเตรียมสิ่งแวดล้อม
- ติดตั้ง Visual Studio หรือ IDE .NET ที่เข้ากันได้ใด ๆ.
- สร้างโครงการการใช้งาน .NET 8 ใหม่
- ติดตั้ง Aspose.Medical จาก NuGet Package Manager
คู่มือขั้นตอนเพื่อแปลง DICOM ไปยัง JSON
ขั้นตอน 1: ติดตั้ง Aspose.Medical
เพิ่มห้องสมุด Aspose.Medical ไปยังโครงการของคุณโดยใช้ NuGet
Install-Package Aspose.Medicalขั้นตอนที่ 2: รวมพื้นที่ชื่อที่จําเป็น
เพิ่มอ้างอิงไปยังพื้นที่ชื่อที่ต้องการในรหัสของคุณ.
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;ขั้นตอนที่ 3: ดาวน์โหลดไฟล์ DICOM
ดาวน์โหลดไฟล์ DICOM ที่คุณต้องการแปลง
DicomFile dcm = DicomFile.Open("patient_scan.dcm");ขั้นตอน 4: Serialize ไปยัง JSON
ใช DicomJsonSerializer.Serialize วิธีการแปลงไฟล์ DICOM ไปยัง JSON
string json = DicomJsonSerializer.Serialize(dcm);ขั้นตอน 5: การบันทึกหรือใช้ JSON Output
เก็บ JSON ไปยังไฟล์หรือใช้มันโดยตรงในแอพของคุณ
// Save to file
File.WriteAllText("patient_scan.json", json);
// Or use directly
Console.WriteLine(json);ตัวอย่างรหัสสมบูรณ์เพื่อแปลง DICOM ไปยัง JSON
นี่คือตัวอย่างสมบูรณ์ที่แสดงให้เห็นว่าวิธีการแปลงไฟล์ DICOM ไปยัง JSON:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
// Load the DICOM file
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Convert to JSON
string json = DicomJsonSerializer.Serialize(dcm);
// Save to file
File.WriteAllText("patient_scan.json", json);
Console.WriteLine("DICOM file converted to JSON successfully!");
Console.WriteLine($"Output saved to: patient_scan.json");การพิมพ์ JSON ที่ดีที่สุด
สําหรับ JSON ที่สามารถอ่านได้โดยมนุษย์ด้วย indentation ใช้ writeIndented พารามิเตอร์ :
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Serialize with indentation for readability
string prettyJson = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
File.WriteAllText("patient_scan_pretty.json", prettyJson);
Console.WriteLine("Pretty-printed JSON saved successfully!");การแปลง Dataset แทน DicomFile
นอกจากนี้คุณยังสามารถแปลงส่วน Datasetเท่านั้น:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Serialize only the dataset (without file meta information)
string datasetJson = DicomJsonSerializer.Serialize(dcm.Dataset);
File.WriteAllText("patient_dataset.json", datasetJson);ตัวอย่างโครงสร้างการส่งออก JSON
JSON ผลผลิตตามมาตรฐาน DICOM PS3.18 นี่คือตัวอย่างของสิ่งที่ผลผลิตดูเหมือน:
{
"00080005": {
"vr": "CS",
"Value": ["ISO_IR 100"]
},
"00080020": {
"vr": "DA",
"Value": ["20240115"]
},
"00080030": {
"vr": "TM",
"Value": ["143022"]
},
"00100010": {
"vr": "PN",
"Value": [
{
"Alphabetic": "DOE^JOHN"
}
]
},
"00100020": {
"vr": "LO",
"Value": ["12345"]
}
}การจัดเรียงตาม Stream
สําหรับไฟล์ขนาดใหญ่หรือแอพเว็บใช้การจัดเรียงตาม Stream:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("large_scan.dcm");
// Write directly to a file stream
using (FileStream fs = File.Create("large_scan.json"))
{
DicomJsonSerializer.Serialize(fs, dcm.Dataset);
}
Console.WriteLine("Large DICOM file serialized to JSON stream!");ตัวอย่างการบูรณาการ: ส่ง JSON ไปยัง Web API
นี่คือวิธีการรวมการแปลง DICOM ไปยัง JSON กับลูกค้า HTTP:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
using System.Net.Http;
using System.Text;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm);
// Send to web API
using HttpClient client = new();
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(
"https://api.example.com/dicom/metadata",
content
);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("DICOM metadata successfully sent to API!");
}Tips การใช้งาน
Debugging DICOM Metadata
ใช้การแปลง JSON เพื่อตรวจสอบค่าแท็ก DICOM:
DicomFile dcm = DicomFile.Open("unknown_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
Console.WriteLine(json);ส่งไปยังเว็บไซต์ Front-End
JSON เหมาะสําหรับการแสดงข้อมูลโลหะ DICOM ในผู้ชมเบราว์เซอร์:
// In ASP.NET Core controller
[HttpGet("dicom/{id}/metadata")]
public IActionResult GetMetadata(string id)
{
DicomFile dcm = DicomFile.Open($"storage/{id}.dcm");
string json = DicomJsonSerializer.Serialize(dcm);
return Content(json, "application/json");
}สิ่งที่รวมอยู่ใน JSON Output
ผล JSON รวมถึง:
- ทั้งหมด DICOM แท็กที่มีค่าของพวกเขา
- การแสดงค่า (VR) สําหรับแต่ละแท็ก
- รายการ sequence ในฐานะที่เป็นวัตถุ JSON
- รายละเอียดข้อมูลไบนารี (BulkData) สําหรับข้อมูล pixel
ข้อมูลเพิ่มเติม
- รูปแบบ JSON ตามข้อกําหนดของ DICOM PS3.18 Web Services
- หลักฐานไบนารีขนาดใหญ่ (เช่นข้อมูลพิกเซล) โดยปกติจะถูกนํามาใช้แทนที่จะรวมไว้
- โปรดพิจารณาการใช้ตัวเลือกการจัดเรียงแบบกําหนดเองสําหรับข้อกําหนดการบูรณาการเฉพาะ
ข้อสรุป
การสอนนี้ได้แสดงให้เห็นว่าวิธีการแปลงไฟล์ DICOM ไปยังรูปแบบ JSON ใน C# โดยใช้ Aspose.Medical ผลลัพธ์ Json ช่วยให้การรวมกันอย่างต่อเนื่องกับ APIs สุขภาพทันสมัยผู้ชมเว็บและระบบวิเคราะห์ข้อมูล