Thursday 5 September 2013

DICOM Tag editing using dcm4che

dcm4che is a collection of open source applications and utilities for the healthcare enterprise. These applications have been developed in the Java programming language for performance and portability, supporting deployment on JDK 1.4 and up.
At the core of the dcm4che project is a robust implementation of the DICOM standard. The dcm4che-1.x DICOM toolkit is used in many production applications across the world, while the current (2.x) version of the toolkit has been re-architected for high performance and flexibility.
Also contained within the dcm4che project is dcm4chee (the extra ‘e’ stands for ‘enterprise’). dcm4chee is an Image Manager/Image Archive (according to IHE). The application contains the DICOM, HL7 services and interfaces that are required to provide storage, retrieval, and workflow to a healthcare environment. dcm4chee is pre-packaged and deployed within the JBoss application server. By taking advantage of many JBoss features (JMS, EJB, Servlet Engine, etc.), and assuming the role of several IHE actors for the sake of interoperability, the application provides many robust and scalable services:
Following jar files are needed for this activity
    dcm4che.jar
    dcm4che-core-2.0.21.jar
    log4j-1.2.13.jar
    slf4j-api-1.5.0.jar
    slf4j-log4j12-1.5.0.jar
    package dicomeditor;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import org.dcm4che2.data.BasicDicomObject;
    import org.dcm4che2.data.DicomObject;
    import org.dcm4che2.data.Tag;
    import org.dcm4che2.data.VR;
    import org.dcm4che2.io.DicomInputStream;
    import org.dcm4che2.io.DicomOutputStream;
    /**
     *
     * @author awaiswaheed
     */
    public class DicomEditor {
        public void UpdateTags() {
            String sourceFolder = "D:\\WRONGTAGS";
            String destinationFolder = "D:\\CRRTAGS";
            //First Delete Temp Folder Files before copy images there
            File[] tempImages = new File(destinationFolder).listFiles();
            for (int j = 0; j < tempImages.length; j++) {
                tempImages[j].delete();
            }
            //now get all files in tag folder
            File[] allFiles = new File(sourceFolder).listFiles();
            DicomObject dcmObj = new BasicDicomObject();
            DicomInputStream din = null;
            for (int i = 0; i < allFiles.length; i++) {
                System.out.println("Current Image in Progress = " + (i + 1)
                        + " out of = " + allFiles.length);
                try {
                    din = new DicomInputStream(allFiles[i]);
                    din.readDicomObject(dcmObj, -1);
                    //System.out.println(" Tag.PatientID  *******************   " + Tag.PatientID);
                    //dcmObj.putString(1048608, VR.LO, "1234");
                    dcmObj.putString(Tag.PatientName, VR.LO, "XYZPatientName");
                    dcmObj.putString(Tag.PatientBirthDate, VR.DA, "19690101");
                    dcmObj.putString(Tag.PatientAge, VR.AS, "060Y");
                    dcmObj.putString(Tag.Modality, VR.CS, "NM");
                    dcmObj.putString(Tag.AccessionNumber, VR.SH, "1213456");
                    this.writeFile(dcmObj, destinationFolder, "\\" + allFiles[i].getName());
                    din.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            } //Loop
            //get all files from tag file and delete them
        }
        public void writeFile(DicomObject obj, String copyServer, String fileName) {
            File f = new File(copyServer + fileName);
            FileOutputStream fos;
            try {
                fos = new FileOutputStream(f);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return;
            }
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            DicomOutputStream dos = new DicomOutputStream(bos);
            try {
                dos.writeDicomFile(obj);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            } finally {
                try {
                    dos.close();
                } catch (IOException ignore) {
                }
            }
        }
        public static void main(String[] args) {
            DicomEditor dcmEdit = new DicomEditor();
            dcmEdit.UpdateTags();
        }
    }

No comments: