Tuesday, March 25, 2008

Converting DICOM to JPEG using dcm4che 2

Hi Folks,

Sorry, I was quite busy these days, weeks, months :) Now, I can share with you how to convert any Dicom image to Jpeg using the amazing dcm4che 2.0 toolkit. I've spent a lot of time trying to figure out the magic of dcm4che and I've got some stuff that will be posted later.However, the best way to learn dcm4che is studying its utilities apps, so dcm2jpg is the one you must have a look. From now on let's just see how to perform the conversion.

First, you have to download the latest version of dcm4che libraries. Also, you'll need the JAI ImageIO Toolkit in order to quickly read pixel data from Dicom images. And if you don't have any java editor get the best one: Eclipse. You are supposed to know basic java programming, the Eclipse environment and some Dicom stuff.

Lets start. Open your Eclipse IDE and choose File > New > Java Project. Name it myDicomToJpeg. The next step is to create a new class, so right-click the src package and select New > Class. Enter DicomToJpeg for class name and select the main method option. You'll get something like this:

public class DicomToJpeg {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}

In order to use dcm4che classes we have to configure the project's build path. Right-click the project's main folder and select Properties. Select the Java Build Path option. Under Libraries tab we'll find the Add External Jars button. Click on it to select all dcm4che jar files and all JAI ImageIO jar files. Now our project is able to work with dcm4che.

Write the following line inside the main's body. This is our Dicom file:

File myDicomFile = new File("c:/dicomImage.dcm");

Then let's declare what will be our Jpeg image:

BufferedImage myJpegImage = null;

The following line returns an Iterator containing all currently registered ImageReaders that claim to be able to decode the named format (e.g., "DICOM", "jpeg", "tiff").

Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");

The java documentation says an ImageReader object are normally instantiated by the service provider interface (SPI) class for the specific format. Service provider classes (e.g., instances of ImageReaderSpi) are registered with the IIORegistry, which uses them for format recognition and presentation of available format readers and writers. So let's get our ImageReader object.

ImageReader reader = (ImageReader) iter.next();

If you check carefully dcm4che libraries you'll find some specific imageio packages. That's where it keeps the secret of reading Dicom pixel data. Therefore, our ImageReader object is now an instance of a specific SPI class for the Dicom format.

In the following line we get parameters for reading the Dicom image. The java documentation says an ImageReadParam class describe how a stream is to be decoded. Instances of this class or its subclasses are used to supply prescriptive "how-to" information to instances of ImageReader.

DicomImageReadParam param = (DicomImageReadParam) reader.getDefaultReadParam();

Before read all pixel data we have to create an ImageInputStream object for use by our ImageReader object. Note that this input stream has our Dicom file as a parameter. As this process throws an IOException it must be between a try-catch block.

Now all we have to do is to call the read() method from our ImageReader object together with our prescriptive Dicom parameters. The result is a new BufferedImage filled with Dicom pixel data. Pretty easy, hun :)

try {
ImageInputStream iis = ImageIO.createImageInputStream(myDicomFile);
reader.setInput(iis, false);
myJpegImage = reader.read(0, param);
iis.close();

That's it! We read the Dicom image into our myJpegImage object. The following lines just test if we really succeeded. If not, we just print a message.

if (myJpegImage == null) {
System.out.println("\nError: couldn't read dicom image!");
return;
}

You may be asking: ok, how do I get my Jpeg file? The answer is in the following lines. We firstly need to create a Jpeg file that will be sent to an OutputStream to be saved. Then, the JPEGImageEncoder object is responsible for encoding our Jpeg image into this output. When we close the OutputStream our Jpeg file is saved. We're done!!

File myJpegFile = new File("c:/jpegImage.jpg");
OutputStream output = new BufferedOutputStream(new FileOutputStream(myJpegFile));
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(myJpegImage);
output.close();

Finally, if our try to read the image failed, we probably got an exception. So let's handle it printing a message.

}
catch(IOException e) {
System.out.println("\nError: couldn't read dicom image!"+ e.getMessage());
return;
}

Have a nice day! Hope it helps :)

Samuel.

91 comments:

navdeeprana said...

nice job.........i will appreciate if u provide the same explanation for converting JPEG to DICOM tool that is there in dcm4che2...

Unknown said...

Thanks navdeeprana!!

I'm preparing another quick tutorial on how to convert Jpeg to Dicom. See you soon!

Anonymous said...

hello, I have the same problem to convert DICOM to JPEG...
I get an error:

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/media/imageio/stream/StreamSegmentMapper
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi.createReaderInstance(DicomImageReaderSpi.java:133)
at javax.imageio.spi.ImageReaderSpi.createReaderInstance(ImageReaderSpi.java:296)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:503)
at javax.imageio.ImageIO$ImageReaderIterator.next(ImageIO.java:487)
at DicomToJpeg.main(DicomToJpeg.java:27)
Caused by: java.lang.ClassNotFoundException: com.sun.media.imageio.stream.StreamSegmentMapper
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

how can I solve it?
thank you

Unknown said...

Hi Anonymous, thanks for comming!

The java runtime environment is not able to load your imageio classes. To solve this, plese, check your build path and be sure imageio is properly installed and referenced by your application :)

See you,
Samuel.

Anonymous said...

I have tried to implement your code. But I have the following exception when I call setInput. Do you know why?

Exception in thread "main" java.lang.IllegalArgumentException
at javax.imageio.ImageReader.setInput(libgcj.so.81)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.setInput(DicomImageReader.java:150)
at javax.imageio.ImageReader.setInput(libgcj.so.81)
at DicomToJpeg.main(DicomToJpeg.java:27)

Unknown said...

Hi Folks!

Please, if you've got any problems running my example codes, just let me know. Send your source code to samucs@gmail.com, so I can check it and return a solution.

Best regards,

Samuel.

Anonymous said...

Thanks to Samuel who is really a very nice guy and replies promptly!

The exceptions I have just found appear only in Linux. The same code works perfectly fine in Windows. I even use the same libraries (jars) in my Linux/Windows shared file directories.

If anyone knows the issues in Linux about using dcm4che and JAI ImageIO toolkit, please discuss.

Thanks Samuel!

Candy.

razumi said...

Thanks samuel.
Very helpful. Can you show an example in how to play dicom image sequence on jpanel ?

Unknown said...

Hello Natam,
Thanks for commenting!

I'd have to spend some time on building such tutorial. However, I would try something like this:

1) Code a class extended from JPanel called DicomPanel;
2) Override the paint() method so you can paint BufferedImages on it;
3) Keep your Dicom images inside a Vector object as elements;
4) Also, you'll need some methods for extracting the image pixel data;
5) Finally, create some methods for rolling the Vector back and forward;

There are several ways to do it. This is just a checklist. If I've got some results in the near future, I'll post here. Give a try and good luck!

Regards,

Samuel.

razumi said...
This comment has been removed by the author.
razumi said...

Hi,

How can we get the dicom image measurement ?

multiframes said...

Sorry i have the following error:

java.lang.UnsupportedClassVersionError: org/dcm4che2/imageioimpl/plugins/dcm/DicomImageReaderSpi (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:219)
at sun.misc.Service$LazyIterator.next(Service.java:270)
at javax.imageio.spi.IIORegistry.registerApplicationClasspathSpis(IIORegistry.java:174)
at javax.imageio.spi.IIORegistry.init(IIORegistry.java:113)
at javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:134)
at javax.imageio.ImageIO.clinit(ImageIO.java:46)
at src.DicomToJpeg.main(DicomToJpeg.java:17)
Exception in thread "main"

Unknown said...

Hello miem,
Please, what kind of measure do you want to perform?

Unknown said...

Hi seducer,

It seems to be class version incompatibility. Please, check if you've got the latest JAI Imageio library. Which version of dcm4che and Jdk are you programming?

Regards,

Samuel.

multiframes said...

Hi Samuel
am using the following:
dcm4che-2.0.15-bin.zip
jai_imageio-1_1-lib-windows-i586-jar.zip

and i have JDK 1.4.2

Thanks

Unknown said...

Hi seducer,

I would try JDK 1.5 or greater :)

Regards,

Samuel.

razumi said...

The scale, meaning to say, when we draw a line on the image, we can calculate the actual length.

Just like in the imagej function.

Unknown said...

Hi miem,

I believe it's an image processing issue. You should code a program that calculates the distance between pixel A and pixel B.

Dcm4che2 Toolkit can provide you some Dicom header information, such as: image width/height, slice thickness (for CT), image modality, etc. I hope it helps!

Regards,

Samuel.

Anonymous said...

Hi,Samuel
I I get an error like that

Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/media/imageio/stream/StreamSegmentMapper
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi.createReaderInstance(DicomImageReaderSpi.java:133)
at javax.imageio.spi.ImageReaderSpi.createReaderInstance(Unknown Source)
at javax.imageio.ImageIO$ImageReaderIterator.next(Unknown Source)
at javax.imageio.ImageIO$ImageReaderIterator.next(Unknown Source)
at DicomToJpeg.main(DicomToJpeg.java:35)

I am using:
dcm4che-2.0.14-bin.zip (is it not great ?)
jai_imageio-1_1-lib-windows-i586-jar.zip
JDK 1.6

i have add all the dcm4che jar and the jai_imageio_windows-i586.jar to the ext-lib

i will appreciate if you could reply.

a Chinese student

Unknown said...

Hi chinese student,

Your program cannot find the imageio libraries. If you're using Eclipse just "add external jars" to your project. Else, you'll should to reference imageio jars at command line.

Regards,

Samuel.

Anonymous said...

Nice work... I really thankful to u. Excellent job. I will appreciate if u provide me solution to get header information and pixel data separately from DICOM file.... u can reply me at bmahussain@yahoo.com.
Thanks in advance....

Ijon Tichy said...

Great work !!

you save me hours of research, thank you.

hope to see better DCM4CHE toolkit doc soon :D

Anonymous said...

hi, i have found your code very helpful but would like to ask you a question. I have a folder containing a large sequence of dicom images from one particular patient. i was wondering if you had any ideas for reading them in sequentially, storing each image in an array, of buffered images possibly,for some later processing and also converting each to a jpeg.

nice job on all the work you've done

Unknown said...

Hi Jay, thanks for commenting!

Suppose you have this directory C:\images. Than you could code something like this:

Vector images = new Vector(1,1);
File dir = new File("c:/images");
File[] files = dir.listFiles();

for (int i=0; i<files.length; i++) {
BufferedImage image = getImage(files[i]);
images.add(image);
}

Where getImage() is a method that implements the code from this tutorial and returns a BufferedImage object.

Finally, if you want to get your images back, do like this:

BufferedImage image = (BufferedImage) images.get(i);

Kindly regards,

Samuel.

Anonymous said...

HI again, thanks very much for the help, that seems to work rather well, i was wondering if you knew a method of naming the files sequentially, ie myJpeg1,myJpeg2 and so on, or of another simpler method perhaps.

thanks again for all of the help!!

Anonymous said...

Hi, thanks for putting up all the great info.

I was wondering if you are aware of an element in the dicom header which contains the sequence or slice number?

Thanks again

Unknown said...

Hello Max,

Thanks for commenting :)

Yes, you're right! There are some mandatory Dicom tags that you must put in the header,such as: patient id, patient name, all UIDs. I might edit the post and add this info.

Kindly regards,

Samuel.

priyavrat narula said...

hello samucs,
Nice work.really i very good work,i want to know can i create the applet of this and make it to work on the browser.please reply soon.thanx

priyavrat narula said...

hi again,
my task is to convert dcm file to jpeg and view it on the browser.it is possible with applet i think so.pls help me out of this samucs

Unknown said...

Hi priyavrat narula,

Thanks for visiting :)

If you transform my Class into an Applet you'll definitely get your task to work. However you'll need to code a graphic user interface for users to upload a Dicom file and then push any button to convert it to Jpeg. Have you ever done the Sun Applet Tutorial? You can learn a lot there :)

Regards,

Samuel.

priyavrat narula said...

hi samuel,
thanx for replying.i converted your class to the applet.but getting run time error.
the errors are
Exception in thread "AWT-EventQueue-2" java.lang.IllegalStateException: Input not set!
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.readMetaData(DicomImageReader.java:246)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:291)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:415)
at JpegToDicom.paint(JpegToDicom.java:53)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-2" java.lang.IllegalStateException: Input not set!
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.readMetaData(DicomImageReader.java:246)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:291)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:415)
at JpegToDicom.paint(JpegToDicom.java:53)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-2" java.lang.IllegalStateException: Input not set!
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.readMetaData(DicomImageReader.java:246)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:291)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:415)
at JpegToDicom.paint(JpegToDicom.java:53)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
.pls reply soon
take care

Unknown said...

Hi priyavrat narula,

Please send me your source code by email. I'll try to help you :)

Samuel.

priyavrat narula said...

hi samuel,
yhanx for replying bro.i have created the applet which converts dicom image to jpeg.but i have to keep jai and dcm4chee jar files to jre6 ext folder.because i have studied that applet dont load external libraries.is their any way to make applet to read external libraries through applet.because for this i have to copy these jar files to each client.Now what my applet do is to read picture from the apache server as i m using php and create jpeg on the client side.but i want to create it on the server.because of server restrictions i m not able to create image on the server.is their any way to do it.pls help me out this.i m mailing u my code pls go through it and i'll be expecting a good response from you.thank u

Anonymous said...

hey samuel.

Thanks for all the help you have provides. i was wondering if you are aware of a method of highlighting specific features in the dicom file.

thanks

Unknown said...

Hi Max,

Which Dicom tags do you want to highlight?

Samuel.

priyavrat narula said...

hi samuel,
have you gone through the code or not.

Anonymous said...

Hi Samuel.

This tutorial is what i was searching for a long time. Thanks a lot for helping.

Every thing works perfect!

I was wondering if You try to display an dicom image directly on jPanel...

Natam was writing about sequences - what i need is to display dicom image on jPanel. Do you have any code, based on this tutorial?

I will be gratefull for any help.

Kind Regards
Raf.

Walid said...

I have this errors:

log4j:WARN No appenders could be found for logger (org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader available for format:jpeg
at org.dcm4che2.imageio.ImageReaderFactory.getReaderForTransferSyntax(ImageReaderFactory.java:99)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initCompressedImageReader(DicomImageReader.java:310)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:295)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:422)
at DicomToJpeg.main(DicomToJpeg.java:32)

Unknown said...

Hello Walid,

Thanks for visiting! Please be sure you have installed JAI Imageio classes properly. Then this error should be solved.

Regards,

Samuel.

Walid said...

Hello Samuel,
i installed the jai_imageio-1_0_01-lib-windows-i586.exe and two jar files appears in the path.
Which library is the right to download ?
Thank you for your help

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Unknown said...

Thank you very much for your programming.
This blog really helped me.
I REALLY REALLY appriciate it.

I had the same problem as others. So let me keep my solutions.

Someone needs to download Image I/O tool and install. They can solve their problems.

* Java Advanced Imaging Image I/O Tools 1.1 Alpha(jai_imageio-1_1-alpha-lib-windows-i586-jre.exe)
->http://java.sun.com/products/java-media/jai/downloads/download-iio-1_1.html

Thanks and regards

Unknown said...

Hello I am sorry that this comment is here but there was no option of leaving comments in the Dicom multiframe playback thread
I saw that I was not the only ome with this problem so please help
I tried the example by the toturial and i got this exceptoin when I tried to read the file



reading dicom image...
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/sun/media/imageio/stream/StreamSegmentMapper
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi.createReaderInstance(DicomImageReaderSpi.java:146)
at javax.imageio.spi.ImageReaderSpi.createReaderInstance(Unknown Source)
at DicomMultiframePlayer.openFile(DicomMultiframePlayer.java:169)
at DicomMultiframePlayer.actionPerformed(DicomMultiframePlayer.java:239)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.media.imageio.stream.StreamSegmentMapper
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 29 more

what to do

Unknown said...

Hello

I had the same problem and I fixed it by adding JAI-imagio.jar

However, now I have new Exeptions:



reading dicom image...
log4j:WARN No appenders could be found for logger (org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader).
log4j:WARN Please initialize the log4j system properly.
dicom image have76frames
extracting frames
Exception in thread "AWT-EventQueue-0" org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader available for format:jpeg
at org.dcm4che2.imageio.ImageReaderFactory.getReaderForTransferSyntax(ImageReaderFactory.java:99)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initCompressedImageReader(DicomImageReader.java:332)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:317)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:479)
at javax.imageio.ImageReader.read(Unknown Source)
at DicomMultiframePlayer.openFile(DicomMultiframePlayer.java:180)
at DicomMultiframePlayer.actionPerformed(DicomMultiframePlayer.java:239)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)


Help, please.

Unknown said...

Hi Folks,

Some of you need to download and install the JAI Imageio (https://jai-imageio.dev.java.net/binary-builds.html) library in order to run some of my tutorials properly.

After installing be sure you have "jai_imageio.jar" and "clibwrapper_jiio.jar" in your classpath so Java can find the required library classes.

Best regards,

Samuel.

Unknown said...

Thanks,

But still have Exeptions:
(After installing JAI imagio and adding the mentioned jars):


log4j:WARN No appenders could be found for logger (org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "AWT-EventQueue-0" org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader available for format:jpeg
at org.dcm4che2.imageio.ImageReaderFactory.getReaderForTransferSyntax(ImageReaderFactory.java:99)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initCompressedImageReader(DicomImageReader.java:332)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:317)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:479)

Regards.

Ben Vercammen said...

I took a slightly different approach to trying out your tutorial, as I used maven to load all dependencies (repository = http://www.dcm4che.org/maven2, I'm using version 2.0.22). Now the issue I'm sharing here probably won't have anything to do with it, however, with the sample Dicom images I was using (http://pubimage.hcuge.ch:8080/ Amnesix set), I ended up with the following ConfigurationException:

org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib available for format:jpeg2000

So I found out that these Readers were defined in the ImageReaderFactory.properties file, which I then copied out of the JAR (dcm4che-imageio) and put in the org.dcm4che.imageio package. I just changed the J2KImageReaderCodecLib references to J2KImageReader, and the error was gone and I finally got my JPEG!

I just wanted to share this in case someone else would stumble upon the same issue. It does leave me wondering if there's some more info to be found (no doubt) the mappings in that properties file, and what the reasons are whether or not the "default" mapping is usable. So if someone can provide me with an easy answer (I currently have no more time to spend), that would be greatly appreciated!

Unknown said...

Hello
I tried to add JAI imageio as well and after adding a user library with the 2 jars to the classpath I still get this errors
as I am not a profesional I will explain all IO did
I downloaded the dcm4che2 and added all jars to the classpath, (I can see imagereaderFactory in one of the added jars) then I added log4j and slf4j (I get a warning on the log4j that I need to intialize it how do I do that?????) then I installed jai-imageio for windows and added jai imageio and clibwrapper jars among more jars in jai-imageio
but when I ran the tutorial I got these Exceptions



reading dicom image...
log4j:WARN No appenders could be found for logger (org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
dicom image have76frames
extracting frames
Exception in thread "AWT-EventQueue-0" org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader available for format:jpeg
at org.dcm4che2.imageio.ImageReaderFactory.getReaderForTransferSyntax(ImageReaderFactory.java:99)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initCompressedImageReader(DicomImageReader.java:332)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:317)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:479)
at javax.imageio.ImageReader.read(Unknown Source)
at DicomMultiframePlayer.openFile(DicomMultiframePlayer.java:180)
at DicomMultiframePlayer.actionPerformed(DicomMultiframePlayer.java:239)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)




can anyone please help me undetstand how to make it work
pllleeeeaaaaassseee
samu por favor ayuda me
obrigado

Unknown said...

I tried this tutorial as well
and to my surprise in this line

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);

it doesnt recognise JPEGImageEncoder nor JPEGCodec

may this is the source of my problem????

Unknown said...

O.K
I'm sorry for all the msgs
however
I managed to run the example how to convert dicom 2 jpeg.
It ran perfectlly but with the warnining mentioned above about log4j.

now when I am running the multiframePlayer
it doesnt work still with the errors mentioned above


any ideas????

Anonymous said...

Hello Semuel..

i am so new in this field..

i have tried your code but suddenly the error appear:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
File cannot be resolved to a type
File cannot be resolved to a type
BufferedImage cannot be resolved to a type
Iterator cannot be resolved to a type
ImageReader cannot be resolved to a type
ImageIO cannot be resolved
ImageReader cannot be resolved to a type
ImageReader cannot be resolved to a type
DicomImageReadParam cannot be resolved to a type
DicomImageReadParam cannot be resolved to a type
ImageInputStream cannot be resolved to a type
ImageIO cannot be resolved
File cannot be resolved to a type
File cannot be resolved to a type
OutputStream cannot be resolved to a type
BufferedOutputStream cannot be resolved to a type
FileOutputStream cannot be resolved to a type
JPEGImageEncoder cannot be resolved to a type
JPEGCodec cannot be resolved
IOException cannot be resolved to a type

at DicomToJpeg.main(DicomToJpeg.java:6)

please help me.. i really dont understand it.. =(

Anonymous said...

I am newbie in Dicom Area. I want to know that dcm4che is free api? Because after using this when I generated images then image generated with demo text on it

Unknown said...

Hello Anonymous,

I suggest you to study some Java programming just to learn it's syntax and see how to solve common errors before start using dcm4che toolkit.

Yes, dcm4che is a free library. If you got text on your image maybe this image has text printed on its pixel data. Try to open the image with some free Dicom viewer to check it.

Kindly regards,

Samuel.

Anonymous said...

hi

I code is working superb. it is running the video. but I am trying to open a another dicom image or modality generated image. it is not opening.

thanks in advance

Unknown said...

Hi,

I think you are talking about the multiframe player. It was tested against XA modality images. It may work with other modalities, but sometimes changes in the code are necessary.

Kindly regards,

Samuel.

Prabhu Ganapathylingam said...

hi

I got error like below while executing ur DicomToJPEG program


Exception in thread "main" java.lang.IllegalStateException: Input not set!
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.readMetaData(DicomImageReader.java:262)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:314)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:479)
at DicomToJpeg.main(DicomToJpeg.java:49)
Java Result: 1

thanks in advance

Anonymous said...

So what about the tutorial for converting jpg to dcm?

Unknown said...

Hi,

Look at the "Don't miss it" box in this page. There you will find the Jpeg to Dicom Tutorial.

Best,

Samuel.

Unknown said...

First: Thanks for a great set of examples. They really make it much easier to get to grips with dcm4che2.

Second: I also get the No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader available for format:jpeg
error.

I've now tried with a number of different dicom files. The program seem to work with other types, such as MR, CT, Ultrasound, and Echocardiography, but not with XA files.
Do you perhaps know why? as it seems you've got it working.

regards
Claus

Unknown said...

... sorry for not telling: my above comment relates to the MultiFramePlaye example.

Unknown said...

Hi samuel ,
i am getting the follwing error

The type HTMLDocument.Iterator is not generic; it cannot be parameterized with arguments

can u please help me out

Unknown said...

Hello Shetty,

Why are you using the HTMLDocument class. I don't understand...

Regards,

Samuel.

Lurker^_^ said...

Hey Samuel,

https://jai-imageio.dev.java.net/
no longer seems to exist. Any other places where I can get the JAI Imageio libs?

Anonymous said...

Hi Samuel,
is there any better java Dicom tool kit than DCM4che to support lossy/lossless compression.

Unknown said...

Hi There!

You may try DCMTK (C#/C++). It is large adopted by many DICOM solutions and research projects.

Best,

Samuel.

Anonymous said...

Hello
i tried your program but i am facing the problem:
DicomImageReader dcmReader = (DicomImageReader)dcmIterator.next();
The above line is not able to create a dicom reader and when i tried iter.hasNext(), it returns als false value.

Thanks in advance

Anonymous said...

thanx bro u were really helpful

Anonymous said...

Many people seemed to have this error:
org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg.CLibJPEGImageReader available for format:jpeg

Found the solution: To work with JPEG Lossless, ImageIO has native libraries, which aren't found.
I had installed ImageIO (1.1) in another JDK as I chose for my project. When I switched, everything worked fine (another solution could be with the java.library.path).

Great tutorial, Samuel, thanks!

Anonymous said...

hie Samuel i have query related to Dicom image...can u explain me how can i load dicom image using JFilechooser ..and it needs any other apis and plugins to load image and save in another format

Anonymous said...

hi samuel.

I got a problem to follow your codes.

in the following line,
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);


Import "com.sun.image.codec.jpeg.JPEGCodec" package is failed;

eclipse show the message like this.

"Access restriction: The type JPEGCodec is not accessible due to restriction on required library /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar"

Do you have any solutions for this problem?

Anonymous said...

Oops I missed the my IDE environments.

I developed on the Mac Snow Leopard and used the default mac's system JRE. (JRE 6)

please let me know the solutions.

Thanks for your post.

ks.park said...

HI Samuel.
Thanks for this article.

How can I improve JPEG image quality?

Actually, I followed your code, then jpg file is created, but I cannot recognize of the result image.

My dicom image has the brain, but jpg image has not.

how can I solve this?

dirko said...

Thanks for your post. I am experiencing the same problem as the previous commenter.

The obtained jpg file is a very noisy version of the original dicom image. From the dcm4che dcm2jpg tool I learned that there are a number of params that can be set, but I have no clue what a good parameter set would be. Any suggestion?

lindi said...

Hello,
I'm trying to convert a DICOM image in jpeg2000 using the below code, it is aslo explained in oracle documentation (http://docs.oracle.com/javase/7/docs/technotes/guides/imageio/spec/apps.fm1.html), but doesn't work! I don't understand what I'm doing wrong, using: ImageIO.getReaderFormatNames() and ImageIO.getWriterFormatNames() it can be verified that DICOM and JPEG2000 are supported!
Thank you in advance...

public void convert2JPEG2000(File sourceFile) throws IOException{

Iterator iter = ImageIO.getImageReadersByFormatName("DICOM");
ImageReader reader = iter.next();

if(reader == null) {
log.error("Could not locate any Readers for the DICOM format image.");
return;
}

ImageInputStream iis = ImageIO.createImageInputStream(sourceFile);


BufferedImage bi;
try{
reader.setInput(iis);
bi = ImageIO.read(iis);

File outputFile = new File(outputFileName);

String format = "jpeg 2000";
ImageIO.write(bi, format, outputFile);

} finally {
log.info("JPEG 2000 file created successfully.");
iis.close();
}
}

Anonymous said...

Hello, I have a problem in converting DICOM to JPG because of JPEGImageEncoder Class is Depreciated in jdk 1.7 thats why i am using ImageIO.write(); to write DICOM to jpg. ImageIO can not write jpg from 16 bit DICOM.

jhon said...

If I have a Dicom image with a series with a certain number of images as I can see.

Anonymous said...

Hi everybody,

I still got the the well known exception. : )

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/dcm4che2/image/PartialComponentSampleModel
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi.createReaderInstance(DicomImageReaderSpi.java:146)
at javax.imageio.spi.ImageReaderSpi.createReaderInstance(ImageReaderSpi.java:300)


What I have in my LIB folder is :

AbsoluteLayout.jar
beansbinding-1.2.1.jar
clibwrapper_jiio.jar
commons-cli-1.2.jar
dcm4che-core-2.0.25.jar
dcm4che-imageio-2.0.25.jar
dcm4che-imageio-rle-2.0.25.jar
jai_codec.jar
jai_core.jar
jai_imageio.jar
JTattoo-1.6.8.jar
log4j-1.2.16.jar
org.apache.commons.io.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
ver.txt
vlcj-1.2.0.jar
vtk-5.4.2.jar
vtk-5.6.1.jar

What else should I add ?

I will appreciate any hint,

Regards,

Nicolás

Anonymous said...

Hi all again,

I read the blog another 3 times and I found I need also next library
dcm4che-image-2.0.25.jar

Sorry, I should read a little bit more before posting.

Now I'm getting another exception. Will try to fix it by myself first.

Thanks and regards,

Nicolás

ChetanRaj said...

Hi Samuel...
I am just impressed by looking at your blog, Great Job...
I was just searching regarding how to convert DICOM to Jpeg, hope it gonna help me...
I appreciate ur prompt response to the problems posted by others...

Anonymous said...

Exception in thread "main" java.lang.NoClassDefFoundError: org/dcm4che2/image/PartialComponentSampleModel
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReaderSpi.createReaderInstance(DicomImageReaderSpi.java:146)
at javax.imageio.spi.ImageReaderSpi.createReaderInstance(Unknown Source)
at javax.imageio.ImageIO$ImageReaderIterator.next(Unknown Source)
at javax.imageio.ImageIO$ImageReaderIterator.next(Unknown Source)
at org.dicom.DicomToJpeg.main(DicomToJpeg.java:29)
Caused by: java.lang.ClassNotFoundException: org.dcm4che2.image.PartialComponentSampleModel
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 5 more


i got this exception how can i solve this please...replay

Anonymous said...

For anyone facing the problem about

log4j:WARN No appenders could be found for logger ...

There is a suggested aproach on:

http://stackoverflow.com/questions/5081316/where-is-the-correct-location-to-put-log4j-properties-in-an-eclipse-project

Darshak Hariya said...

How do I solve this issue?

Exception in thread "main" java.lang.RuntimeException: Image Reader: com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib not registered
at org.dcm4che.imageio.codec.ImageReaderFactory.getImageReader(ImageReaderFactory.java:205)
at org.dcm4che.imageio.plugins.dcm.DicomImageReader.readMetadata(DicomImageReader.java:497)
at org.dcm4che.imageio.plugins.dcm.DicomImageReader.read(DicomImageReader.java:273)
at converter.DicomToJPEG.main(DicomToJPEG.java:27)

Anonymous said...

I also got following issue:
Image Reader: com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib not registered
java.lang.RuntimeException: Image Reader: com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib not registered
at org.dcm4che.imageio.codec.ImageReaderFactory.getImageReader(ImageReaderFactory.java:205)
at org.dcm4che.imageio.plugins.dcm.DicomImageReader.readMetadata(DicomImageReader.java:497)
at org.dcm4che.imageio.plugins.dcm.DicomImageReader.read(DicomImageReader.java:273)

Anyone know how to solve it?

Wahyudi Arifandi said...

Please put clib_jiio.dll, clib_jiio_sse2.dll, and clib_jiio_util.dll in your PATH if you are using windows.
Please use do similar by using appropriate libraries that you find inside dcm4che-3.2.1-bin.zip for other operating system. They support library for linux and solaris as well

Anonymous said...

Hi,
I'd like to write about something different becouse I can't find any solvation of my problem: I have to write small application that connects to pacs and gets images. I decided to use dcm4che toolkit. I've written following code:

public static void main(String[] args) {
// TODO code application logic here
DcmQR dcmqr = new MyDcmQR("server");

dcmqr.setCalledAET("server", true);
dcmqr.setRemoteHost("213.165.94.158");
dcmqr.setRemotePort(104);
dcmqr.getKeys();

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

dcmqr.setQueryLevel(MyDcmQR.QueryRetrieveLevel.IMAGE);

dcmqr.addMatchingKey(Tag.toTagPath("PatientID"),"2011");
dcmqr.addMatchingKey(Tag.toTagPath("StudyInstanceUID"),"1.2.276.0.7230010.3.1.2.669896852.2528.1325171276.917");
dcmqr.addMatchingKey(Tag.toTagPath("SeriesInstanceUID"),"1.2.276.0.7230010.3.1.3.669896852.2528.1325171276.916");


dcmqr.configureTransferCapability(true);
List 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){
System.out.println(dco);
dcmTools.toByteArray(dco);
System.out.println("end parsing");
}

} catch (Exception e) {
System.out.println("error "+e);
}

try{
dcmqr.stop();
dcmqr.close();
}catch (Exception e) {

}

System.out.println("done");
}
}

As you can see I'd like to create ByteArray from DICOM data which I got running my code using Samuel's code:

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;
}

The problem is that when I run my code I get followin output:

List Size = 1
(0008,0052) CS #6 [IMAGE] Query/Retrieve Level
(0008,0054) AE #6 [server] Retrieve AE Title
(0020,000E) UI #54 [1.2.276.0.7230010.3.1.3.669896852.2528.1325171276.916] Series Instance UID
error java.lang.IllegalArgumentException: Missing (0002,0010) Transfer Syntax UID

I've read about this problem in dcm4che old forum:
http://forums.dcm4che.org/jiveforums/thread.jspa?threadID=2611
but I can't find solvation. The only thing I understood is that using my code I haven't got all DICOM image data I need and the problem is with/in DcmQR.createStorageService() method. Can anyone help me???


Unknown said...

Hi,

I'm using jdk8 and windows 8 environment and trying convert dicom (.dcm) to jpeg getting following issue.

Exception in thread "main" org.dcm4che2.data.ConfigurationError: No Image Reader of class com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib available for format:jpeg2000
at org.dcm4che2.imageio.ImageReaderFactory.getReaderForTransferSyntax(ImageReaderFactory.java:99)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initCompressedImageReader(DicomImageReader.java:410)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.initImageReader(DicomImageReader.java:395)
at org.dcm4che2.imageioimpl.plugins.dcm.DicomImageReader.read(DicomImageReader.java:636)
at dicom.Example1.main(Example1.java:34)


My code snippet here :

package dicom;

import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.dcm4che2.imageio.plugins.dcm.DicomImageReadParam;

import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codecimpl.JPEGCodec;
import com.sun.media.jai.codecimpl.JPEGImageEncoder;

public class Example1 {

static BufferedImage myJpegImage=null;

public static void main(String[] args) {
File file = new File("D:\\dicom_libs\\dcms\\IM-0001-0001.dcm");
Iterator iterator =ImageIO.getImageReadersByFormatName("DICOM");
while (iterator.hasNext()) {
ImageReader imageReader = (ImageReader) iterator.next();
DicomImageReadParam dicomImageReadParam = (DicomImageReadParam) imageReader.getDefaultReadParam();
try {
ImageInputStream iis = ImageIO.createImageInputStream(file);
imageReader.setInput(iis,false);
myJpegImage = imageReader.read(0, dicomImageReadParam);
iis.close();
if(myJpegImage == null){
System.out.println("Could not read image!!");
}
} catch (IOException e) {
e.printStackTrace();
}
File file2 = new File("D:\\dicom_libs\\test.jpg");
try {
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file2));
ImageEncoder encoder = JPEGCodec.createImageEncoder("test", outputStream, null);
encoder.encode(myJpegImage);
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Completed");
}

}
}

I'm using following jars :

dcm4che-core-2.0.28.jar

dcm4che-image-2.0.28.jar

dcm4che-imageio-2.0.28.jar

jai_imageio-1.1.jar

jai_windows-i586.jar

sun-jai_codec.jar

apache-logging-log4j.jar

slf4j-api-1.7.7.jar

slf4j-log4j12-1.7.7.jar



Unknown said...

greats. But i can't download JAI ImageIO Toolkit . Can you give me link other? thanks

ASKAR said...

Hey Hung duong quang,
Please find it here :
https://mvnrepository.com/artifact/com.github.jai-imageio/jai-imageio-core/1.2.1

Unknown said...

HI,

I trying to compress DICOM image in java.

Could please help me how to implement java code to compress DICOM image in java.

Thanks for helping...

Unknown said...

HI,

I trying to compress DICOM image in java.

Could please help me how to implement java code to compress DICOM image in java.

Thanks for helping...

DICOM library that i am using dcm4che3 5.22.3 version

erectile dysfunction causes said...

Wonderful work! This is the kind of information that should be shared around the internet. Shame on Google for no longer positioning this submit higher! Come on over and visit my web site . Thanks =)