The ImageIO package doesn't work with .tif images and I cannot create a BufferedImage (Class I'm more familiar with) from a .tif file.
How do I easily get the pixel value of a TIFF image in Java? How can I do it FAST?
I'm not experienced with image processing and some sample code would be greatly appreciated!
Thanks!
You will need the Java Advanced Imaging API: JAI in order to work with TIFF images.
From the JAI API description:
TIFF
In addition to the baseline specification, the encoder and decoder support PackBits, modified Huffman and CCITT bilevel encodings (fax), JPEG-in-TIFF (per TIFF Technical Note #2), and DEFLATE compression schemes, can handle images with 16- and 32-bit integral samples and 32-bit floating point samples, and can read and write tiled images of all supported data types. The decoder in addition can decompress LZW-compressed imagery.
Additional features may be addressed in the future.
A single page of a multi-page TIFF file may loaded most easily by using the page parameter with the "TIFF" operator which is documented in the class comments of javax.media.jai.operator.TIFFDescriptor. A code sample is included here to show a means of loading a single page of a multi-page TIFF file using the ancillary codec classes directly.
Try out some of these tutorials.
Related
I have created a GUI using java Swing which displays some images. I have been testing it and have managed to create some labels which I have filled with JPEG images as a test.
Now, I face the problem that I cannot display the actual files i need to display because they are .nd2 files (from a Nikon microscope). I have been looking at how to use the Bio-formats and/or IJ packages to do so...but I don't know where to start.
Can anyone help? I am using the Eclipse IDE for Java
About the format
From https://www.file-extensions.org/
... The ND2 format uses JPEG-2000 compression, and also can be
uncompressed or Zip-compressed ...
As mentioned in read jpeg2000 files in java
JPEG 2000 seems to be not included inside standard Java SDK.
Potential solutions
1. Use Open JPEG + existing JNI wrapper
I would try out https://github.com/uclouvain/openjpeg and search for some java wrappers to use openjpeg (e.g. look at https://github.com/barmintor/openjpeg for an JNI approach for maven).
2. Use Open JPEG + Write own JNI wrapper
Another approach would be to look at
https://github.com/ThalesGroup/JP2ForAndroid/blob/master/library/src/main/java/com/gemalto/jp2/JP2Decoder.java , inspect involved classes etc. and write an own JNI wrapper
The mentioned github reposoitory code writes to android bitmap, so not directly usable for your Swing project, but it shows you the way to decode JPEG2000 format by native calls to OpenJPEG library
How to convert a byte[] to a BufferedImage in Java? describes conversion from byte array to a buffered image - so these information should help you to read the image data into a buffered image (so usable in Swing).
I need to convert a DICOM file (.dcm) in a jpeg2000 format, someone has a code snippet that perform this operation?
I found in the web only a way for the dcm to jpeg conversion (the first answer to that question: How to convert dicom file to jpg conversion), but that doesn't work for me.
Thanks in advice for te help and sorry for my not perfect english.
The best tool I encountered when doing image conversion is ImageMagick (http://www.imagemagick.org/), which supports Dicom as input and JPEG-2000 as output.
I also have used dcm4che for this purpose (it has the advantage to be a purely Java solution), but the JPEG-2000 encoder had a memory leak - so if you have to do it for lots of images, it might be a problem (recent version might be fixed though).
Does javax.imageio.ImageIO read image in PPM format (e.g., rawbits P6) at all?
BufferedImage rawimage = ImageIO.read(new File(getClass().getResource("/lena.ppm").getFile()));
There is no Exception but rawimage is null. Other formats like png and jpg work fine.
I have an awkward workaround, by striping off the header and scan ppm file line-by-line.
I am assuming the JAVA JAI (part of JDK now) will do it out-of-box.
Do I miss anything here?
To my knowledge, JAI is still not part of the JDK/JRE, it's a separate download/install. And, yes, you will need JAI ImageIO (or other 3rd party plugin, like my own) to read PPM, it is not supported out of the box.
ImageIO.read() will return null for formats it doesn't support. This is the expected/documented behavior.
You can query the formats you have installed support for, using ImageIO.getReaderFormatNames().
Decoding a PPM file is not natively available: "Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP." But you may be able to find a library (or plugin) that has this support, just search for something like java ppm imagereader.
With the ImageIO plugin (licensed under LGPL) from this page PPM (Portable PixMap), PGM, PBM image file java image reader/writer you could read/write the rawbit PPM files.
The JAR file would need at least following files
META-INF/
META-INF/MANIFEST.MF
META-INF/services/
META-INF/services/javax.imageio.spi.ImageReaderSpi
META-INF/services/javax.imageio.spi.ImageWriterSpi
uk/
uk/co/
uk/co/mmscomputing/
uk/co/mmscomputing/imageio/
uk/co/mmscomputing/imageio/ppm/
uk/co/mmscomputing/imageio/ppm/PPMConstants.class
uk/co/mmscomputing/imageio/ppm/PPMImageReader.class
uk/co/mmscomputing/imageio/ppm/PPMImageReaderSpi.class
uk/co/mmscomputing/imageio/ppm/PPMImageWriter.class
uk/co/mmscomputing/imageio/ppm/PPMImageWriterSpi.class
I'm attempting to get a JPEG into a BufferedImage to display on a JPanel . However, javax.imageio.ImageIO.read() seems to be very fussy about the JPEGs it processes, often throwing an IIOException. The same JPEGs open fine in pretty much any image reader you'd care to name.
I've looked at Apache's Sanselan and JAI. But Sanselan can't process JPEGs and JAI isn't available for 64-bit Windows platforms and doesn't seem to be maintained (the last update was in 2006). A previous answer on StackOverflow suggested com.sun.image.codec.jpeg.JPEGCodec, but this was deprecated in Java 5 and has disappeared in 7.
Are these my only options? Are there really no Java libraries capable of robustly reading JPEGs into a BufferedImage?
Legacy Toolkit methods such as createImage and getImage are known to be more lenient than ImageIO.
Werner Randelshofer also wrote a Service Provider to read CMYK JPEGs with ImageIO.
By combining both approaches (try every possible ImageReader and then fallback to Toolkit) you will be able to handle a reasonable number of JPEG Images.
Reading JPEGs with CMYK profile may be a interesting read.
I have to create thumbnails with images uploaded by my users. The image formats can be PNG, JPG, GIF. I gave a try to java.awt and javax.imageio but it is hard to deal with all the cases (image too large, image too small, image in XXX format, image with transparency...). I would prefer a library simple and not so verbose.
What java library do you use to convert / edit / create images ?
you might like to explore JMagick
Well, we use ImageIO and Apache Sanselan, but JAI or - if you don't mind using native libaries - jmagick should do as well.
image too large, image too small, image in XXX format, image with transparency
I'm not sure there is a library that doesn't have constraints like that. Since the images might be PNG, JPG or GIF, the format problem shouldn't apply.
Transparency is format dependent and thus should not depend on the library.
I chose Scalr, not need for extra ImageMagick or JNI wrapper :
http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
On the other hand, quality is better with JMagick (if you can compile it and make it work)
Snowbound has the RasterMaster Java Imaging SDK. There is a decompress_bitmap call that will read in PNG, JPG, GIF and many other formats. The IMG_resize_bitmap_bicubic method can then be used to make a nice thumbnail.
You can go to Snowbound and push the "free trial" button to get a 30-day free trial version. The SDK comes with a Thumbnails code sample. If you want to learn more about the methods above before you expose your email address you can view the online documentation at RasterMaster.com.
Full disclosure - yes, I work for Snowbound.