0

I am trying to retrieve DICOM images from PACS server, I get DICOM object using dcmqr.get and convert it to byte array, and when I pass byte array of DICOM object to getPixelDataAsBufferedImage method its gives exception java.lang.ArrayIndexOutOfBoundsException: 0 at line 'buff = reader.read(0, param);'.

I am very new in DICOM, not getting exactly what is the issue. Is there any problem in making query to get DICOM or any problem in converting byte array to buffered image ?

public class TestDCM {

public static DcmQR dcmqr;

public static void main(String[] args) {

    dcmqr = new DcmQR("DCM4CHEE");
    dcmqr.setCalledAET("DCM4CHEE", true);
    dcmqr.setRemoteHost("IP address");
    dcmqr.setRemotePort(11112);

    dcmqr.setDateTimeMatching(true);
    //dcmqr.setCFind(true);
    dcmqr.setCGet(true);        
    dcmqr.configureTransferCapability(true);

    dcmqr.setQueryLevel(DcmQR.QueryRetrieveLevel.IMAGE);

    dcmqr.addMatchingKey(Tag.toTagPath("SOPClassUID"),
            "1.2.840.10008.5.1.4.1.1.12.2");

    List<DicomObject> result = null;
    byte[] imgTab = null;
    BufferedImage bImage = null;
    try {           
        dcmqr.start();
        System.out.println("started");
        dcmqr.open();
        System.out.println("opened");
        result = dcmqr.query();
        System.out.println("queried");
        dcmqr.get(result);
        System.out.println("List Size = " + result.size());

        for (DicomObject dco : result) {
            dco.putString(Tag.TransferSyntaxUID, VR.UI,UID.ImplicitVRLittleEndian);
            // convert dicom object to byte array  
            imgTab = toByteArray(dco);
            // convert byte array to BufferedImage 
            bImage = getPixelDataAsBufferedImage(imgTab);
            File file = new File("filepath.jpg");
            writeBufferedImage(file, bImage);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        if (dcmqr != null) {
            dcmqr.stop();
            dcmqr.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("done");
}
}

// toByteArray method
public static byte[] toByteArray(DicomObject obj) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(baos);
    DicomOutputStream dos = new DicomOutputStream(bos);
    dos.writeDicomFile(obj);
    dos.close();
    byte[] data = baos.toByteArray();
    return data;
}

//getPixelDataAsBufferedImage method
public static BufferedImage getPixelDataAsBufferedImage(byte[] dicomData)
        throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(dicomData);
    BufferedImage buff = null;
    Iterator<ImageReader> iter = ImageIO
            .getImageReadersByFormatName("DICOM");
    ImageReader reader = (ImageReader) iter.next();
    DicomImageReadParam param = (DicomImageReadParam) reader
            .getDefaultReadParam();
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    reader.setInput(iis, false);
    buff = reader.read(0, param);
    iis.close();
    if (buff == null)
        throw new IOException(
                "Could not read Dicom file. Maybe pixel data is invalid.");
    return buff;
}

//writeBufferedImage method
public static void writeBufferedImage(File file , BufferedImage bufferedImage){
    try {
        ImageIO.write(bufferedImage, "jpg", file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("Jpeg image created successfully.");
}
Dharmraj
  • 164
  • 1
  • 15

2 Answers2

1

I was successfully retrieve and convert DICOM. I am sharing the complete steps to retrieve and convert. It will help others too.

//Step 1: Retrieve DICOM

// Transfer Syntax
String[] DEF_TS = { 
    UID.JPEGLossless,
    UID.JPEGLosslessNonHierarchical14, UID.JPEGLSLossless,
    UID.JPEGLSLossyNearLossless, UID.JPEG2000LosslessOnly,
    UID.JPEG2000, UID.JPEGBaseline1, UID.JPEGExtended24, UID.MPEG2,
    UID.DeflatedExplicitVRLittleEndian, UID.ExplicitVRBigEndian,
    UID.ExplicitVRLittleEndian, UID.ImplicitVRLittleEndian 
}; 

DcmQR dcmqr = new DcmQR();

/* Set remote PACS server parameter */
dcmqr.setCalledAET("[Called AET]", false); //AET, host and port configured in PACS server
dcmqr.setRemoteHost("[PACS server host]"); 
dcmqr.setRemotePort("[Pacs server port]");

/* Set local port to listen */
dcmqr.setCalling("Receiving AET"); //Receiving AET, host and port should be configured in PACS server in association
dcmqr.setLocalHost("Receiving host(Local server host)");
dcmqr.setLocalPort("Receiving port(Local server post)");

// Set user detail
UserIdentity dcmuserId = new UserIdentity.UsernamePasscode(userId,
pin.toCharArray());
dcmuserId.setPositiveResponseRequested(true);
dcmqr.setUserIdentity(dcmuserId);

// Add Transfer syntax UID
dcmqr.addStoreTransferCapability("1.2.840.10008.5.1.4.1.1.1", DEF_TS); // CR
dcmqr.addStoreTransferCapability("1.2.840.10008.5.1.4.1.1.2", DEF_TS); // CT
dcmqr.addStoreTransferCapability("1.2.840.10008.5.1.4.1.1.4", DEF_TS); // MR/MRI
//.... Add all UID as required

dcmqr.setEvalRetrieveAET(true);

dcmqr.setCFind(true);

dcmqr.setCGet(true);

dcmqr.setMoveDest([Receving AE]);

dcmqr.configureTransferCapability(true);

// Set complete destination directory path, Where you want to store DICOMs
dcmqr.setStoreDestination("[dicomFilePath]");

dcmqr.setQueryLevel(DcmQR.QueryRetrieveLevel.STUDY);

dcmqr.setQueryLevel(DcmQR.QueryRetrieveLevel.SERIES);

dcmqr.setQueryLevel(DcmQR.QueryRetrieveLevel.IMAGE);

dcmqr.addMatchingKey(Tag.toTagPath("StudyID"), [study id]); // other filters also can be add here

dcmqr.addReturnKey(Tag.toTagPath("SeriesNumber"));

dcmqr.start();
dcmqr.open();
List<DicomObject> result = dcmqr.query(); 
dcmqr.move(result);
// Here found dicoms with matching criteria will move from PACS server to your machine in provided destination path.


//step 2: DICOM file to DICOM object
DicomInputStream din = new DicomInputStream("dicom file path");
DicomObject dicomObject = din.readDicomObject();


//step 3: Convert DICOM to JPEG
byte[] imgTab = toByteArray(dicomObject); // See in question
/* create buffered image from byte array */
BufferedImage bufferedImage = getPixelDataAsBufferedImage(imgTab); // See in question
File outPutFile = new File("JPEG file path");
/* creating jpeg image from buffered image */
ImageIO.write(bufferedImage, "jpeg", outPutFile);

Your JPEG is ready. Cheers

Dharmraj
  • 164
  • 1
  • 15
0

While it is possible with a C-GET to pull full dicom objects including pixel data, it is fraught with difficulty. It is much simpler to use C-MOVE. The documentation for both the dcmqr utility and other stack overflow discussions cover this.

http://www.dcm4che.org/confluence/download/attachments/438/dcmqr+usage.txt?version=1&modificationDate=1351401060265

DICOM C-GET vs C-MOVE

Community
  • 1
  • 1
Brett Okken
  • 6,210
  • 1
  • 19
  • 25
  • Hi Brett, Thanks for reply. how can i use C-MOVE in java? i use the image retrieval command in the above link its working for me.but how can i implement it in java? i tried to implement this i added 2 lines in posted code 1st is dcmqr.setMoveDest("C:/dicom"); and at the place of dcmqr.get(result); i used dcmqr.move(result); but destination folder is remains empty. – Dharmraj Jun 11 '14 at 09:27
  • I would suggest looking directly at the source as reference: https://svn.code.sf.net/p/dcm4che/svn/dcm4che2/trunk/dcm4che-tool/dcm4che-tool-dcmqr/src/main/java/org/dcm4che2/tool/dcmqr/DcmQR.java – Brett Okken Jun 11 '14 at 11:42