How to get DicomObject using InputStraem of a file, to read header information

2,488 views
Skip to first unread message

narendras...@gmail.com

unread,
Jan 8, 2013, 5:04:12 AM1/8/13
to dcm...@googlegroups.com
right now i m trying from following code.

         InputStream is = new FileInputStream(file);
         DicomInputStream dis = new DicomInputStream(is);
         setImage(ImageIO.read(is));
         DicomObject  dcmObj = dis.readDicomObject();
         Iterator iter = dcmObject.datasetIterator();
but from above code i don't able to retrieve header info.


dimitri pianeta

unread,
Jan 8, 2013, 11:06:47 AM1/8/13
to dcm...@googlegroups.com

Here first answer :

/**

  • Modify metadata dicom
  • not vr= SQ
  • @param file : input File
  • @param tagChooser: choosing tag
  • @param aModify : "valueString"
  • @param vr : vr of Tag choosing
  • @param newString : new String
  • exemple:
try {
Dicom.changementTag(chooser.getSelectedFile(), Tag.PatientName , "Dimitri", VR.PN, "piane" );
} catch (IOException e) {
e.printStackTrace();
}
  • @throws IOException
*/

public static void changementTag(File file, int tagChooser, String aModify, VR vr, String newString ) throws IOException{

DicomInputStream dis = new DicomInputStream(file);
DicomObject dio = dis.readDicomObject();
dis.close();

boolean change = false;
if(dio.getString(tagChooser).equals(aModify )){
dio.putString(tagChooser, vr, newString);
change = true;
}

if(change){
FileOutputStream fos = new FileOutputStream( new File(file.getParent()+ "/"file.getName() "(" + "modifier"+ ")"+".dcm"));
BufferedOutputStream bos = new BufferedOutputStream(fos);
DicomOutputStream dos = new DicomOutputStream(bos);
dos.writeDicomFile(dio);
dos.close();
}
}

or orther method following :
/**

  • Modify metadata dicom
  • not vr= SQ
  • @param file : input File
  • @param tagChooser: choosing tag
  • @param aModify : "valueString"
  • @param vr : vr of Tag choosing
  • @param newString : new String
  • Example:
  • JChooser choix = new JChooser();
int retour = choix.showOpenDialog(parent);
*
  • if(retour == JFileChooser.APPROVE_OPTION){
fileInput = choix.getSelectedFile();
JFileChooser saveDicom = new JFileChooser();
saveDicom.setMultiSelectionEnabled(false);
saveDicom.showSaveDialog(null);
File fileOutput =saveDicom.getSelectedFile();
} else return;
  • changementTag2(fileInput, int tagChooser, String aModify, VR vr, String newString, fileOutput)
*/
public static void changementTag2(File fileInput, int tagChooser, String aModify, VR vr, String newString, File fileOutput ) throws IOException{

DicomInputStream dis = new DicomInputStream(fileInput);
DicomObject dio = dis.readDicomObject();
dis.close();

boolean change = false;
if(dio.getString(tagChooser).equals(aModify )){
dio.putString(tagChooser, vr, newString);
change = true;
}

if(change){
FileOutputStream fos = new FileOutputStream(fileOutput +".dcm");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DicomOutputStream dos = new DicomOutputStream(bos);
dos.writeDicomFile(dio);
dos.close();
}
}

and the file displayTag who are differents methods for processing metadata DICOM with dcm4che2.






displayTag.java

Bradley Ross

unread,
Jan 8, 2013, 6:46:01 PM1/8/13
to dcm...@googlegroups.com
The steps are
Create a DicomInputStream that points to the file
Read the file to create a DicomObject

DicomInputStream din = null;
DicomObject working = null;
try {
din = new DicomInputStream(file);
working = din.readDicomObject();
} catch (IOException e) {
System.out.println(e.getClass().getName() + " " + e.getMessage();
}

Look at the methods in org.dcm4che2.data.DicomObject, especially getString, getInt, getString, etc.  These can be used to obtain header values. Also look at org.dcm4che2.data.Tag.

String studyInstanceUID = working.getString(Tag.StudyInstanceUID);
int numberOfStudyRelatedInstances = working.getInt(Tag.NumberOfStudyRelatedInstances);

The same approach can be used for other tags.  When looking for nested Dicom objects, it gets a little more complicated.

The iterator is used to get a list of all of the tags.  (Remember that one of the elements can be a nested object, in which case you have to find
the elements of that object.)  Look at org.dcm4che2.data.DicomElement

Iterator<org.dcm4che2.org.DicomElement> iter = working.iterator();
while (iter.hasNext()) {
DicomElement element = iter.next();
/*
 *  Each element will appear here as the iterator steps through the object.  You can do
 *  anything you want with the elements.
 */
}

The commandIterator, datasetIterator, and iterator methods look for different sets of elements.

narendras...@gmail.com

unread,
Jan 9, 2013, 12:42:15 AM1/9/13
to dcm...@googlegroups.com
Thanks Bradley Ross for your Valuable response.

narendras...@gmail.com

unread,
Jan 9, 2013, 12:50:42 AM1/9/13
to dcm...@googlegroups.com
Thanks Sir , I got it how to retrieve header info  from your code. very very thanks.

Bradley Ross

unread,
Jan 9, 2013, 4:36:29 PM1/9/13
to dcm...@googlegroups.com
I forgot to mention that you need to close the DicomInputStream after creating the Dicom object.

eleac

unread,
Jan 30, 2017, 3:25:51 PM1/30/17
to dcm4che
Hi! I'm using this code and everything works fine, except when I'm trying to read a large file (600 MB), then I get java.lang.IndexOutOfBoundsException. in line din = new DicomInputStream(file); Complete exception:

Exception in thread "DCMRCV-2" java.lang.IndexOutOfBoundsException
        at java.io.BufferedInputStream.read(BufferedInputStream.java:327)
        at org.dcm4che2.io.DicomInputStream.read(DicomInputStream.java:302)
        at org.dcm4che2.io.DicomInputStream.readFully(DicomInputStream.java:349)
        at org.dcm4che2.io.DicomInputStream.readBytes(DicomInputStream.java:656)
        at org.dcm4che2.io.DicomInputStream.readValue(DicomInputStream.java:587)
        at org.dcm4che2.io.DicomInputStream.parse(DicomInputStream.java:515)
        at org.dcm4che2.io.DicomInputStream.readDicomObject(DicomInputStream.java:415)
        at org.dcm4che2.io.DicomInputStream.readDicomObject(DicomInputStream.java:423)

Any ideas? I'm really stuck here.

Thank you in advance.
Reply all
Reply to author
Forward
0 new messages