Thank you for getting back to me so promptly and by the way congratulation for all your effort and excellent work in making these libraries.
I'm using a ready-made video file from android or tablet (.mp4 files), just convert therm to byte streams and encapsulate them in pixeldata. Here's the full code - 2 functions.
To be honest I'm not 100% sure that this transfer syntax corresponds to the file format of the produced MP4 files but I had to start from some place.
public static string dicomizeVideo(string videoFile, Context ctx)
{
//videoFile = sessionData._dir.ToString() + "/hale.mpg";
string dcmOutput = sessionData._dir.CanonicalPath + "/DicomVideoFile_"+ DateTime.Now.ToString("dd.MM.yyyy-HH.mm.ss")+".dcm";
DateTime currentDate = DateTime.Now;
string accessionNumber = currentDate.Millisecond.ToString();
string patientName = @"Video^Test";
string patientID = accessionNumber + "9821";
string patientSex = "F";
DateTime patientDOB = new DateTime(1990, 1, 15);
DicomUID studyInstanceUID = DicomUID.AbdominalArteriesLateral12111;
DicomUID seriesInstanceUID = DicomUID.GeneralRiskFactors6087;
StringBuilder uid = new StringBuilder();
uid.Append("1.08.1982.10121984.2.0.07").Append(".z").Append(DateTime.UtcNow.Ticks);
var sopUID = new DicomUID(uid.ToString(), "SOP Instance UID", DicomUidType.SOPInstance);
//construct dataset
DicomDataset dataset = new DicomDataset();
dataset.Add(DicomTag.SOPClassUID, DicomUID.MultiFrameTrueColorSecondaryCaptureImageStorage);
dataset.Add(DicomTag.MediaStorageSOPInstanceUID, GenerateUid());
dataset.Add(DicomTag.StudyInstanceUID, GenerateUid());
dataset.Add(DicomTag.SeriesInstanceUID, GenerateUid());
dataset.Add(DicomTag.SOPInstanceUID, GenerateUid());
dataset.Add(DicomTag.ImageType, "ORIGINAL\\PRIMARY");
dataset.Add(DicomTag.StudyDate, currentDate);
dataset.Add(DicomTag.SeriesDate, currentDate);
dataset.Add(DicomTag.StudyTime, currentDate.ToString());
dataset.Add(DicomTag.AccessionNumber, accessionNumber);
dataset.Add(DicomTag.Modality, "OT");
//dataset.Add(DicomTag.ReferringPhysicianName, referringPhysician);
dataset.Add(DicomTag.PatientName, patientName);
dataset.Add(DicomTag.PatientSex, patientSex);
dataset.Add(DicomTag.PatientBirthDate, patientDOB);
dataset.Add(DicomTag.PatientID, patientID);
dataset.Add(DicomTag.FrameTime, "33.000000");
dataset.Add(DicomTag.StudyID, uid.ToString());
dataset.Add(DicomTag.SeriesNumber, "1238712");
dataset.Add(DicomTag.SeriesDescription, "MG DICOM Description");
dataset.Add(DicomTag.InstanceNumber, "2");
dataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.YbrPartial420.Value);
//dataset.Add(DicomTag.PixelAspectRatio, "4\\3");
dataset.Add(DicomTag.PixelRepresentation, (ushort)0);
dataset.Add(DicomTag.LossyImageCompression, "01");
dataset.Add(DicomTag.SpecificCharacterSet, "ISO_IR 100");
dataset.Add(DicomTag.ConversionType, "SI");
dataset.Add(DicomTag.Manufacturer, "GlobalMed");
dataset.Add(DicomTag.BitsAllocated, "8");
//dataset.Add(DicomTag.TransferSyntaxUID, DicomUID.ImplicitVRLittleEndian);
//dataset.Add(DicomTag.PixelData, DicomVR.OB);
//--if they exist...
insertUserTags(dataset);
//---get video metadata
MediaMetadataRetriever mdr = new MediaMetadataRetriever();
mdr.SetDataSource(videoFile);
string vh = mdr.ExtractMetadata(MetadataKey.VideoHeight);
string vw = mdr.ExtractMetadata(MetadataKey.VideoWidth);
//string frameRate = mdr.ExtractMetadata(MetadataKey.CaptureFramerate);
string frameRate = sessionData.videoConfig.frameRate;
string duration = mdr.ExtractMetadata(MetadataKey.Duration);
if (frameRate == null) frameRate = "30";
int frameCount = Convert.ToInt32(frameRate) * Convert.ToInt32(duration)/1000;
//----add pixeldata info
DicomPixelData pixelData = DicomPixelData.Create(dataset, true);
pixelData.Width = Convert.ToUInt16(vw);
pixelData.Height = Convert.ToUInt16(vh);
pixelData.NumberOfFrames = frameCount;
pixelData.HighBit = 7;
pixelData.BitsStored = 8;
//pixelData.BitsAllocated = 8;
pixelData.SamplesPerPixel = 3;
pixelData.PlanarConfiguration = 0;
pixelData.PhotometricInterpretation = PhotometricInterpretation.YbrPartial420;
//---add to tags too (is it necessary??)
dataset.AddOrUpdate(DicomTag.NumberOfFrames, frameCount);
dataset.AddOrUpdate(DicomTag.Columns, pixelData.Width);
dataset.AddOrUpdate(DicomTag.Rows, pixelData.Height);
//----add video bytes
byte[] videoBytes = getVideoBytes(videoFile);
MemoryByteBuffer buffer = new MemoryByteBuffer(videoBytes);
pixelData.AddFrame(buffer);
DicomFile dicomfile = new DicomFile(dataset);
dicomfile.FileMetaInfo.TransferSyntax = DicomTransferSyntax.Lookup(DicomUID.MPEG2MainProfileHighLevel);
dicomfile.Save(dcmOutput);
Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); //---update file system
return dcmOutput;
}
private static byte[] getVideoBytes(string videoFile)
{
FileInputStream fis = new FileInputStream(videoFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.Read(buf)))
baos.Write(buf, 0, n);
byte[] videoBytes = baos.ToByteArray();
return videoBytes;
}
In order to send them, I used the SCP example you have on the first page of Git, I have just added some event handling to find out when the transmission ends and if it was successful. Btw, is there any way in 3.0.2 to disable the auto-resend if PACS server closes the connection because it causes endless transmissions?