下圖顯示了DICOM3.0標(biāo)準(zhǔn)的通用通信模型,該模型跨越了 網(wǎng)絡(luò)(在線)和媒體存儲(chǔ)交換(離線)通信。應(yīng)用程序可利用以下任一傳輸機(jī)制:
DICOM的通用通信模型旨在為醫(yī)療圖像和相關(guān)數(shù)據(jù)的傳輸和存儲(chǔ)提供靈活和多樣化的解決方案。DICOM的通用通信模型不僅僅是簡單地傳輸和存儲(chǔ)醫(yī)療圖像和相關(guān)數(shù)據(jù),而是提供了一種多層次、多種方式的靈活解決方案,以滿足不同場景下的需求和要求。這種多樣化的傳輸和存儲(chǔ)機(jī)制使得DICOM成為醫(yī)療行業(yè)中不可或缺的通信標(biāo)準(zhǔn),為醫(yī)療圖像和相關(guān)數(shù)據(jù)的交換和共享提供了可靠和高效的技術(shù)支持。
DICOM的通用通信模型的重要性和價(jià)值在于其能夠滿足醫(yī)療行業(yè)不同方面的需求,為醫(yī)療圖像和相關(guān)數(shù)據(jù)的傳輸和存儲(chǔ)提供了全面而可靠的解決方案,從而推動(dòng)了醫(yī)療信息技術(shù)的發(fā)展和應(yīng)用。
`fo-dicom` 使用了Socket和TcpClient等底層網(wǎng)絡(luò)通信類來與DICOM服務(wù)器進(jìn)行連接和通信,從而實(shí)現(xiàn) DICOM 的網(wǎng)絡(luò)通信功能。下面是 `fo-dicom` 網(wǎng)絡(luò)通信的實(shí)現(xiàn)基本原理:
`fo-dicom` 通過使用 .NET 平臺(tái)的網(wǎng)絡(luò)通信庫來實(shí)現(xiàn)底層的網(wǎng)絡(luò)傳輸,并且遵循 DICOM 標(biāo)準(zhǔn)的數(shù)據(jù)格式和編碼規(guī)則。它提供了一組簡潔而強(qiáng)大的 API,使得用戶可以方便地進(jìn)行 DICOM 數(shù)據(jù)的傳輸和處理。
以下是一個(gè)使用 `fo-dicom` 進(jìn)行C-STORE命令的網(wǎng)絡(luò)通信的簡單案例:
案例來源于官網(wǎng)的示例:https://github.com/fo-dicom/fo-dicom-samples
假設(shè)我們有一個(gè)服務(wù)器端應(yīng)用程序,它監(jiān)聽在本地的端口號(hào) 11112上,等待客戶端的連接請(qǐng)求。一旦接收到來自客戶端的 C-STORE 請(qǐng)求,服務(wù)器將把接收到的 DICOM 圖像數(shù)據(jù)保存到本地磁盤。
using System;using System.IO;using System.Text;using System.Threading.Tasks;using FellowOakDicom.Network;using Microsoft.Extensions.Logging;namespace Samples{ internal class Program { private const string _storagePath = @"./DICOM"; private static void Main(string[] args) { // start DICOM server on port from command line argument or 11112 var port = args != null && args.Length > 0 && int.TryParse(args[0], out int tmp) ? tmp : 11112; Console.WriteLine($"Starting C-Store SCP server on port {port}"); using (var server = DicomServerFactory.Create<CStoreSCP>(port)) { // end process Console.WriteLine("Press <return> to end..."); Console.ReadLine(); } } private class CStoreSCP : DicomService, IDicomServiceProvider, IDicomCStoreProvider, IDicomCEchoProvider { private static readonly DicomTransferSyntax[] _acceptedTransferSyntaxes = new DicomTransferSyntax[] { DicomTransferSyntax.ExplicitVRLittleEndian, DicomTransferSyntax.ExplicitVRBigEndian, DicomTransferSyntax.ImplicitVRLittleEndian }; private static readonly DicomTransferSyntax[] _acceptedImageTransferSyntaxes = new DicomTransferSyntax[] { // Lossless DicomTransferSyntax.JPEGLSLossless, DicomTransferSyntax.JPEG2000Lossless, DicomTransferSyntax.JPEGProcess14SV1, DicomTransferSyntax.JPEGProcess14, DicomTransferSyntax.RLELossless, // Lossy DicomTransferSyntax.JPEGLSNearLossless, DicomTransferSyntax.JPEG2000Lossy, DicomTransferSyntax.JPEGProcess1, DicomTransferSyntax.JPEGProcess2_4, // Uncompressed DicomTransferSyntax.ExplicitVRLittleEndian, DicomTransferSyntax.ExplicitVRBigEndian, DicomTransferSyntax.ImplicitVRLittleEndian }; public CStoreSCP(INetworkStream stream, Encoding fallbackEncoding, ILogger log, DicomServiceDependencies dependencies) : base(stream, fallbackEncoding, log, dependencies) { } public Task OnReceiveAssociationRequestAsync(DicomAssociation association) { if (association.CalledAE != "STORESCP") { return SendAssociationRejectAsync( DicomRejectResult.Permanent, DicomRejectSource.ServiceUser, DicomRejectReason.CalledAENotRecognized); } foreach (var pc in association.PresentationContexts) { if (pc.AbstractSyntax == DicomUID.Verification) { pc.AcceptTransferSyntaxes(_acceptedTransferSyntaxes); } else if (pc.AbstractSyntax.StorageCategory != DicomStorageCategory.None) { pc.AcceptTransferSyntaxes(_acceptedImageTransferSyntaxes); } } return SendAssociationAcceptAsync(association); } public Task OnReceiveAssociationReleaseRequestAsync() { return SendAssociationReleaseResponseAsync(); } public void OnReceiveAbort(DicomAbortSource source, DicomAbortReason reason) { /* nothing to do here */ } public void OnConnectionClosed(Exception exception) { /* nothing to do here */ } public async Task<DicomCStoreResponse> OnCStoreRequestAsync(DicomCStoreRequest request) { var studyUid = request.Dataset.GetSingleValue<string>(DicomTag.StudyInstanceUID).Trim(); var instUid = request.SOPInstanceUID.UID; var path = Path.GetFullPath(Program._storagePath); path = Path.Combine(path, studyUid); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, instUid) + ".dcm"; await request.File.SaveAsync(path); return new DicomCStoreResponse(request, DicomStatus.Success); } public Task OnCStoreRequestExceptionAsync(string tempFileName, Exception e) { // let library handle logging and error response return Task.CompletedTask; } public Task<DicomCEchoResponse> OnCEchoRequestAsync(DicomCEchoRequest request) { return Task.FromResult(new DicomCEchoResponse(request, DicomStatus.Success)); } } }}
在上面的代碼中,首先從命令行參數(shù)中獲取端口號(hào),然后創(chuàng)建一個(gè) CStoreSCP 對(duì)象作為 DICOM 服務(wù)器,并將其綁定到指定的端口。在 CStoreSCP 類中,實(shí)現(xiàn)了 IDicomServiceProvider、IDicomCStoreProvider 和 IDicomCEchoProvider 接口,分別處理 DICOM 關(guān)聯(lián)請(qǐng)求、C-Store 請(qǐng)求和 C-Echo 請(qǐng)求。其中,
OnReceiveAssociationRequestAsync() 方法會(huì)檢查 Called AE 是否為 STORESCP,如果不是則拒絕關(guān)聯(lián)請(qǐng)求。OnCStoreRequestAsync() 方法則會(huì)將接收到的 DICOM 數(shù)據(jù)保存到本地文件系統(tǒng)中。其他的方法實(shí)現(xiàn)通常為空實(shí)現(xiàn),因?yàn)椴⒉恍枰獙?duì)其進(jìn)行特殊處理。
對(duì)于客戶端應(yīng)用程序,我們可以使用 `DicomClient` 類來發(fā)送 C-STORE 請(qǐng)求到服務(wù)器。以下是一個(gè)簡單的客戶端示例:
using System;using System.IO;using System.Net;using System.Net.Sockets;using System.Threading.Tasks;using FellowOakDicom.Network;using FellowOakDicom.Network.Client;namespace Samples{ internal static class Program { private static string _storeServerHost = "127.0.0.1"; private static int _storeServerPort = 11112; private const string _storeServerAET = "STORESCP"; private const string _aet = "FODICOMSCU"; static async Task Main(string[] args) { var storeMore = ""; _storeServerHost = GetServerHost(); _storeServerPort = GetServerPort(); Console.WriteLine("***************************************************"); Console.WriteLine("Server AE Title: " + _storeServerAET); Console.WriteLine("Server Host Address: " + _storeServerHost); Console.WriteLine("Server Port: " + _storeServerPort); Console.WriteLine("Client AE Title: " + _aet); Console.WriteLine("***************************************************"); var client = DicomClientFactory.Create(_storeServerHost, _storeServerPort, false, _aet, _storeServerAET); client.NegotiateAsyncOps(); do { try { Console.WriteLine(); Console.WriteLine("Enter the path for a DICOM file:"); Console.Write(">>>"); string dicomFile = Console.ReadLine(); while (!File.Exists(dicomFile)) { Console.WriteLine("Invalid file path, enter the path for a DICOM file or press Enter to Exit:"); dicomFile = Console.ReadLine(); if (string.IsNullOrWhiteSpace(dicomFile)) { return; } } var request = new DicomCStoreRequest(dicomFile); request.OnResponseReceived += (req, response) => Console.WriteLine("C-Store Response Received, Status: " + response.Status); await client.AddRequestAsync(request); await client.SendAsync(); } catch (Exception exception) { Console.WriteLine(); Console.WriteLine("----------------------------------------------------"); Console.WriteLine("Error storing file. Exception Details:"); Console.WriteLine(exception.ToString()); Console.WriteLine("----------------------------------------------------"); Console.WriteLine(); } Console.WriteLine("To store another file, enter /"y/"; Othersie, press enter to exit: "); Console.Write(">>>"); storeMore = Console.ReadLine().Trim(); } while (storeMore.Length > 0 && storeMore.ToLower()[0] == 'y'); } private static string GetServerHost() { var hostAddress = ""; var localIP = GetLocalIPAddress(); do { Console.WriteLine("Your local IP is: " + localIP); Console.WriteLine("Enter /"1/" to use your local IP Address: " + localIP); Console.WriteLine("Enter /"2/" to use defult: " + _storeServerHost); Console.WriteLine("Enter /"3/" to enter custom"); Console.Write(">>>"); string input = Console.ReadLine().Trim().ToLower(); if (input.Length > 0) { if (input[0] == '1') { hostAddress = localIP; } else if (input[0] == '2') { hostAddress = _storeServerHost; } else if (input[0] == '3') { Console.WriteLine("Enter Server Host Address:"); Console.Write(">>>"); hostAddress = Console.ReadLine(); } } } while (hostAddress.Length == 0); return hostAddress; } private static int GetServerPort() { Console.WriteLine("Enter Server port, or /"Enter/" for default /"" + _storeServerPort + "/":"); Console.Write(">>>"); var input = Console.ReadLine().Trim(); return string.IsNullOrEmpty(input) ? _storeServerPort : int.Parse(input); } public static string GetLocalIPAddress() { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { return ip.ToString(); } } return ""; } }}
首先定義了一些變量,包括存儲(chǔ)服務(wù)器的主機(jī)地址、端口號(hào),以及客戶端和服務(wù)器的 AE(Application Entity)標(biāo)題。然后,在 Main 方法中創(chuàng)建了一個(gè) DicomClient 對(duì)象,并通過調(diào)用 NegotiateAsyncOps 方法進(jìn)行異步操作的協(xié)商。接下來,進(jìn)入一個(gè)循環(huán),用戶可以輸入要發(fā)送的 DICOM 文件的路徑。程序會(huì)檢查路徑是否有效,如果無效則提示用戶重新輸入,直到輸入為空或用戶選擇退出。然后,創(chuàng)建一個(gè) DicomCStoreRequest 對(duì)象,傳入要發(fā)送的 DICOM 文件路徑作為參數(shù)。并通過訂閱 OnResponseReceived 事件來處理響應(yīng)。最后,調(diào)用 AddRequestAsync 方法將請(qǐng)求添加到客戶端的請(qǐng)求隊(duì)列中,并調(diào)用 SendAsync 方法發(fā)送請(qǐng)求。
其中GetServerHost 方法用于獲取服務(wù)器主機(jī)地址,它會(huì)提示用戶選擇使用本地 IP 地址、默認(rèn)地址還是自定義地址。GetServerPort 方法用于獲取服務(wù)器端口號(hào),用戶可以輸入自定義端口號(hào),或者直接回車使用默認(rèn)端口號(hào)。GetLocalIPAddress 方法用于獲取本地 IP 地址。
這個(gè)案例展示了一個(gè)簡單的基于 `fo-dicom` 的 DICOM 網(wǎng)絡(luò)通信示例,即SCU和SCP對(duì)CStore的通信的簡單處理,服務(wù)器接收到客戶端發(fā)送的 C-STORE 請(qǐng)求并保存圖像到本地磁盤。
本文鏈接:http://www.www897cc.com/showinfo-26-87977-0.htmlFo-dicom是如何實(shí)現(xiàn)DICOM 的網(wǎng)絡(luò)通信功能
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com