1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
/*=========================================================================
Program: GDCM (Grassroots DICOM). A DICOM library
Copyright (c) 2006-2011 Mathieu Malaterre
All rights reserved.
See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
/**
* Simple example showing that Jav UTF-16 string are properly passed to
* GDCM layer as locale 8bits
* This example also explain the use of try {} finally {} in java to
* make sure the C++ RAII design of gdcm.Reader is properly used from
* Java and does not leak opened file destructor.
*
* Compilation:
* $ CLASSPATH=gdcm.jar javac ../../gdcm/Examples/Java/ReadFiles.java -d .
*
* Usage:
* $ LD_LIBRARY_PATH=. CLASSPATH=gdcm.jar:. java ReadFiles gdcmData
*/
import gdcm.*;
import java.io.File;
public class ReadFiles
{
static int i = 0;
public static void process(String path)
{
//String path = file.getPath();
assert PosixEmulation.FileExists(path) : "Problem converting to 8bits";
System.out.println("Reading: " + path );
System.out.println("File: " + i++);
Reader r = new Reader();
try
{
r.SetFileName( path );
TagSetType skip = new TagSetType();
skip.insert( new Tag(0x7fe0,0x10) );
boolean b = r.ReadUpToTag( new Tag(0x88,0x200), skip );
//System.out.println("DS:\n" + r.GetFile().GetDataSet().toString() );
}
finally
{
r.delete(); // will properly call C++ destructor and close file descriptor
}
}
// Process only files under dir
public static void visitAllFiles(File dir)
{
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i=0; i<children.length; i++)
{
visitAllFiles(new File(dir, children[i]));
}
}
else
{
process(dir.getPath());
}
}
public static void waiting (int n)
{
long t0, t1;
t0 = System.currentTimeMillis();
do
{
t1 = System.currentTimeMillis();
}
while ((t1 - t0) < (n * 1000));
}
public static void main(String[] args) throws Exception
{
String directory = args[0];
Directory gdir = new Directory();
long n = gdir.Load( directory, true );
System.out.println( gdir.toString() );
FilenamesType files = gdir.GetFilenames();
for( long i = 0; i < n; ++i )
{
String path = files.get( (int)i );
process( path );
}
System.out.println( "Java API" );
//waiting( 10 );
for( int i = 0; i < 2; ++i )
{
File dir = new File(directory);
visitAllFiles(dir);
}
}
}
|