Converting TIFF file to bufferedImage - java

I am trying to convert a Tiff file to Buffered Image.
I used ImageIO to do that but i figured out that it doesn't support this type of files.
ImageIO.read(file);
Should i use other than ImageIO or there's a solution ?

You need to put jai_imageio.jar in your classpath. check whether you have appropriate readers by using ImageIO.getReadersByFormatName("tiff").
Hope this helps.

Related

Java ImageIO read image in ppm format return null

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

ImageIO give me a CMMException due to bad JPEG

I try to read a JPEG file with ImageIO.read() but for this image it give me a CMMException. after read this and this I understand ImageIO can't read some kind of jpeg file.
So I need a solution to read all kind of images. JAI library look to be a dead library. And I don't undestrand how TwelveMonkeys works. So if someone have explainations about it or another alternative, I'll take it. Thank's
For reading most JPEGs (even those that that cause CMMExceptions), you can use ImageIO and TwelveMonkeys ImageIO plug-ins. To do so, add the following dependency to your Maven project:
<groupId>com.twelvemonkeys.imageio</groupId>
<artifactId>imageio-jpeg</artifactId>
<version>3.0</version>
If you already use ImageIO to read images, there's no need to change your code. :-)
To verify that the plugin is installed and used at run-time, you could use the following code:
Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
while (readers.hasNext()) {
System.out.println("reader: " + readers.next());
}
The first line should print:
reader: com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader#somehash

How to use Image in JRadioButtons instead of text

I have a group of JRadioButtons. I want to use image instead of text in my JRadioButtons. So I believe i will have to use this:
JRadioButton(Icon icon, boolean selected)
Now the issue is that I am not sure about how to create this icon. I have the image that I want to use and I have copied the image in my source code folder. It is in .tiff format. i want to read that .tiff image (inputStream I believe) and convert that to icon so that i can have my JRadioButtons.
Please help in implementing this.
Thanks in advance.
Assuming you put the image in your source folder, in the package com.foo.bar, and that your build process copies this file with your classes so that it's in the classpath when running the application (that's what all IDEs do by default), you can just use
new ImageIcon(MyClass.class.getResource("/com/foo/bar/MyImage.png"))
to get you an icon.
I'm not sure that Java has native support for the tiff format, so you might have to convert the image to another supported format to load it (gif, JPEG and PNG will work fine).
If you're getting a NullPointerException, it probably means that the image is not at the path you're indicating.
You said you stuck it right into the src folder so this should work:
new ImageIcon(getClass().getResource("icon.jpg"))

Getting the pixel value of a TIFF image in Java

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.

Reading JCS_YCCK images using ImageIO

I'm using ImageIO.read to process uploaded image files. The code is similar to
BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(image.getContents()));
I've managed to solve most of the issues, but this one has left me clueless. The uploaded image has a JCS_YCCK profile, as defined in com.sun.imageio.plugins.jpeg.JPEG, which is not supported by com.sun.imageio.plugins.jpeg.JPEGImageReader. This leads to a nice stack trace similar to:
Caused by: javax.imageio.IIOException: Unsupported Image Type
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(JPEGImageReader.java:910)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(JPEGImageReader.java:885)
at javax.imageio.ImageIO.read(ImageIO.java:1422)
at javax.imageio.ImageIO.read(ImageIO.java:1326)
at com.example.ImageWriter.resizeEmbeddableImageInPlace(ImageWriter.java:231)
How can I process this type of JPEG using Java's ImageIO?
Update: I've tried Commons-Sanselan, indicated by an answer, but unfortunately it does not support JPEG files:
org.apache.sanselan.ImageReadException: Sanselan cannot read or write JPEG images.
at org.apache.sanselan.formats.jpeg.JpegImageParser.getBufferedImage(JpegImageParser.java:90)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1264)
at org.apache.sanselan.Sanselan.getBufferedImage(Sanselan.java:1231)
One possible solution is to use the Java Advanced Imaging Image IO extensions. When properly installed, the conversion works out of the box.
The problem is that it does not play well with Maven, so I've asked Using Java Advanced Imaging with Maven. If that works out, this answer will be accepted.
I don't know for ImageIO, but you could use the Commons Sanselan library, which offers easy ways to access all sorts of images.

Categories